我已经读到 src/setupTests.js 应该在每次测试之前加载,但我仍然得到每个测试失败并出现错误:
"Enzyme Internal Error: Enzyme expects an adapter to be configured, but found none."
这是我的 src/setupTests.js
import Enzyme from 'enzyme';
import Adapter from 'enzyme-adapter-react-16';
import 'whatwg-fetch';
Enzyme.configure({ adapter: new Adapter() });
这是我的 package.json:
"devDependencies": {
"babel-core": "^6.26.3",
"babel-jest": "^23.6.0",
"babel-plugin-transform-class-properties": "^6.24.1",
"babel-preset-env": "^1.7.0",
"babel-preset-react": "^6.24.1",
"coveralls": "^3.0.2",
"enzyme": "^3.7.0",
"enzyme-adapter-react-16": "^1.6.0",
"enzyme-to-json": "^3.3.4",
"jest": "^23.6.0",
"react-test-renderer": "^16.5.2",
"redux-mock-store": "^1.5.3",
"regenerator-runtime": "^0.12.1",
"whatwg-fetch": "^3.0.0"
}
我正在通过 npm run test
运行“测试”脚本
"scripts": {
"test": "jest",
"test:watch": "jest --watch",
"test:updateSnapshots": "jest --updateSnapshot"
}
我以以下某种形式开始每个测试文件:
import React from 'react';
import { shallow } from 'enzyme';
我使用的是 React 16 和 React-scripts 2.1.1
我做错了什么任何人都可以看到的事情吗?
最佳答案
将以下内容添加到 package.json 后是否有效
"scripts": {
...
"test": "react-scripts test",
...
}
关于reactjs - setupTests.js 未在 CRA React 应用程序中自动加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53642594/