我想创建一个custom matcher开 Jest 。我希望这应该放在我的 jest-setup.js 文件中,在我的 jest.config.js 文件中指定。
jest-setup.js
// ...
expect.extend({
myCustomMatcher(received, val) {
// ...
}
});
不幸的是,虽然 jest
在执行过程中是一个全局变量,但 expect
似乎不是。我可以将我的扩展放入帮助程序文件中,并为使用它的任何测试套件导入该文件,但这似乎是错误的解决方案。
有没有办法从jest
变量(或其他方式)访问expect
?
最佳答案
您可以使用setupFilesAfterEnv
来执行此操作。来自 the docs (强调我的):
setupFilesAfterEnv [array]
A list of paths to modules that run some code to configure or set up the testing framework before each test. Since setupFiles executes before the test framework is installed in the environment, this script file presents you the opportunity of running some code immediately after the test framework has been installed in the environment.
关于javascript - 如何在 jest setupFiles 中访问 'expect',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58103938/