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 模块导入的值。 每次调用此函数都会在 TAP 输出中创建一个测试点。

传给 fn 参数的 TestContext 对象可用于执行与当前测试相关的操作。 示例包括跳过测试、添加额外的 TAP 诊断信息、或创建子测试。

test() 返回 Promise,一旦测试完成就解决。 返回值通常可以被顶层测试丢弃。 但是,应该使用子测试的返回值来防止父测试先完成并取消子测试,如下例所示。

test('top level test', async (t) => {
  // 如果在下一行删除了 'await',
  // 则以下子测试中的 setTimeout() 将导致它的父测试寿命超过其父测试。
  // 一旦父测试完成,则它将取消所有未完成的子测试。
  await t.test('longer running subtest', async (t) => {
    return new Promise((resolve, reject) => {
      setTimeout(resolve, 1000);
    });
  });
});
  • name <string> The name of the test, 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 test. 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.

The test() function is the value imported from the test module. Each invocation of this function results in the creation of a test point in the TAP output.

The TestContext object passed to the fn argument can be used to perform actions related to the current test. Examples include skipping the test, adding additional TAP diagnostic information, or creating subtests.

test() returns a Promise that resolves once the test completes. The return value can usually be discarded for top level tests. However, the return value from subtests should be used to prevent the parent test from finishing first and cancelling the subtest as shown in the following example.

test('top level test', async (t) => {
  // The setTimeout() in the following subtest would cause it to outlive its
  // parent test if 'await' is removed on the next line. Once the parent test
  // completes, it will cancel any outstanding subtests.
  await t.test('longer running subtest', async (t) => {
    return new Promise((resolve, reject) => {
      setTimeout(resolve, 1000);
    });
  });
});