重新运行失败的测试


【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