Skip to content

Commit

Permalink
test: use sync file in test-runner-output
Browse files Browse the repository at this point in the history
  • Loading branch information
pmarchini committed Jan 19, 2025
1 parent a32f885 commit 4e93338
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 25 deletions.
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
import { writeFile } from 'node:fs/promises';
import { describe, it } from 'node:test';
import { setTimeout } from 'node:timers/promises';

const testSyncPath = process.env.__TEST_SYNC_PATH__;
const testSyncFile = process.env.__TEST_SYNC_FILE__;

describe('long-running-test', async () => {
it('first', async (t) => {
t.assert.ok(true, 'First test passed');
});
// Create a file to signal that the second test has started
writeFile(`${testSyncPath}/${testSyncFile}`, 'file content');

it('second', async () => {
while (true) {
// a long running test
await new Promise((resolve) => setTimeout(resolve, 2500));
await setTimeout(5000);
}
throw new Error('Second test should not run');
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,24 @@
import { describe, it, before } from 'node:test';
import { existsSync } from 'node:fs';
import { describe, it } from 'node:test';

const testSyncPath = process.env.__TEST_SYNC_PATH__;
const testSyncFile = process.env.__TEST_SYNC_FILE__;

describe('test-with-setup', { concurrency: false }, () => {
before(async () => {
// Give some time to the concurrent test to start
await new Promise((resolve) => setTimeout(resolve, 2500));
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}/${testSyncFile}`);
if (!exist) {
throw new Error('Second file does not exist yet');
}
},
{
interval: 1000,
timeout: 60_000,
},
);
});

it('first', async () => {
Expand Down
14 changes: 10 additions & 4 deletions test/fixtures/test-runner/output/bail-concurrent-spec.snapshot
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
▶ long-running-test
✔ first (*ms)
▶ long-running-test.test.mjs
▶ test-with-setup
✔ awaits second test to start running before passing (*ms)
✖ first (*ms)
Error: First test failed
*
*
*
*
*
*
Expand All @@ -11,11 +14,11 @@
*

✖ Bail out!
ℹ tests 3
ℹ tests 4
ℹ suites 1
ℹ pass 1
ℹ fail 1
ℹ cancelled 1
ℹ cancelled 2
ℹ skipped 0
ℹ todo 0
ℹ duration_ms *
Expand All @@ -25,6 +28,9 @@
*
✖ first (*ms)
Error: First test failed
*
*
*
*
*
*
Expand Down
21 changes: 12 additions & 9 deletions test/fixtures/test-runner/output/bail-concurrent-tap.snapshot
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
TAP version 13
# Subtest: long-running-test
# Subtest: first
ok 1 - first
# Subtest: long-running-test.test.mjs
# Subtest: test-with-setup
# Subtest: awaits second test to start running before passing
ok 1 - awaits second test to start running before passing
---
duration_ms: *
type: 'test'
...
# Subtest: test-with-setup
# Subtest: first
not ok 1 - first
not ok 2 - first
---
duration_ms: *
type: 'test'
Expand All @@ -17,6 +17,9 @@ TAP version 13
error: 'First test failed'
code: 'ERR_TEST_FAILURE'
stack: |-
*
*
*
*
*
*
Expand All @@ -25,14 +28,14 @@ TAP version 13
*
...
# Subtest: second
1..2
1..3
Bail out!
1..1
# tests 3
1..2
# tests 4
# suites 1
# pass 1
# fail 1
# cancelled 1
# cancelled 2
# skipped 0
# todo 0
# duration_ms *
20 changes: 16 additions & 4 deletions test/parallel/test-runner-output.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import { describe, it } from 'node:test';
import { hostname } from 'node:os';
import { chdir, cwd } from 'node:process';
import { fileURLToPath } from 'node:url';
import tmpdir from '../common/tmpdir.js';

tmpdir.refresh();

const skipForceColors =
process.config.variables.icu_gyp_path !== 'tools/icu/icu-generic.gyp' ||
Expand Down Expand Up @@ -298,20 +301,29 @@ const tests = [
{
name: 'test-runner/output/bail-concurrent-spec.mjs',
transform: specTransform,
env: {
__TEST_SYNC_PATH__: tmpdir.path,
__TEST_SYNC_FILE__: 'bail-concurrent-spec'
},
},
{
name: 'test-runner/output/bail-sequential-spec.js',
transform: specTransform,
},
{ name: 'test-runner/output/bail-concurrent-tap.mjs' },

{
name: 'test-runner/output/bail-concurrent-tap.mjs',
env: {
__TEST_SYNC_PATH__: tmpdir.path,
__TEST_SYNC_FILE__: 'bail-concurrent-tap'
},
},
{ name: 'test-runner/output/bail-sequential-tap.js' },
]
.filter(Boolean)
.map(({ flags, name, tty, transform }) => ({
.map(({ flags, name, tty, transform, env }) => ({
name,
fn: common.mustCall(async () => {
await snapshot.spawnAndAssert(fixtures.path(name), transform ?? defaultTransform, { tty, flags });
await snapshot.spawnAndAssert(fixtures.path(name), transform ?? defaultTransform, { tty, flags, env });
}),
}));

Expand Down

0 comments on commit 4e93338

Please sign in to comment.