What is the purpose of using the โ€œplanโ€ versus the โ€œendโ€ in the stand / tape?

The test feed testing module allows you to specify the number of statements ahead of time using the plan method, after which it will automatically call end for you. Why not just put end at the end of the test? What is the difference between using plan and end ?

+5
source share
1 answer

The first example in readme shows the situation when plan works, but end will not - asynchronous resolution of the test. In this case, you do not explicitly say when all the tests have been solved, you say how many of them should ultimately solve:

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

If we used end , the intuitive way to write this test would be as follows:

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

... but that would finish the test before the second statement got a chance to run.

You can extrapolate this further to any situation where you need to execute asynchronous or dynamic code.

+4
source

Source: https://habr.com/ru/post/1234485/


All Articles