Skip to content

Commit a32f885

Browse files
committed
test: prevent flaky behavior in test runner bail option
1 parent 524ffe8 commit a32f885

File tree

5 files changed

+48
-34
lines changed

5 files changed

+48
-34
lines changed

test/fixtures/test-runner/bailout/parallel-concurrency/first.mjs

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,23 @@
1+
import { existsSync } from 'node:fs';
12
import { describe, it } from 'node:test';
23

3-
describe('first parallel file', () => {
4+
const testSyncPath = process.env.__TEST_SYNC_PATH__;
45

5-
it('passing test with delay', async (t) => {
6-
await new Promise((resolve) => setTimeout(resolve, 200));
7-
t.assert.ok(true);
6+
describe('first parallel file', () => {
7+
it('awaits second test to start running before passing', async (t) => {
8+
// Wait for the second test to run and create the file
9+
await t.waitFor(
10+
() => {
11+
const exist = existsSync(`${testSyncPath}/second-file`);
12+
if (!exist) {
13+
throw new Error('Second file does not exist yet');
14+
}
15+
},
16+
{
17+
interval: 1000,
18+
timeout: 60_000,
19+
},
20+
);
821
});
922

1023
it('failing test', () => {
Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
1+
import { writeFileSync } from 'node:fs';
12
import { describe, it } from 'node:test';
3+
import { setTimeout } from 'node:timers/promises';
4+
5+
const testSyncPath = process.env.__TEST_SYNC_PATH__;
26

37
describe('second parallel file', () => {
4-
it('slow test that should be cancelled', async () => {
5-
await new Promise((resolve) => setTimeout(resolve, 5000));
8+
// Create a file to signal that the second test has started
9+
writeFileSync(`${testSyncPath}/second-file`, 'second file content');
10+
it('slow test that should be cancelled', async (t) => {
11+
while (true) {
12+
await setTimeout(5000);
13+
}
614
});
715
});

test/fixtures/test-runner/bailout/parallel-loading/infinite-loop.mjs

Lines changed: 0 additions & 10 deletions
This file was deleted.

test/fixtures/test-runner/bailout/parallel-loading/slow-loading.mjs

Lines changed: 0 additions & 12 deletions
This file was deleted.

test/parallel/test-runner-bail.js

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,16 @@
22

33
require('../common');
44
const fixtures = require('../common/fixtures');
5-
const { describe, it } = require('node:test');
5+
const { describe, it, beforeEach } = require('node:test');
66
const { spawnSync } = require('node:child_process');
77
const assert = require('node:assert');
8+
const tmpdir = require('../common/tmpdir.js');
89

910
describe('node:test bail', () => {
11+
beforeEach(() => {
12+
tmpdir.refresh();
13+
});
14+
1015
it('should run all tests when --test-bail is not set', () => {
1116
const child = spawnSync(
1217
process.execPath,
@@ -81,18 +86,23 @@ describe('node:test bail', () => {
8186
'--test-bail',
8287
'--test-reporter=spec',
8388
'--test-concurrency=2',
84-
fixtures.path('test-runner', 'bailout', 'parallel-loading', 'slow-loading.mjs'),
85-
fixtures.path('test-runner', 'bailout', 'parallel-loading', 'infinite-loop.mjs'),
89+
fixtures.path('test-runner', 'bailout', 'parallel-concurrency', 'first.mjs'),
90+
fixtures.path('test-runner', 'bailout', 'parallel-concurrency', 'second.mjs'),
8691
],
92+
{
93+
env: {
94+
__TEST_SYNC_PATH__: tmpdir.path,
95+
}
96+
}
8797
);
8898

8999
assert.strictEqual(child.stderr.toString(), '');
90100
const output = child.stdout.toString();
91101

92102
assert.match(output, /Bail out!/);
93-
assert.match(output, /tests 2/);
103+
assert.match(output, /tests 3/);
94104
assert.match(output, /suites 1/);
95-
assert.match(output, /pass 0/);
105+
assert.match(output, /pass 1/);
96106
assert.match(output, /fail 1/);
97107
assert.match(output, /cancelled 1/);
98108
});
@@ -108,7 +118,12 @@ describe('node:test bail', () => {
108118
fixtures.path('test-runner', 'bailout', 'parallel-concurrency', 'first.mjs'),
109119
fixtures.path('test-runner', 'bailout', 'parallel-concurrency', 'second.mjs'),
110120
fixtures.path('test-runner', 'bailout', 'parallel-concurrency', 'third.mjs'),
111-
]
121+
],
122+
{
123+
env: {
124+
__TEST_SYNC_PATH__: tmpdir.path,
125+
}
126+
}
112127
);
113128

114129
assert.strictEqual(child.stderr.toString(), '');

0 commit comments

Comments
 (0)