无关的异步活动


【Extraneous asynchronous activity】

一旦测试函数执行完成,TAP 结果会尽快输出,同时保持测试顺序。然而,测试函数可能会产生超出测试自身生命周期的异步活动。测试运行器会处理这种类型的活动,但不会为了配合这些活动而延迟测试结果的报告。

【Once a test function finishes executing, the TAP results are output as quickly as possible while maintaining the order of the tests. However, it is possible for the test function to generate asynchronous activity that outlives the test itself. The test runner handles this type of activity, but does not delay the reporting of test results in order to accommodate it.】

在以下示例中,一个测试在还有两个 setImmediate() 操作未完成的情况下就完成了。第一个 setImmediate() 尝试创建一个新的子测试。由于父测试已经完成并输出了结果,新子测试会立即被标记为失败,并在文件的顶层 TAP 输出中报告。

【In the following example, a test completes with two setImmediate() operations still outstanding. The first setImmediate() attempts to create a new subtest. Because the parent test has already finished and output its results, the new subtest is immediately marked as failed, and reported in the top level of the file's TAP output.】

第二个 setImmediate() 会触发 uncaughtException 事件。来自已完成测试的 uncaughtExceptionunhandledRejection 事件由 test 模块处理,并在文件 TAP 输出的顶层作为诊断警告报告。

【The second setImmediate() creates an uncaughtException event. uncaughtException and unhandledRejection events originating from a completed test are handled by the test module and reported as diagnostic warnings in the top level of the file's TAP output.】

test('a test that creates asynchronous activity', (t) => {
  setImmediate(() => {
    t.test('subtest that is created too late', (t) => {
      throw new Error('error1');
    });
  });

  setImmediate(() => {
    throw new Error('error2');
  });

  // The test finishes after this line.
});