jest 是否会在测试模块之间自动恢复模拟模块? [英] Does jest automatically restore mocked modules between test modules?

查看:25
本文介绍了jest 是否会在测试模块之间自动恢复模拟模块?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

jest 是否会在测试文件之间自动恢复模拟模块?例如,如果我在一个文件中调用 jest.mock('some_module') ,我是否需要确保在所有这些之后调用 jest.unmock('some_module')测试是在那个文件中运行的?

Does jest automatically restore mocked modules between test files? For example, if I call jest.mock('some_module') in one file, do I need to ensure I call jest.unmock('some_module') after all the tests are run in that file?

我不清楚文档中是否发生了这种情况.

It's not clear to me whether that happens in the documentation.

推荐答案

您不必重置模拟,因为测试是并行运行的,每个测试文件都在其自己的沙盒线程中运行.即使像 DateMath.random 这样的模拟 JavaScript 全局变量也只会影响实际的测试文件.

You don't have to reset the mocks, as the test are run in parallel, every test file run in its own sandboxed thread. Even mocking JavaScript globals like Date or Math.random only affects the actual test file.

到目前为止,我们遇到的唯一问题是模拟 process.env.NODE_ENV,这会影响同时运行的其他测试.但是在测试运行后重置它解决了问题.

The only problem we had so far was mocking process.env.NODE_ENV which affected other test that run at the same time. But reseting this after the test run solved the problem.

这篇关于jest 是否会在测试模块之间自动恢复模拟模块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆