调试测试时自动设置 jest 超时的最佳方法是什么?

Rob*_*man 6 javascript typescript jestjs

我有一个nodejs项目。我正在使用调试器(如果重要的话在 IntelliJ 中)来逐步完成测试。我想使用最佳实践配置我的项目,以便在使用调试器时将测试超时修改为长于默认的 5 秒。我知道我可以将以下行放入 package.json 中来更改超时。

{
  "name": "my-project",
  "jest": {
    "testTimeout": 20000
  }
}
Run Code Online (Sandbox Code Playgroud)

但我正在寻找一种在调试时仅(并且自动)修改超时的方法。我确信有一个好方法,但我对 Nodejs 和设置各种环境的约定/机制相当陌生。

Den*_*nis 7

如果使用create-react-app,则setupFilesAfterEnv配置为src\setupTests.ts

这是执行/调试任何测试之前的入口点,如果您可以检测到您处于调试模式,那么您可以设置新的超时。

例如,

if (process.env.DEBUG === 'jest') {
  jest.setTimeout(5 * 60 * 1000);
}
Run Code Online (Sandbox Code Playgroud)

在 VS Code 中,您可以launch.json按照添加"DEBUG": "jest"env.

{
  "name": "Debug CRA Tests",
  "type": "node",
  "request": "launch",
  "runtimeExecutable": "${workspaceRoot}/node_modules/.bin/react-app-rewired",
  "args": ["test", "--runInBand", "--no-cache", "--watchAll=false"],
  "cwd": "${workspaceRoot}",
  "protocol": "inspector",
  "console": "integratedTerminal",
  "internalConsoleOptions": "neverOpen",
  "env": {
    "CI": "true",
    "DEBUG": "jest"
  },
  "disableOptimisticBPs": true
}
Run Code Online (Sandbox Code Playgroud)