在 substack/tape 中使用 "plan" 与 "end" 的目的是什么?

What is the purpose of using "plan" vs "end" in substack/tape?

substack 的磁带测试模块允许您使用plan 方法提前指定断言数量,然后它会自动为您调用end。为什么不直接将 end 放在测试的末尾?使用 planend 有什么区别?

The first example on the readme 显示了 plan 有效但 end 无效的情况 - 异步测试解决方案。在这种情况下,您并没有明确地说 什么时候 测试应该全部解决,您是说最终应该解决多少:

test('timing test', function (t) {
    t.plan(2);

    t.equal(typeof Date.now, 'function');
    var start = Date.now();

    setTimeout(function () {
        t.equal(Date.now() - start, 100);
    }, 100);
});

如果我们使用 end,编写此测试的直观方式如下:

test('timing test', function (t) {
    t.equal(typeof Date.now, 'function');
    var start = Date.now();

    setTimeout(function () {
        t.equal(Date.now() - start, 100);
    }, 100);

    t.end();
});

...但这将在第二个断言有机会 运行.

之前结束测试

您可以将其进一步推断为任何需要执行异步或动态代码的情况。