- 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.
推薦閱讀
- DevOps:軟件架構(gòu)師行動指南
- Mobile Web Performance Optimization
- SOA實(shí)踐
- JavaScript 從入門到項(xiàng)目實(shí)踐(超值版)
- Java EE框架整合開發(fā)入門到實(shí)戰(zhàn):Spring+Spring MVC+MyBatis(微課版)
- JavaScript語言精髓與編程實(shí)踐(第3版)
- 程序員數(shù)學(xué):用Python學(xué)透線性代數(shù)和微積分
- HTML5游戲開發(fā)案例教程
- JavaScript入門經(jīng)典
- Swift細(xì)致入門與最佳實(shí)踐
- Rust游戲開發(fā)實(shí)戰(zhàn)
- 軟件項(xiàng)目管理實(shí)用教程
- 深度學(xué)習(xí)原理與PyTorch實(shí)戰(zhàn)(第2版)
- Building Serverless Web Applications
- Python 3 數(shù)據(jù)分析與機(jī)器學(xué)習(xí)實(shí)戰(zhàn)