Ken*_*ong 6 javascript unit-testing reactjs jestjs
有没有办法让它变得总是嘲笑我的模块所以我不需要在我所有的开玩笑测试中包含它?
目前我有一个配置文件
// src/__mocks__/config.js
export default {
dbConfig: 'TestDBConfiguration'
};
// src/config.js
export default {
dbConfig: ... // Some other stuff
};
Run Code Online (Sandbox Code Playgroud)
要运行我的单元测试,我必须使用模拟的config.js.目前在我的所有单元测试中,我必须包括jest.mock('../ config');
// org.test.js
jest.mock('../../config');
import db from '../db'; // This internally uses the config file
import otherFile from '../otherFile'; // This also uses the config file
// Code that calls both db/otherFile
Run Code Online (Sandbox Code Playgroud)
有时当我创建测试时,我可能忘记包含jest.mock('../ config'),这是我希望它包含在我所有测试中的原因之一.
我已经研究过启用automock但它似乎打破了我的测试.https://facebook.github.io/jest/docs/en/configuration.html#automock-boolean
小智 12
您可以将setupTestFrameworkScriptFile添加到您的jest配置中,以便在所有测试之前运行.
https://facebook.github.io/jest/docs/en/configuration.html#setuptestframeworkscriptfile-string
// package.json
...
"jest": {
"setupTestFrameworkScriptFile": "./src/setupTests.js"
}
// setupTests.js
jest.mock('config'); // Path to config file
Run Code Online (Sandbox Code Playgroud)
Edu*_*tet 10
Jest 文档已更新并用于setupFilesAfterEnv运行可以利用 Jest 功能的代码,例如jest.mock().
更新@smirky的示例以使用setupFilesAfterEnv:
// package.json
...
"jest": {
"setupFilesAfterEnv": ["./src/setupTests.js"]
}
// setupTests.js
jest.mock('config'); // Path to config file
Run Code Online (Sandbox Code Playgroud)
在旁注中,如果您希望为import/require您运行的每个测试模块,您可以使用setupFiles.
| 归档时间: |
|
| 查看次数: |
3261 次 |
| 最近记录: |