无关的异步活动
【Extraneous asynchronous activity】
测试函数执行完成后,结果会迅速报告 尽可能保持测试顺序。不过,这也是可能的 测试函数产生异步活动并存续于测试 就是它自己。测试跑者负责处理此类活动,但不会延迟 报告检测结果以配合该情况。
【Once a test function finishes executing, the results are reported 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() 尝试创建一个新的子测试。由于父测试已经完成并输出了其结果,新子测试会立即被标记为失败,并随后报告给 <TestsStream>。
【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 later
to the <TestsStream>.】
第二个 setImmediate() 会触发 uncaughtException 事件。来自已完成测试的 uncaughtException 和 unhandledRejection 事件会被 test 模块标记为失败,并由 <TestsStream> 在顶层作为诊断警告报告。
【The second setImmediate() creates an uncaughtException event.
uncaughtException and unhandledRejection events originating from a completed
test are marked as failed by the test module and reported as diagnostic
warnings at the top level by the <TestsStream>.】
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.
});