按名称过滤测试


¥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.