创作带标签的测试
🌐 Authoring tagged tests
在 test()、it()、suite() 或 describe() 中的任何一个上传递一个 tags 数组。标签通过并集从套件继承到其子测试——套件中带有 ['db'] 标签的测试如果声明了自己的 tags: ['integration'],则实际上具有两个标签。
🌐 Pass a tags array on any of test(), it(), suite(), or describe().
Tags inherit from a suite to its child tests by union—a test inside a
suite tagged ['db'] that declares its own tags: ['integration']
effectively has both tags.
import { describe, it } from 'node:test';
describe('database', { tags: ['db'] }, () => {
it('reads a row'); // tags: ['db']
it('writes a row', { tags: ['integration'] }); // tags: ['db', 'integration']
it('reconnects after disconnect', { tags: ['flaky'] }); // tags: ['db', 'flaky']
});const { describe, it } = require('node:test');
describe('database', { tags: ['db'] }, () => {
it('reads a row'); // tags: ['db']
it('writes a row', { tags: ['integration'] }); // tags: ['db', 'integration']
it('reconnects after disconnect', { tags: ['flaky'] }); // tags: ['db', 'flaky']
});标签值必须是不为空的字符串,不能包含空格、运算符字符(& | ! ( ) *),也不能是保留字 'and'、'or' 或 'not'(不区分大小写)。标签匹配时不区分大小写;标准形式是小写。在同一个 tags 数组中重复的标签会根据小写形式合并,保留第一次出现的顺序。
🌐 Tag values must be non-empty strings that contain no whitespace, no
operator characters (& | ! ( ) *), and are not the reserved words
'and', 'or', or 'not' in any casing. Tags are matched
case-insensitively; the canonical form is lowercase. Duplicates within a
single tags array are collapsed on the lowercased form, preserving the
first-seen declaration order.
钩子(before、after、beforeEach、afterEach)不会声明它们自己的标签。它们作为所属套件的一部分运行,该套件携带套件的标签。
🌐 Hooks (before, after, beforeEach, afterEach) do not declare their
own tags. They run as part of their owning suite, which carries the
suite's tags.