context.plan(count)


稳定性: 1 - 实验性

  • count <number> 预期要运行的断言和子测试的数量。

此函数用于设置测试中预期运行的断言和子测试的数量。如果实际运行的断言和子测试数量与预期数量不符,测试将会失败。

🌐 This function is used to set the number of assertions and subtests that are expected to run within the test. If the number of assertions and subtests that run does not match the expected count, the test will fail.

注意:为了确保断言被跟踪,必须使用 t.assert,而不能直接使用 assert

test('top level test', (t) => {
  t.plan(2);
  t.assert.ok('some relevant assertion here');
  t.test('subtest', () => {});
}); 

在处理异步代码时,可以使用 plan 函数来确保运行正确数量的断言:

🌐 When working with asynchronous code, the plan function can be used to ensure that the correct number of assertions are run:

test('planning with streams', (t, done) => {
  function* generate() {
    yield 'a';
    yield 'b';
    yield 'c';
  }
  const expected = ['a', 'b', 'c'];
  t.plan(expected.length);
  const stream = Readable.from(generate());
  stream.on('data', (chunk) => {
    t.assert.strictEqual(chunk, expected.shift());
  });

  stream.on('end', () => {
    done();
  });
});