我使用 create-react-app 创建了一个 reactjs 应用。
运行该应用程序有效。运行测试有效。非常好,也很容易。
然后为 gitlab 创建了一个管道来运行构建和测试。
在 gitlab 上成功构建,但在测试时失败。
> react-scripts test
PASS src/App.test.js
✓ renders without crashing (25ms)
Test Suites: 1 passed, 1 total
Tests: 1 passed, 1 total
Snapshots: 0 total
Time: 1.955s
Ran all test suites.
npm ERR! Test failed. See above for more details.
无论如何,我无法找到有关失败原因的更多详细信息,也不知道如何解决。
运行 npm test --verbose 后,这里是新的输出:
$npm test --verbose
npm info it worked if it ends with ok
npm verb cli [ '/usr/local/bin/node',
npm verb cli '/usr/local/bin/npm',
npm verb cli 'test',
npm verb cli '--verbose' ]
npm info using npm@6.5.0
npm info using node@v11.7.0
npm verb run-script [ 'pretest', 'test', 'posttest' ]
npm info lifecycle easyauto123@0.1.0~pretest: easyauto123@0.1.0
npm info lifecycle easyauto123@0.1.0~test: easyauto123@0.1.0
> easyauto123@0.1.0 test /builds/easyauto123/website
> react-scripts test
PASS src/App.test.js
✓ renders without crashing (30ms)
Test Suites: 1 passed, 1 total
Tests: 1 passed, 1 total
Snapshots: 0 total
Time: 2.13s
Ran all test suites.
npm verb lifecycle easyauto123@0.1.0~test: unsafe-perm in lifecycle true
npm verb lifecycle easyauto123@0.1.0~test: PATH: /usr/local/lib/node_modules/npm/node_modules/npm-lifecycle/node-gyp-bin:/builds/easyauto123/website/node_modules/.bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
npm verb lifecycle easyauto123@0.1.0~test: CWD: /builds/easyauto123/website
npm info lifecycle easyauto123@0.1.0~test: Failed to exec test script
npm ERR! Test failed. See above for more details.
npm verb exit [ 1, true ]
npm timing npm Completed in 3576ms
npm verb code 1
ERROR: Job failed: exit code 1
最佳答案
您可以验证您的项目中是否配置了任何阈值。
根据 jest 文档,https://jestjs.io/docs/configuration ,如果你在你的 jest.config.js
上设置了一个 coverageThreshold
如果分支、行和函数覆盖率低于 80%,则测试将失败,根据配置下面。
"coverageThreshold": {
"global": {
"branches": 80,
"functions": 80,
"lines": 80,
"statements": -10
}
}
您可以通过所有测试,但如果您未达到指定的阈值,jest 将失败。此外,它不会在错误日志中给你任何关于此的提示。
我在一个角度项目中遇到了同样的问题,这是由于这种配置造成的。
关于reactjs - "npm test"虽然测试成功但失败(使用 create-react-app 创建的 reactjs 应用程序),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54301678/