【问题标题】:"npm test" fails although tests are successful (reactjs app created with create-react-app)尽管测试成功,但“npm test”失败(使用 create-react-app 创建的 reactjs 应用程序)
【发布时间】:2019-01-22 05:18:03
【问题描述】:

我使用 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

【问题讨论】:

  • 能否在本地运行yarn test并检查是否成功运行?
  • 在 gitlab build 中使用 --verbose 选项运行您的测试。它应该为您提供有关实际损坏位置的更多信息。 npm run test --verbose
  • 谢谢。我使用了 --verbose 并用新的输出更新了问题。
  • 运行 yarn test 而不是 npm test 并没有什么不同。我都尝试过,结果相同。
  • 有趣!测试用例失败,错误代码为 1。正如你所说,那里没有太多信息。

标签: reactjs jestjs create-react-app


【解决方案1】:

您可以验证您的项目中是否配置了任何阈值

根据笑话文档https://jestjs.io/docs/configuration,如果您在jest.config.js 上设置coverageThreshold,则根据以下配置,如果分支、行和函数覆盖率低于 80%,则测试将失败。

"coverageThreshold": {
  "global": {
    "branches": 80,
    "functions": 80,
    "lines": 80,
    "statements": -10
  }
}

您可以通过所有测试,但如果您未达到指定的阈值,则 jest 将失败。而且,它并没有在错误日志上给你任何提示。

我在 Angular 项目中遇到了同样的问题,这是由于这种配置造成的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-05-06
    • 1970-01-01
    • 1970-01-01
    • 2021-11-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多