我正在尝试开始使用jest并进行快照测试。我实际上已经有了这个工作,我来了,看到了下面的错误,所以我继续阅读并尽可能地升级包,但我仍然得到错误。
Splash.test.js
import { shallow } from 'enzyme';
import shallowToJson from 'enzyme-to-json';
import Splash from './Splash';
it('Splash page is rendered', () => {
const result = shallow(
<Splash />,
);
expect(shallowToJson(result)).toMatchSnapshot();
});
终端输出
> jest
FAIL src/components/Splash/Splash.test.js
● Test suite failed to run
Cannot find module 'enzyme/build/RSTTraversal' from 'shallow.js'
at Resolver.resolveModule (node_modules/jest-resolve/build/index.js:194:17)
at Object.<anonymous> (node_modules/enzyme-to-json/shallow.js:15:21)
Test Suites: 1 failed, 1 total
Tests: 0 total
Snapshots: 0 total
Time: 0.83s
Ran all test suites.
大型Package.json
{
....
"scripts": {
"test": "jest",
},
"dependencies": {
"enzyme": "^2.9.1",
"react": "^15.5.0",
"react-addons-test-utils": "^15.5.0",
"react-css-modules": "^4.1.0",
"react-dom": "^15.5.0",
"react-redux": "^5.0.3",
"react-router": "^4.1.1",
"react-router-dom": "^4.0.0",
"react-router-page-transition": "^3.0.2",
"react-router-redux": "^5.0.0-alpha.6",
"react-tap-event-plugin": "^2.0.1",
"react-test-renderer": "^15.5.4",
"redux": "^3.6.0",
"webpack": "^2.2.1"
......
},
"devDependencies": {
......
"babel-cli": "^6.24.0",
"babel-core": "^6.24.0",
"babel-jest": "^22.0.6",
"babel-loader": "^6.4.0",
"babel-plugin-dynamic-import-node": "^1.1.0",
"babel-plugin-module-resolver": "^2.7.1",
"babel-plugin-react-css-modules": "^3.3.0",
"babel-plugin-transform-react-require": "^1.0.1",
"babel-plugin-transform-react-stateless-component-name": "^1.1.2",
"babel-plugin-transform-runtime": "^6.23.0",
"babel-preset-env": "^1.2.2",
"babel-preset-es2015": "^6.24.0",
"babel-preset-react": "^6.23.0",
"babel-preset-react-hmre": "^1.1.1",
"babel-preset-stage-0": "^6.24.1",
"babel-register": "^6.24.1",
"babel-runtime": "^6.23.0",
"enzyme-to-json": "^3.3.0",
"jest": "^22.0.6",
"jest-cli": "^22.0.6",
"jest-enzyme": "^4.0.2",
"react-hot-loader": "^3.0.0-beta.7",
"redux-actions": "^2.2.1",
"redux-devtools-extension": "^2.13.2",
"redux-form": "^6.7.0",
"redux-logger": "^3.0.0",
"redux-persist": "^4.8.0",
"regenerator-runtime": "^0.11.0",
"require-hacker": "^3.0.0",
"resolve-url-loader": "^2.0.2",
"style-loader": "^0.16.0",
"svg-react-loader": "^0.4.5",
"url-loader": "^0.5.8",
"webpack-config": "^7.0.0",
"webpack-dashboard": "^0.3.0",
"webpack-dev-server": "^2.4.2"
},
......
"jest": {
"setupTestFrameworkScriptFile": "./node_modules/jest-enzyme/lib/index.js",
"moduleFileExtensions": [
"js"
],
"moduleDirectories": [
"node_modules"
],
"testPathIgnorePatterns": [
"<rootDir>/node_modules/",
"<rootDir>/app/"
],
"moduleNameMapper": {
"moduleNameMapper": {
"\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$": "<rootDir>/__mocks__/fileMock.js",
"\\.(css|less)$": "identity-obj-proxy"
}
},
"transform": {
"^.+\\.js$": "babel-jest"
}
}
}
答案 0 :(得分:2)
当我的酶与酶-json之间的版本不匹配时,我得到了同样的错误。您需要升级酶或将酶降级为json。
我目前在不同的项目中有两种不同的组合:
酶2.8.2&amp;酶-json 1.5.0
酶3.3.0&amp;酶-json 3.3.0
答案 1 :(得分:0)
尝试将shallowToJson
导入替换为:
import { shallowToJson } from 'enzyme-to-json';