创作带标签的测试


🌐 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']
});

标签值必须是非空字符串。标签匹配时不区分大小写;规范形式是小写。在单个 tags 数组中,重复项会根据小写形式合并,保留首次出现的声明顺序。

🌐 Tag values must be non-empty strings. 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.

钩子(beforeafterbeforeEachafterEach)不会声明它们自己的标签。它们作为所属套件的一部分运行,该套件携带套件的标签。

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