- 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.
推薦閱讀
- 大學(xué)計算機(jī)基礎(chǔ)(第三版)
- Learning Cython Programming
- Practical Data Science Cookbook(Second Edition)
- Mastering QGIS
- PLC編程及應(yīng)用實(shí)戰(zhàn)
- CKA/CKAD應(yīng)試教程:從Docker到Kubernetes完全攻略
- Mastering Python Networking
- Mastering Data Mining with Python:Find patterns hidden in your data
- Kubernetes進(jìn)階實(shí)戰(zhàn)
- JSP程序設(shè)計實(shí)例教程(第2版)
- Kotlin Programming By Example
- C++程序設(shè)計
- OpenCV 3.0 Computer Vision with Java
- H5頁面設(shè)計與制作(全彩慕課版·第2版)
- 深入實(shí)踐C++模板編程