- Mastering React Test:Driven Development
- Daniel Irvine
- 159字
- 2021-06-24 14:45:03
Using a beforeEach block
Both of our tests start with some setup or arrangement. When that setup is common to all tests, we can promote them into a beforeEach block instead. Code in this block is executed before each test.
Above your first test, write the following code, and delete the corresponding call to createElement from each of your two tests:
beforeEach(() => {
container = document.createElement('div');
});
Since we defined container in the scope of the describe block, the value set here in the beforeEach block will be available to our test once it executes.
Be careful when you use variables defined within the describe scope. These variables are not cleared between each test execution, so you are running the risk of non-independent tests. Therefore, any variable you declare in the describe scope should be assigned to a new value in a corresponding beforeEach block, or in the first part of each test, just as we've done here.
推薦閱讀
- Spring 5.0 Microservices(Second Edition)
- Python科學計算(第2版)
- Cross-platform Desktop Application Development:Electron,Node,NW.js,and React
- OpenStack Cloud Computing Cookbook(Fourth Edition)
- Instant 960 Grid System
- Learning Three.js:The JavaScript 3D Library for WebGL
- Learning FuelPHP for Effective PHP Development
- Programming with CodeIgniterMVC
- After Effects CC案例設計與經典插件(視頻教學版)
- MySQL數據庫教程(視頻指導版)
- 你好!Python
- Roslyn Cookbook
- Mathematica Data Visualization
- SAP HANA Cookbook
- Visual C++實用教程