重新运行失败的测试


¥Rerunning failed tests

测试运行器支持将运行状态保存到文件中,从而允许测试运行器重新运行失败的测试,而无需重新运行整个测试套件。使用 --test-rerun-failures 命令行选项指定存储运行状态的文件路径。如果状态文件不存在,测试运行器将创建该文件。状态文件是一个 JSON 文件,其中包含运行尝试的数组。每次运行尝试都是一个对象,将成功的测试映射到它们传入的尝试。此映射中用于标识测试的键是测试文件路径,其中包含定义测试的行和列。如果在特定位置定义的测试多次运行(例如在函数或循环中),则会在键后附加一个计数器,以消除测试运行的歧义。请注意,更改测试执行的顺序或测试的位置可能会导致测试运行器将测试视为在上次尝试中已通过,这意味着当测试以确定顺序运行时应使用 --test-rerun-failures

¥The test runner supports persisting the state of the run to a file, allowing the test runner to rerun failed tests without having to re-run the entire test suite. Use the --test-rerun-failures command-line option to specify a file path where the state of the run is stored. if the state file does not exist, the test runner will create it. the state file is a JSON file that contains an array of run attempts. Each run attempt is an object mapping successful tests to the attempt they have passed in. The key identifying a test in this map is the test file path, with the line and column where the test is defined. in a case where a test defined in a specific location is run multiple times, for example within a function or a loop, a counter will be appended to the key, to disambiguate the test runs. note changing the order of test execution or the location of a test can lead the test runner to consider tests as passed on a previous attempt, meaning --test-rerun-failures should be used when tests run in a deterministic order.

状态文件示例:

¥example of a state file:

[
  {
    "test.js:10:5": { "passed_on_attempt": 0, "name": "test 1" },
  },
  {
    "test.js:10:5": { "passed_on_attempt": 0, "name": "test 1" },
    "test.js:20:5": { "passed_on_attempt": 1, "name": "test 2" }
  }
] 

在此示例中,有两次运行尝试,在 test.js 中定义了两个测试,第一个测试在第一次尝试时成功,第二个测试在第二次尝试时也成功。

¥in this example, there are two run attempts, with two tests defined in test.js, the first test succeeded on the first attempt, and the second test succeeded on the second attempt.

使用 --test-rerun-failures 选项时,测试运行器将仅运行尚未通过的测试。

¥When the --test-rerun-failures option is used, the test runner will only run tests that have not yet passed.

node --test-rerun-failures /path/to/state/file