按名称过滤测试


¥Filtering tests by name

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

¥The --test-name-pattern command-line option can be used to only run tests whose name matches the provided pattern, and the --test-skip-pattern option can be used to skip tests whose name matches the provided pattern. Test name patterns are interpreted as JavaScript regular expressions. The --test-name-pattern and --test-skip-pattern options 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. Tests that are not executed are omitted from the test runner output.

给定以下测试文件,使用 --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"(或 --test-skip-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" (or --test-skip-pattern="/test [4-5]/i") would match Test 4 and Test 5 because the pattern is case-insensitive.

要将单个测试与模式匹配,你可以使用其所有祖级测试名称作为前缀,并用空格分隔,以确保它是唯一的。例如,给定以下测试文件:

¥To match a single test with a pattern, you can prefix it with all its ancestor test names separated by space, to ensure it is unique. For example, given the following test file:

describe('test 1', (t) => {
  it('some test');
});

describe('test 2', (t) => {
  it('some test');
}); 

使用 --test-name-pattern="test 1 some test" 启动 Node.js 将仅匹配 test 1 中的 some test

¥Starting Node.js with --test-name-pattern="test 1 some test" would match only some test in test 1.

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

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

如果同时提供了 --test-name-pattern--test-skip-pattern,则测试必须满足这两个要求才能执行。

¥If both --test-name-pattern and --test-skip-pattern are supplied, tests must satisfy both requirements in order to be executed.