我有一个 nodejs 项目。我正在使用调试器(如果重要的话在 IntelliJ 中)来逐步完成测试。我想使用最佳实践配置我的项目,以便在我使用调试器时将测试超时修改为比默认的 5 秒长。我知道我可以将以下行放在 package.json 中以更改超时。
{
"name": "my-project",
"jest": {
"testTimeout": 20000
}
}
但我正在寻找一种在调试时仅(自动)修改超时的方法。我确信有一个好方法,但我对 nodejs 和设置各种环境的约定/机制还很陌生。
最佳答案
如果使用 create-react-app
,setupFilesAfterEnv
配置为 src\setupTests.ts
。
这是执行/调试任何测试之前的入口点,如果您可以检测到您处于DEBUG 模式,那么您可以设置一个新的超时时间。
例如,
if (process.env.DEBUG === 'jest') {
jest.setTimeout(5 * 60 * 1000);
}
在 VS Code 中,您可以将 launch.json
配置添加为 documented将 "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
}
关于javascript - 调试测试时自动设置 Jest 超时的最佳方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69137737/