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

Yus*_*ick 10 testing githooks reactjs jestjs create-react-app

我有一个使用Create-React-App创建的项目.我希望添加一个precommit钩子来运行我们的linter并测试pre-commit包.

"pre-commit": [
  "precommit-msg",
  "lint",
  "test"
],
Run Code Online (Sandbox Code Playgroud)

但是,由于测试脚本默认在监视模式下运行,因此可以防止提交实际发生.如何在预提交中添加不在监视中移动的测试?

Boo*_*gie 29

您可以使用 --watchAll=false 参数。例如,您可以像这样创建另一个脚本:

"scripts": {
  "test:nowatch": "react-scripts test --watchAll=false",
}
Run Code Online (Sandbox Code Playgroud)

然后运行

"pre-commit": [
  "precommit-msg",
  "lint",
  "test:nowatch"
],
Run Code Online (Sandbox Code Playgroud)


Yus*_*ick 7

通过在package.json文件中添加以下脚本,我找到了设置的解决方案。

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

"pre-commit": [
  "precommit-msg",
  "lint",
  "test:nowatch"
],
Run Code Online (Sandbox Code Playgroud)

这来自以下线程:https : //github.com/facebook/create-react-app/issues/2336

  • @RohanBagchi是的,上面有窗户。但是,在Windows上使用git bash可以运行类似的脚本:`“ test:all”:“ set CI = true && react-scripts test”` (2认同)

小智 6

跨平台解决方案:

  1. 安装跨环境
  2. 将您的测试命令与此类道具一起使用: "test:nowatch": "cross-env CI=true react-scripts test --env=jsdom --findRelatedTests"


小智 6

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