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

IT技术 reactjs testing jestjs create-react-app githooks
2021-04-28 08:44:00

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

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

然而,由于测试脚本默认在监视模式下运行,这会阻止提交实际发生。如何在预提交中添加不在 watch move 中的测试?

4个回答

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

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

然后运行

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

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

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

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

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

跨平台解决方案:

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

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