我一直在尝试配置Jest,以便它可以运行在相同层次结构中另一个文件夹中编写的测试用例。但这会引发以下错误。
当我的测试用例文件和要测试的实际代码位于asCore文件夹中时,我试图从as_test文件夹中运行笑话。
我认为Jest无法找到酶,因为它位于as_test的node_modules中,而使用它的测试用例位于asCore文件夹中。
有人可以帮我解决这个问题吗?
● Test suite failed to run
Cannot find module 'enzyme' from 'Button.test.js'
1 |
> 2 | import { shallow, mount } from 'enzyme';
| ^
3 | import Button from '../JSX/Button';
4 |
5 |
at Resolver.resolveModule (as_test/node_modules/jest-resolve/build/index.js:221:17)
at Object.<anonymous> (asCore/components/button/__tests__/Button.test.js:2:1)
Package.JSON
{
"private": true,
"version": "0.0.0",
"name": "example-enzyme",
"dependencies": {
"react": "16.5.2",
"react-dom": "16.5.2"
},
"devDependencies": {
"@babel/core": "^7.1.2",
"@babel/node": "^7.0.0",
"@babel/preset-env": "^7.1.0",
"@babel/preset-react": "^7.0.0",
"@babel/register": "^7.0.0",
"babel-core": "^7.0.0-bridge.0",
"babel-jest": "*",
"babel-loader": "^8.0.4",
"jest": "*",
"react-test-renderer": "*",
"enzyme": "*",
"enzyme-adapter-react-16": "*",
"enzyme-to-json": "*",
"jest-html-reporter": "*"
},
"scripts": {
"test": "jest --no-cache"
}
}
Jest.config.js
const { resolve } = require('path');
const root = resolve(__dirname, '..');
console.log(root);
module.exports = {
"verbose": true,
"rootDir": root,
"setupTestFrameworkScriptFile": "<rootDir>/as_test/config/jestSetup.js",
"collectCoverage": true,
"coverageDirectory":"<rootDir>/as_test/coverage",
"coverageReporters": [
"html", "text"
],
"moduleDirectories": ["node_modules", "bower_components", "<rootDir>/*"],
"testResultsProcessor": "<rootDir>/as_test/node_modules/jest-html-reporter",
"snapshotSerializers": ["<rootDir>/as_test/node_modules/enzyme-to-json/serializer"],
"moduleNameMapper": {
"core/extendables/reactHelper$": "<rootDir>/as_test/deps/js/reactHelper.js",
"core/components/button/JSX-generated/Button$": "<rootDir>/as_test/deps/jsx/Button"
}
};
babel.config.js
module.exports = {
presets: [['@babel/preset-env'],'@babel/preset-react'],
};
JestSetup.js
import Enzyme from 'enzyme';
import Adapter from 'enzyme-adapter-react-16';
Enzyme.configure({ adapter: new Adapter() });