按名称过滤测试


🌐 Filtering tests by name

--test-name-pattern 命令行选项可用于仅运行名称与提供的模式匹配的测试。测试名称模式会被解释为 JavaScript 正则表达式。--test-name-pattern 选项可以多次指定以运行嵌套测试。对于每个执行的测试,任何相应的测试钩子,例如 beforeEach(),也会被执行。

🌐 The --test-name-pattern command-line option can be used to only run tests whose name matches the provided pattern. Test name patterns are interpreted as JavaScript regular expressions. The --test-name-pattern option can be specified multiple times in order to run nested tests. For each test that is executed, any corresponding test hooks, such as beforeEach(), are also run.

给定以下测试文件,使用 --test-name-pattern="test [1-3]" 选项启动 Node.js 会导致测试运行器执行 test 1test 2test 3。如果 test 1 不符合测试名称模式,则其子测试将不会执行,即使它们匹配该模式。同一组测试也可以通过多次传递 --test-name-pattern 来执行(例如 --test-name-pattern="test 1"--test-name-pattern="test 2" 等)。

🌐 Given the following test file, starting Node.js with the --test-name-pattern="test [1-3]" option would cause the test runner to execute test 1, test 2, and test 3. If test 1 did not match the test name pattern, then its subtests would not execute, despite matching the pattern. The same set of tests could also be executed by passing --test-name-pattern multiple times (e.g. --test-name-pattern="test 1", --test-name-pattern="test 2", etc.).

test('test 1', async (t) => {
  await t.test('test 2');
  await t.test('test 3');
});

test('Test 4', async (t) => {
  await t.test('Test 5');
  await t.test('test 6');
}); 

测试名称模式也可以使用正则表达式字面量来指定。这允许使用正则表达式标志。在前面的示例中,使用 --test-name-pattern="/test [4-5]/i" 启动 Node.js 将匹配 Test 4Test 5,因为该模式不区分大小写。

🌐 Test name patterns can also be specified using regular expression literals. This allows regular expression flags to be used. In the previous example, starting Node.js with --test-name-pattern="/test [4-5]/i" would match Test 4 and Test 5 because the pattern is case-insensitive.

测试名称模式不会更改测试运行程序执行的文件集。

🌐 Test name patterns do not change the set of files that the test runner executes.