Mistake: 不 Restoring Mock
找错误 中等
题目
什么是 错误使用这个 spy usage?
代码
test('spy test', () => {
jest.spyOn(console, 'log').mockImplementation(() => {});
console.log('hello');
// test continues...
});
提示
spy replaces console.log permanently 如果不 restored.
解释
mistake 是不 restoring spy 之后测试. console.log remains mocked for 所有 subsequent 测试, 哪个能 cause confusing failures. fix: call spy.mockRestore() in afterEach, or use jest.restoreAllMocks() in afterEach. Better yet, use jest.spyOn 在内 beforeEach/afterEach pair.
相关资源
相关课程
- Mocking
Using mocks 到 isolate code.
查阅不认识的术语
新手友好的术语表用大白话解释编程术语——变量、函数、DOM、Promise 等等。
解读错误信息
用大白话解释常见错误——什么意思、为什么发生、如何修复。
动手做项目
通过引导式项目应用所学,含起始代码与解决方案。