Skip to content

Commit 5a14f95

Browse files
committed
test_runner: move global setup watch mode test to single file
1 parent ad734a8 commit 5a14f95

File tree

2 files changed

+113
-102
lines changed

2 files changed

+113
-102
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
import '../common/index.mjs';
2+
import { beforeEach, describe, it } from 'node:test';
3+
import * as fixtures from '../common/fixtures.mjs';
4+
import { once } from 'node:events';
5+
import assert from 'node:assert';
6+
import { spawn } from 'node:child_process';
7+
import { writeFileSync, existsSync, readFileSync } from 'node:fs';
8+
import tmpdir from '../common/tmpdir.js';
9+
import { join } from 'node:path';
10+
11+
const testFixtures = fixtures.path('test-runner');
12+
13+
describe('test runner watch mode with global setup hooks', () => {
14+
beforeEach(() => {
15+
tmpdir.refresh();
16+
});
17+
18+
for (const isolation of ['none', 'process']) {
19+
it(`should run global setup/teardown hooks with each test run in watch mode (isolation: ${isolation})`, async () => {
20+
const testContent = `
21+
const test = require('node:test');
22+
23+
test('test with global hooks', (t) => {
24+
t.assert.ok('test passed');
25+
});
26+
`;
27+
28+
const globalSetupFileFixture = join(testFixtures, 'global-setup-teardown', 'basic-setup-teardown.mjs');
29+
const testFilePath = tmpdir.resolve('test-with-hooks.js');
30+
const setupFlagPath = tmpdir.resolve('setup-executed-watch.tmp');
31+
const teardownFlagPath = tmpdir.resolve('teardown-executed-watch.tmp');
32+
33+
writeFileSync(testFilePath, testContent);
34+
35+
const ran1 = Promise.withResolvers();
36+
const ran2 = Promise.withResolvers();
37+
38+
const child = spawn(process.execPath,
39+
[
40+
'--watch',
41+
'--test',
42+
'--test-reporter=spec',
43+
`--test-isolation=${isolation}`,
44+
'--test-global-setup=' + globalSetupFileFixture,
45+
testFilePath,
46+
],
47+
{
48+
encoding: 'utf8',
49+
stdio: 'pipe',
50+
cwd: tmpdir.path,
51+
env: {
52+
...process.env,
53+
SETUP_FLAG_PATH: setupFlagPath,
54+
TEARDOWN_FLAG_PATH: teardownFlagPath
55+
}
56+
});
57+
58+
let stdout = '';
59+
let currentRun = '';
60+
const runs = [];
61+
62+
child.stdout.on('data', (data) => {
63+
stdout += data.toString();
64+
currentRun += data.toString();
65+
const testRuns = stdout.match(/duration_ms\s\d+/g);
66+
if (testRuns?.length >= 1) ran1.resolve();
67+
if (testRuns?.length >= 2) ran2.resolve();
68+
});
69+
70+
await ran1.promise;
71+
runs.push(currentRun);
72+
currentRun = '';
73+
74+
assert.ok(existsSync(setupFlagPath), 'Setup flag file should exist');
75+
assert.ok(!existsSync(teardownFlagPath), 'Teardown flag file should not exist');
76+
77+
// Modify test file to trigger watch
78+
writeFileSync(testFilePath, testContent + `
79+
test('another test', (t) => {
80+
t.assert.ok('another test passed');
81+
});
82+
`);
83+
84+
await ran2.promise;
85+
runs.push(currentRun);
86+
87+
currentRun = '';
88+
child.kill();
89+
await once(child, 'exit');
90+
91+
92+
// Verify the teardown file was updated
93+
const secondTeardownContent = readFileSync(teardownFlagPath, 'utf8');
94+
assert.strictEqual(secondTeardownContent, 'Teardown was executed');
95+
96+
assert.match(runs[0], /Global setup executed/);
97+
assert.match(runs[0], /tests 1/);
98+
assert.match(runs[0], /pass 1/);
99+
assert.match(runs[0], /fail 0/);
100+
101+
assert.doesNotMatch(runs[1], /Global setup executed/);
102+
assert.doesNotMatch(runs[1], /Global teardown executed/);
103+
assert.match(runs[1], /tests 2/);
104+
assert.match(runs[1], /pass 2/);
105+
assert.match(runs[1], /fail 0/);
106+
107+
// Verify stdout after killing the child
108+
assert.match(currentRun, /Global teardown executed/);
109+
assert.match(currentRun, /Data from setup: data from setup/);
110+
});
111+
}
112+
});

test/parallel/test-runner-watch-mode.mjs

+1-102
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,18 @@
11
import * as common from '../common/index.mjs';
22
import { describe, it, beforeEach } from 'node:test';
3-
import * as fixtures from '../common/fixtures.mjs';
43
import { once } from 'node:events';
54
import assert from 'node:assert';
65
import { spawn } from 'node:child_process';
7-
import { writeFileSync, renameSync, unlinkSync, existsSync, readFileSync } from 'node:fs';
6+
import { writeFileSync, renameSync, unlinkSync } from 'node:fs';
87
import { setTimeout } from 'node:timers/promises';
98
import tmpdir from '../common/tmpdir.js';
10-
import { join } from 'node:path';
119

1210
if (common.isIBMi)
1311
common.skip('IBMi does not support `fs.watch()`');
1412

1513
if (common.isAIX)
1614
common.skip('folder watch capability is limited in AIX.');
1715

18-
const testFixtures = fixtures.path('test-runner');
19-
2016
let fixturePaths;
2117

2218
// This test updates these files repeatedly,
@@ -194,101 +190,4 @@ describe('test runner watch mode', () => {
194190
});
195191
});
196192
}
197-
198-
describe('test runner watch mode with global setup hooks', () => {
199-
for (const isolation of ['none', 'process']) {
200-
it(`should run global setup/teardown hooks with each test run in watch mode (isolation: ${isolation})`, async () => {
201-
const testContent = `
202-
const test = require('node:test');
203-
204-
test('test with global hooks', (t) => {
205-
t.assert.ok('test passed');
206-
});
207-
`;
208-
209-
const globalSetupFileFixture = join(testFixtures, 'global-setup-teardown', 'basic-setup-teardown.mjs');
210-
const testFilePath = tmpdir.resolve('test-with-hooks.js');
211-
const setupFlagPath = tmpdir.resolve('setup-executed-watch.tmp');
212-
const teardownFlagPath = tmpdir.resolve('teardown-executed-watch.tmp');
213-
214-
writeFileSync(testFilePath, testContent);
215-
216-
const ran1 = Promise.withResolvers();
217-
const ran2 = Promise.withResolvers();
218-
219-
const child = spawn(process.execPath,
220-
[
221-
'--watch',
222-
'--test',
223-
'--test-reporter=spec',
224-
`--test-isolation=${isolation}`,
225-
'--test-global-setup=' + globalSetupFileFixture,
226-
testFilePath,
227-
],
228-
{
229-
encoding: 'utf8',
230-
stdio: 'pipe',
231-
cwd: tmpdir.path,
232-
env: {
233-
...process.env,
234-
SETUP_FLAG_PATH: setupFlagPath,
235-
TEARDOWN_FLAG_PATH: teardownFlagPath
236-
}
237-
});
238-
239-
let stdout = '';
240-
let currentRun = '';
241-
const runs = [];
242-
243-
child.stdout.on('data', (data) => {
244-
stdout += data.toString();
245-
currentRun += data.toString();
246-
const testRuns = stdout.match(/duration_ms\s\d+/g);
247-
if (testRuns?.length >= 1) ran1.resolve();
248-
if (testRuns?.length >= 2) ran2.resolve();
249-
});
250-
251-
await ran1.promise;
252-
runs.push(currentRun);
253-
currentRun = '';
254-
255-
assert.ok(existsSync(setupFlagPath), 'Setup flag file should exist');
256-
assert.ok(!existsSync(teardownFlagPath), 'Teardown flag file should not exist');
257-
258-
// Modify test file to trigger watch
259-
writeFileSync(testFilePath, testContent + `
260-
test('another test', (t) => {
261-
t.assert.ok('another test passed');
262-
});
263-
`);
264-
265-
await ran2.promise;
266-
runs.push(currentRun);
267-
268-
currentRun = '';
269-
child.kill();
270-
await once(child, 'exit');
271-
272-
273-
// Verify the teardown file was updated
274-
const secondTeardownContent = readFileSync(teardownFlagPath, 'utf8');
275-
assert.strictEqual(secondTeardownContent, 'Teardown was executed');
276-
277-
assert.match(runs[0], /Global setup executed/);
278-
assert.match(runs[0], /tests 1/);
279-
assert.match(runs[0], /pass 1/);
280-
assert.match(runs[0], /fail 0/);
281-
282-
assert.doesNotMatch(runs[1], /Global setup executed/);
283-
assert.doesNotMatch(runs[1], /Global teardown executed/);
284-
assert.match(runs[1], /tests 2/);
285-
assert.match(runs[1], /pass 2/);
286-
assert.match(runs[1], /fail 0/);
287-
288-
// Verify stdout after killing the child
289-
assert.match(currentRun, /Global teardown executed/);
290-
assert.match(currentRun, /Data from setup: data from setup/);
291-
});
292-
}
293-
});
294193
});

0 commit comments

Comments
 (0)