context.plan(count)


稳定性: 1 - 实验性的

¥Stability: 1 - Experimental

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

    ¥count <number> The number of assertions and subtests that are expected to run.

此函数用于设置预期在测试中运行的断言和子测试的数量。如果运行的断言和子测试的数量与预期计数不匹配,则测试将失败。

¥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

¥Note: To make sure assertions are tracked, t.assert must be used instead of assert directly.

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();
  });
});