"devDependencies": {
"@babel/core": "^7.8.4",
"@babel/preset-env": "^7.8.4",
"@babel/preset-react": "^7.8.3",
"babel-plugin-transform-es2015-modules-commonjs": "^6.26.2",
"enzyme": "^3.11.0",
"enzyme-adapter-react-16": "^1.15.2",
"react": "^15.3.1",
"redux-mock-store": "^1.5.4"
}
//Test.js
import React from "react";
import { render, unmountComponentAtNode } from "reactdom";
import { shallow,mount } from "enzyme";`enter code here`
import {Provider} from 'react-redux'
import configureMockStore from 'redux-mock-store';
import App from "../../App";
let container = null;
let mockStore = configureMockStore()
beforeEach(() => {
container = document.createElement("div");
document.body.appendChild(container);
});
afterEach(() => {
unmountComponentAtNode(container);
container.remove();
container = null;
});
it('It RENDERS WITHOUT CRASHING', () => {
})```
我收到此错误
●测试套件无法运行。我尝试配置jest和babelrc,但没有任何效果。当我开始使用redux存储测试组件时,它对于纯React组件工作正常,但会引发错误。
Cannot find module 'react' from 'Provider.js'
However, Jest was able to find:
'components/Provider.js'
You might want to include a file extension in your import, or update your 'moduleFileExtensions', which is currently ['web.js', 'js', 'web.ts', 'ts', 'web.tsx', 'tsx', 'json', 'web.jsx', 'jsx', 'node'].
See https://jestjs.io/docs/en/configuration#modulefileextensions-array-string
at Resolver.resolveModule (node_modules/jest-resolve/build/index.js:259:17)
at Object.<anonymous> (../../node_modules/react-redux/lib/components/Provider.js:10:38)
只需执行npm install --save react
,就可以了。
npm install --save react