Skip to content

Commit

Permalink
test: prevent flaky behavior in test runner bail option
Browse files Browse the repository at this point in the history
  • Loading branch information
pmarchini committed Jan 19, 2025
1 parent 524ffe8 commit 8833b0c
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 34 deletions.
21 changes: 17 additions & 4 deletions test/fixtures/test-runner/bailout/parallel-concurrency/first.mjs
Original file line number Diff line number Diff line change
@@ -1,10 +1,23 @@
import { existsSync } from 'node:fs';
import { describe, it } from 'node:test';

describe('first parallel file', () => {
const testSyncPath = process.env.__TEST_SYNC_PATH__;

it('passing test with delay', async (t) => {
await new Promise((resolve) => setTimeout(resolve, 200));
t.assert.ok(true);
describe('first parallel file', () => {
it('awaits second test to start running before passing', async (t) => {
// Wait for the second test to run and create the file
await t.waitFor(
() => {
const exist = existsSync(`${testSyncPath}/second-file`);
if (!exist) {
throw new Error('Second file does not exist yet');
}
},
{
interval: 1000,
timeout: 60_000,
},
);
});

it('failing test', () => {
Expand Down
12 changes: 10 additions & 2 deletions test/fixtures/test-runner/bailout/parallel-concurrency/second.mjs
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
import { writeFileSync } from 'node:fs';
import { describe, it } from 'node:test';
import { setTimeout } from 'node:timers/promises';

const testSyncPath = process.env.__TEST_SYNC_PATH__;

describe('second parallel file', () => {
it('slow test that should be cancelled', async () => {
await new Promise((resolve) => setTimeout(resolve, 5000));
// Create a file to signal that the second test has started
writeFileSync(`${testSyncPath}/second-file`, 'second file content');
it('slow test that should be cancelled', async (t) => {
while (true) {
await setTimeout(5000);
}
});
});

This file was deleted.

This file was deleted.

27 changes: 21 additions & 6 deletions test/parallel/test-runner-bail.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,16 @@

require('../common');
const fixtures = require('../common/fixtures');
const { describe, it } = require('node:test');
const { describe, it, beforeEach } = require('node:test');
const { spawnSync } = require('node:child_process');
const assert = require('node:assert');
const tmpdir = require('../common/tmpdir.js');

describe('node:test bail', () => {
beforeEach(() => {
tmpdir.refresh();
});

it('should run all tests when --test-bail is not set', () => {
const child = spawnSync(
process.execPath,
Expand Down Expand Up @@ -81,18 +86,23 @@ describe('node:test bail', () => {
'--test-bail',
'--test-reporter=spec',
'--test-concurrency=2',
fixtures.path('test-runner', 'bailout', 'parallel-loading', 'slow-loading.mjs'),
fixtures.path('test-runner', 'bailout', 'parallel-loading', 'infinite-loop.mjs'),
fixtures.path('test-runner', 'bailout', 'parallel-concurrency', 'first.mjs'),
fixtures.path('test-runner', 'bailout', 'parallel-concurrency', 'second.mjs'),
],
{
env: {
__TEST_SYNC_PATH__ : tmpdir.path,

Check failure on line 94 in test/parallel/test-runner-bail.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

Extra space after key '__TEST_SYNC_PATH__'
}
}
);

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

assert.match(output, /Bail out!/);
assert.match(output, /tests 2/);
assert.match(output, /tests 3/);
assert.match(output, /suites 1/);
assert.match(output, /pass 0/);
assert.match(output, /pass 1/);
assert.match(output, /fail 1/);
assert.match(output, /cancelled 1/);
});
Expand All @@ -108,7 +118,12 @@ describe('node:test bail', () => {
fixtures.path('test-runner', 'bailout', 'parallel-concurrency', 'first.mjs'),
fixtures.path('test-runner', 'bailout', 'parallel-concurrency', 'second.mjs'),
fixtures.path('test-runner', 'bailout', 'parallel-concurrency', 'third.mjs'),
]
],
{
env: {
__TEST_SYNC_PATH__ : tmpdir.path,

Check failure on line 124 in test/parallel/test-runner-bail.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

Extra space after key '__TEST_SYNC_PATH__'
}
}
);

assert.strictEqual(child.stderr.toString(), '');
Expand Down

0 comments on commit 8833b0c

Please sign in to comment.