简体   繁体   English

不在监视模式下运行 Create-React-App 测试

[英]Run Create-React-App Tests not in Watch Mode

I have a project created using Create-React-App.我有一个使用 Create-React-App 创建的项目。 I am looking to add in a precommit hook to run our linter and tests with the pre-commit package.我希望添加一个precommit钩子来运行我们的 linter 并使用pre-commit包进行测试。

"pre-commit": [
  "precommit-msg",
  "lint",
  "test"
],

However, since the test script runs by default in watch mode, this prevents the commit from ever actually happening.然而,由于测试脚本默认在监视模式下运行,这会阻止提交实际发生。 How can add the tests not in watch move in the pre-commit?如何在预提交中添加不在 watch move 中的测试?

You can use the --watchAll=false parameter.您可以使用 --watchAll=false 参数。 So for example you can create another script like this:例如,您可以像这样创建另一个脚本:

"scripts": {
  "test:nowatch": "react-scripts test --watchAll=false",
}

And then run然后运行

"pre-commit": [
  "precommit-msg",
  "lint",
  "test:nowatch"
],

I found a solution for my setup by adding the following script in my package.json file.我通过在我的package.json文件中添加以下脚本为我的设置找到了解决方案。

"test:nowatch": "CI=true react-scripts-ts test --env=jsdom",

"pre-commit": [
  "precommit-msg",
  "lint",
  "test:nowatch"
],

This came from the following thread: https://github.com/facebook/create-react-app/issues/2336这来自以下线程: https : //github.com/facebook/create-react-app/issues/2336

Cross platform solution for this:跨平台解决方案:

  1. Install cross-env安装跨环境
  2. Use your test command with such props: "test:nowatch": "cross-env CI=true react-scripts test --env=jsdom --findRelatedTests"使用带有以下道具的测试命令: "test:nowatch": "cross-env CI=true react-scripts test --env=jsdom --findRelatedTests"

只需添加CI=true像这样的"test": "CI=true react-scripts test"对我有用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM