Skip to content

Commit 31c319d

Browse files
authored
Wait for pending ticks started by test (#995)
1 parent 9b16f2a commit 31c319d

File tree

3 files changed

+48
-0
lines changed

3 files changed

+48
-0
lines changed

lib/runner.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -662,6 +662,11 @@ internals.protect = function (item, state) {
662662

663663
try {
664664
await item.fn.call(null, flags);
665+
// wait for any pending ticks started by the test
666+
await new Promise((next) => {
667+
668+
setImmediate(next);
669+
});
665670
finish();
666671
}
667672
catch (ex) {

test/cli.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -889,4 +889,13 @@ describe('CLI', () => {
889889
expect(result.output).to.contain('1 of 2 tests failed');
890890
expect(result.output).to.contain('oops');
891891
});
892+
893+
it('awaits any pending ticks started by test before starting next test', async () => {
894+
895+
const { code, output } = await RunCli(['test/cli_nexttick/test.js']);
896+
897+
expect(code).to.equal(1);
898+
expect(output).to.contain('does not crash lab');
899+
expect(output).to.not.contain('has another test');
900+
});
892901
});

test/cli_nexttick/test.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
'use strict';
2+
3+
// Load modules
4+
5+
const Hoek = require('@hapi/hoek');
6+
const _Lab = require('../../test_runner');
7+
8+
9+
// Declare internals
10+
11+
const internals = {};
12+
13+
14+
// Test shortcuts
15+
16+
const lab = exports.lab = _Lab.script();
17+
const describe = lab.describe;
18+
const it = lab.it;
19+
20+
21+
describe('Test CLI', () => {
22+
23+
it('does not crash lab', async () => {
24+
25+
await Hoek.wait(1);
26+
27+
process.nextTick(() => {
28+
29+
throw new Error('fail');
30+
});
31+
});
32+
33+
it('has another test', async () => {});
34+
});

0 commit comments

Comments
 (0)