javascript - 如何在 jest setupFiles 中访问 'expect'

标签 javascript jestjs

我想创建一个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/

相关文章:

javascript - 创建 React App ,所有测试用例都因 1 个错误而失败

javascript - 如何在 Jest 测试中防止 "polluting"变量?

jestjs - 用 enzyme 测试回调 Prop

javascript - 它不允许我使用 css 设计我的 <div>

javascript - 重绘/刷新/设置超时 - 数据 xmlhttp.responseText

javascript - AngularJS:基于链接输入字段的自定义验证

javascript - 使用 Vue 和 Jest/Vue 测试工具模拟 API 调用

javascript - 按设定的时间间隔刷新 DIV 元素

javascript - 第一次加载时出现 Angularjs $on $routeChangeSuccess 错误

node.js - jest 是否运行用于测试 "toThrowError"的包装函数?