官术网_书友最值得收藏!

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.
主站蜘蛛池模板: 柏乡县| 洛隆县| 浑源县| 谷城县| 武陟县| 平塘县| 桐乡市| 吉林省| 华蓥市| 沾化县| 阿图什市| 丰原市| 平利县| 丰顺县| 潍坊市| 天祝| 丹巴县| 东至县| 加查县| 榆社县| 黄平县| 浦江县| 贡嘎县| 苍溪县| 山东| 安福县| 浦县| 东乌| 涡阳县| 淄博市| 息烽县| 广灵县| 博野县| 张家港市| 城口县| 岐山县| 明水县| 肥城市| 巴马| 延安市| 云阳县|