context.test([name][, options][, fn])


  • name <string> 子测试的名称,在报告测试结果时显示。默认值: fnname 属性,或者如果 fn 没有名称,则为 '<anonymous>'
  • options <Object> 子测试的配置选项。支持以下属性:
    • concurrency <number> | <boolean> | <null> 如果提供了一个数字,那么应用线程中将会并行运行相应数量的测试。如果是 true,则会并行运行所有子测试。如果是 false,则一次只运行一个测试。如果未指定,子测试将从其父测试继承此值。默认值: null
    • only <boolean> 如果为真,并且测试环境配置为运行 only 测试,则将运行此测试。否则,该测试将被跳过。默认值: false
    • signal <AbortSignal> 允许中止正在进行的测试。
    • skip <boolean> | <string> 如果为真,该测试将被跳过。如果提供了一个字符串,该字符串将显示在测试结果中,作为跳过测试的原因。默认值: false
    • todo <boolean> | <string> 如果为真,该测试将标记为 TODO。如果提供了字符串,该字符串将在测试结果中显示,作为测试为 TODO 的原因。默认: false
    • timeout <number> 测试将在若干毫秒后失败。如果未指定,子测试将继承其父测试的此值。默认值: Infinity
    • plan <number> 测试中预期运行的断言和子测试的数量。如果测试中运行的断言数量与计划中指定的数量不符,测试将失败。默认值: undefined
  • fn <Function> | <AsyncFunction> 被测试的函数。这个函数的第一个参数是一个 TestContext 对象。如果测试使用回调函数,回调函数作为第二个参数传入。默认: 一个空操作函数。
  • 返回:<Promise> 测试完成后,undefined 已完成。

此函数用于在当前测试下创建子测试。此函数的行为与顶层 test() 函数相同。

🌐 This function is used to create subtests under the current test. This function behaves in the same fashion as the top level test() function.

test('top level test', async (t) => {
  await t.test(
    'This is a subtest',
    { only: false, skip: false, concurrency: 1, todo: false, plan: 1 },
    (t) => {
      t.assert.ok('some relevant assertion here');
    },
  );
});