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


  • name <string> 子测试的名称,在报告测试结果时显示。 默认值: The name fn 的属性,如果 fn 没有名称,则为 '<anonymous>'
  • options <Object> 子测试的配置选项。 支持以下属性:
    • concurrency <number> 可同时运行的测试数。 如果未指定,则子测试从其父测试继承此值。 默认值: 1
    • only <boolean> 如果为真,并且测试上下文配置为运行 only 测试,则将运行此测试。 否则跳过测试。 默认值: false
    • skip <boolean> | <string> 如果为真,则跳过测试。 如果提供了字符串,则该字符串将作为跳过测试的原因显示在测试结果中。 默认值: false
    • todo <boolean> | <string> 如果为真,则测试标记为 TODO。 如果提供了字符串,则该字符串会显示在测试结果中作为测试为 TODO 的原因。 默认值: false
  • fn <Function> | <AsyncFunction> 被测试的函数。 此函数的第一个参数是 TestContext 对象。 如果测试使用回调,则回调函数作为第二个参数传入。 默认值: 无操作的函数。
  • 返回: <Promise> 测试完成后使用 undefined 解决。

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

test('top level test', async (t) => {
  await t.test(
    'This is a subtest',
    { only: false, skip: false, concurrency: 1, todo: false },
    (t) => {
      assert.ok('some relevant assertion here');
    }
  );
});
  • name <string> The name of the subtest, which is displayed when reporting test results. Default: The name property of fn, or '<anonymous>' if fn does not have a name.
  • options <Object> Configuration options for the subtest. The following properties are supported:
    • concurrency <number> The number of tests that can be run at the same time. If unspecified, subtests inherit this value from their parent. Default: 1.
    • only <boolean> If truthy, and the test context is configured to run only tests, then this test will be run. Otherwise, the test is skipped. Default: false.
    • skip <boolean> | <string> If truthy, the test is skipped. If a string is provided, that string is displayed in the test results as the reason for skipping the test. Default: false.
    • todo <boolean> | <string> If truthy, the test marked as TODO. If a string is provided, that string is displayed in the test results as the reason why the test is TODO. Default: false.
  • fn <Function> | <AsyncFunction> The function under test. The first argument to this function is a TestContext object. If the test uses callbacks, the callback function is passed as the second argument. Default: A no-op function.
  • Returns: <Promise> Resolved with undefined once the test completes.

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 },
    (t) => {
      assert.ok('some relevant assertion here');
    }
  );
});