【问题标题】:What is the best way to automatically set jest timeout when debugging tests?调试测试时自动设置开玩笑超时的最佳方法是什么?
【发布时间】:2022-02-17 21:03:37
【问题描述】:

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

{
  "name": "my-project",
  "jest": {
    "testTimeout": 20000
  }
}

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

【问题讨论】:

  • 如何。如果您的意思是单个测试笔记 test/它需要第三个超时参数:jestjs.io/docs/api#testname-fn-timeout
  • 但是我希望正常运行测试与调试时的超时时间不同。这是必要的,因为断点会任意减慢测试执行速度。
  • 你可以设置一个 env var 或其他东西并在你的配置中使用它(如果它是 JS 而不是 JSON)但是在 Jest 中没有具体的处理。

标签: javascript typescript jestjs


【解决方案1】:

如果使用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
}

【讨论】:

    猜你喜欢
    • 2022-10-21
    • 2018-07-02
    • 2020-09-03
    • 1970-01-01
    • 2023-03-15
    • 1970-01-01
    • 2019-09-13
    • 2021-01-10
    • 1970-01-01
    相关资源
    最近更新 更多