[英]How to move '__mocks__' folder in Jest to /test?
I have started using mocks in Jest for testing my NodeJS app and they're working nice, but it would make more sense to move __mocks__
folder to /test
folder so everything related to tests is there instead of mixing files in /src
.我已经开始在 Jest 中使用模拟来测试我的 NodeJS 应用程序,它们运行良好,但将__mocks__
文件夹移动到/test
文件夹更有意义,这样与测试相关的所有内容都在那里,而不是在/src
中混合文件。
Is it possible in some way?以某种方式可能吗? I cannot find it anywhere in Jest docs.我在 Jest 文档中的任何地方都找不到它。
jest.mock('../src/service', () => require('./__mocks__/request'));
这对我有用
In my package.json i have some jest config在我的 package.json 中,我有一些开玩笑的配置
"jest": {
"preset": "jest-preset-angular",
"transform": {
"^.+\\.js$": "babel-jest",
"^.+\\.(ts|html)$": "<rootDir>/node_modules/jest-preset-angular/preprocessor.js"
},
"transformIgnorePatterns": [
"node_modules/babel-runtime"
],
"setupTestFrameworkScriptFile": "<rootDir>/src/__tests__/setupJest.ts",
"moduleNameMapper": {
"mockData": "<rootDir>/src/__tests__/data",
"mockService": "<rootDir>/src/__tests__/services"
},
"testResultsProcessor": "jest-junit",
"testMatch": [
"**/?(*.)(spec).ts?(x)"
]
},
"jest-junit": {
"suiteName": "jest tests",
"output": "./reports/tests/TESTS-junit.xml",
"classNameTemplate": "{classname}-{title}",
"titleTemplate": "{classname}-{title}",
"usePathForSuiteName": "true"
}
The "moduleNameMapper" part is what you need, I added these parts after my devDependencies. “moduleNameMapper”部分正是您所需要的,我在 devDependencies 之后添加了这些部分。 You can also create a config file elsewhere.您还可以在其他地方创建配置文件。
https://jestjs.io/docs/en/configuration https://jestjs.io/docs/en/configuration
EDIT:编辑:
You have to modify these path :您必须修改这些路径:
"mockData": "<rootDir>/src/__tests__/data",
"mockService": "<rootDir>/src/__tests__/services"
So if you want Mock outside src folder you can replace my conf by :因此,如果您想在 src 文件夹外进行 Mock,则可以将我的 conf 替换为:
"mockData": "<rootDir>/__tests__/data",
"mockService": "<rootDir>/__tests__/services"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.