Mistake: Shared 可变状态
找错误 中等
题目
什么是 错误使用这个测试 suite?
代码
let items = [];
test('add item', () => {
items.push('a');
expect(items).toHaveLength(1);
});
test('add another', () => {
items.push('b');
expect(items).toHaveLength(1);
});
提示
测试 share 项数组 — 顺序 matters.
解释
mistake 是 shared 可变状态. 项 是 declared 之外测试 and 从不 reset. 其次测试添加到 数组那个 already 有 '' 从首先测试, 所以 项 有长度 2, 不 1. fix: use beforeEach(() => { 项 = []; }); 到 reset 状态之前每个测试.
相关资源
相关课程
- 设置 and Teardown
测试 Hook for 设置 and cleanup.
查阅不认识的术语
新手友好的术语表用大白话解释编程术语——变量、函数、DOM、Promise 等等。
解读错误信息
用大白话解释常见错误——什么意思、为什么发生、如何修复。
动手做项目
通过引导式项目应用所学,含起始代码与解决方案。