【发布时间】:2021-05-26 21:39:42
【问题描述】:
我在从我的create-react-app 的package.json 中的脚本运行完整测试覆盖时遇到了一些困难。链接到 repo here;
package.json
{
"name": "React Kitchen Sink",
...
"dependencies": {
...
"react": "^16.14.0",
"react-dom": "^16.14.0",
"react-scripts": "^3.4.4"
},
"scripts": {
"start": "react-scripts -r @cypress/instrument-cra start",
"start:silent": "BROWSER=none yarn start",
"start:server": "start-server-and-test start:silent http://localhost:3000",
"build": "react-scripts build",
"eject": "react-scripts eject",
"jest:test": "react-scripts test --env=jest-environment-jsdom-sixteen",
"jest:coverage": "react-scripts test --env=jest-environment-jsdom-sixteen --watchAll=false --coverage",
...
...
运行
yarn jest:test
在覆盖范围内以监视模式开始测试,如果我按a,那么我会得到一份所有测试都通过的报告
如果我这样做了
yarn jest:coverage
那么所有的测试都失败了
错误看起来像这样:
FAIL src/components/Photo/index.test.js
● Console
console.error node_modules/jsdom/lib/jsdom/virtual-console.js:29
Error: Uncaught [Error: Photo(...): Nothing was returned from render. This usually means a return statement is missing. Or, to render nothing, return null.]
at reportException (/Users/norfeldt/Abtion/Playground/react-kitchen-sink/node_modules/jsdom/lib/jsdom/living/helpers/runtime-script-errors.js:62:24)
at innerInvokeEventListeners (/Users/norfeldt/Abtion/Playground/react-kitchen-sink/node_modules/jsdom/lib/jsdom/living/events/EventTarget-impl.js:333:9)
at invokeEventListeners
...
The above error occurred in the <Photo> component:
in Photo (at Photo/index.test.js:32)
Consider adding an error boundary to your tree to customize error handling behavior.
Visit <link> to learn more about error boundaries.
● has a test id
Photo(...): Nothing was returned from render. This usually means a return statement is missing. Or, to render nothing, return null.
...
【问题讨论】:
-
如果直接运行
react-scripts命令会发生什么? -
你的意思是没有
--env=jest..??? -
我的意思是不要运行
yar test:coverage,而是尝试运行react-scripts test --env=jest-environment-jsdom-sixteen --watchAll=false --coverage。 (您可能必须提供react-scripts实用程序的完整路径。)这至少有助于缩小问题范围。 -
@srk 这给了我同样失败的测试。我已经更新了我的问题以显示一些错误
-
@Norfeldt 尝试将
react-scripts升级到版本 >=4.0.0 :)
标签: reactjs jestjs code-coverage create-react-app babel-jest