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


  • name <string> 测试的名称,报告测试结果时显示。默认值:fnname 属性,如果 fn 没有名称,则为 '<anonymous>'

    ¥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> 测试的配置选项。支持以下属性:

    ¥options <Object> Configuration options for the test. The following properties are supported:

    • concurrency <number> | <boolean> 如果提供了一个数字,那么许多测试将在应用线程中并行运行。如果为 true,则所有计划的异步测试在线程内同时运行。如果是 false,一次只运行一个测试。如果未指定,则子测试从其父测试继承此值。默认值:false

      ¥concurrency <number> | <boolean> If a number is provided, then that many tests would run in parallel within the application thread. If true, all scheduled asynchronous tests run concurrently within the thread. If false, only one test runs at a time. If unspecified, subtests inherit this value from their parent. Default: false.

    • only <boolean> 如果为真,并且测试上下文配置为运行 only 测试,则将运行此测试。否则跳过测试。默认值:false

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

    • signal <AbortSignal> 允许中止正在进行的测试。

      ¥signal <AbortSignal> Allows aborting an in-progress test.

    • skip <boolean> | <string> 如果为真,则跳过测试。如果提供了字符串,则该字符串将作为跳过测试的原因显示在测试结果中。默认值: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> 如果为真,则测试标记为 TODO。如果提供了字符串,则该字符串会显示在测试结果中作为测试为 TODO 的原因。默认值: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.

    • timeout <number> 测试失败的毫秒数。如果未指定,则子测试从其父测试继承此值。默认值:Infinity

      ¥timeout <number> A number of milliseconds the test will fail after. If unspecified, subtests inherit this value from their parent. Default: Infinity.

    • plan <number> 预期在测试中运行的断言和子测试的数量。如果测试中运行的断言数量与计划中指定的数量不匹配,则测试将失败。默认值:undefined

      ¥plan <number> The number of assertions and subtests expected to be run in the test. If the number of assertions run in the test does not match the number specified in the plan, the test will fail. Default: undefined.

  • fn <Function> | <AsyncFunction> 被测试的函数。此函数的第一个参数是 TestContext 对象。如果测试使用回调,则回调函数作为第二个参数传入。默认值:空操作函数。

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

  • 返回:<Promise> 一旦测试完成,或者如果测试在套件中运行,则立即满足 undefined

    ¥Returns: <Promise> Fulfilled with undefined once the test completes, or immediately if the test runs within a suite.

test() 函数是从 test 模块导入的值。每次调用此函数都会向 <TestsStream> 报告测试。

¥The test() function is the value imported from the test module. Each invocation of this function results in reporting the test to the <TestsStream>.

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

¥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 diagnostic information, or creating subtests.

test() 返回测试完成后满足的 Promise。如果 test() 在套件内被调用,它会立即执行。返回值通常可以被顶层测试丢弃。但是,应该使用子测试的返回值来防止父测试先完成并取消子测试,如下例所示。

¥test() returns a Promise that fulfills once the test completes. if test() is called within a suite, it fulfills immediately. 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);
    });
  });
}); 

如果完成时间超过 timeout 毫秒,则可以使用 timeout 选项使测试失败。但是,它不是取消测试的可靠机制,因为正在运行的测试可能会阻塞应用线程,从而阻止预定的取消。

¥The timeout option can be used to fail the test if it takes longer than timeout milliseconds to complete. However, it is not a reliable mechanism for canceling tests because a running test might block the application thread and thus prevent the scheduled cancellation.