Skip to content

Commit afd3b2f

Browse files
committed
test: add isolation option to global hooks configuration
1 parent c751fa3 commit afd3b2f

2 files changed

Lines changed: 149 additions & 129 deletions

File tree

test/fixtures/test-runner/test-runner-global-hooks.mjs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ const options = {
99
globalSetup: {
1010
type: 'string',
1111
},
12+
isolation: {
13+
type: 'string',
14+
},
1215
};
1316

1417
const {

test/parallel/test-runner-run-global-hooks.mjs

Lines changed: 146 additions & 129 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,12 @@ describe('require(\'node:test\').run with global hooks', { concurrency: false },
1616
tmpdir.refresh();
1717
});
1818

19-
async function runTestWithGlobalHooks({ globalSetupFile, testFile = 'test-file.js', runnerEnv = {} }) {
19+
async function runTestWithGlobalHooks({
20+
globalSetupFile,
21+
testFile = 'test-file.js',
22+
runnerEnv = {},
23+
isolation = 'process'
24+
}) {
2025
const testFilePath = path.join(testFixtures, testFile);
2126
const globalSetupPath = path.join(testFixtures, globalSetupFile);
2227

@@ -26,6 +31,7 @@ describe('require(\'node:test\').run with global hooks', { concurrency: false },
2631
runnerFixture,
2732
'--file', testFilePath,
2833
'--globalSetup', globalSetupPath,
34+
'--isolation', isolation,
2935
],
3036
{
3137
encoding: 'utf8',
@@ -52,157 +58,168 @@ describe('require(\'node:test\').run with global hooks', { concurrency: false },
5258
return { results };
5359
}
5460

55-
it('should run globalSetup and globalTeardown functions', async () => {
56-
const setupFlagPath = tmpdir.resolve('setup-executed.tmp');
57-
const teardownFlagPath = tmpdir.resolve('teardown-executed.tmp');
58-
59-
const { results } = await runTestWithGlobalHooks({
60-
globalSetupFile: 'basic-setup-teardown.js',
61-
runnerEnv: {
62-
SETUP_FLAG_PATH: setupFlagPath,
63-
TEARDOWN_FLAG_PATH: teardownFlagPath
64-
}
65-
});
66-
67-
assert.strictEqual(results.passed, 2);
68-
assert.strictEqual(results.failed, 0);
69-
// After all tests complete, the teardown should have run
70-
assert.ok(fs.existsSync(teardownFlagPath), 'Teardown flag file should exist');
71-
const content = fs.readFileSync(teardownFlagPath, 'utf8');
72-
assert.strictEqual(content, 'Teardown was executed');
73-
// Setup flag should have been removed by teardown
74-
assert.ok(!fs.existsSync(setupFlagPath), 'Setup flag file should have been removed');
75-
});
61+
for (const isolation of ['none', 'process']) {
62+
describe(`with isolation : ${isolation}`, () => {
63+
it('should run globalSetup and globalTeardown functions', async () => {
64+
const setupFlagPath = tmpdir.resolve('setup-executed.tmp');
65+
const teardownFlagPath = tmpdir.resolve('teardown-executed.tmp');
66+
67+
const { results } = await runTestWithGlobalHooks({
68+
globalSetupFile: 'basic-setup-teardown.js',
69+
runnerEnv: {
70+
SETUP_FLAG_PATH: setupFlagPath,
71+
TEARDOWN_FLAG_PATH: teardownFlagPath
72+
},
73+
isolation
74+
});
7675

77-
it('should run setup-only module', async () => {
78-
const setupOnlyFlagPath = tmpdir.resolve('setup-only-executed.tmp');
76+
assert.strictEqual(results.passed, 2);
77+
assert.strictEqual(results.failed, 0);
78+
// After all tests complete, the teardown should have run
79+
assert.ok(fs.existsSync(teardownFlagPath), 'Teardown flag file should exist');
80+
const content = fs.readFileSync(teardownFlagPath, 'utf8');
81+
assert.strictEqual(content, 'Teardown was executed');
82+
// Setup flag should have been removed by teardown
83+
assert.ok(!fs.existsSync(setupFlagPath), 'Setup flag file should have been removed');
84+
});
7985

80-
const { results } = await runTestWithGlobalHooks({
81-
globalSetupFile: 'setup-only.js',
82-
runnerEnv: {
83-
SETUP_ONLY_FLAG_PATH: setupOnlyFlagPath,
84-
SETUP_FLAG_PATH: setupOnlyFlagPath
85-
}
86-
});
86+
it('should run setup-only module', async () => {
87+
const setupOnlyFlagPath = tmpdir.resolve('setup-only-executed.tmp');
8788

88-
assert.strictEqual(results.passed, 1);
89-
assert.strictEqual(results.failed, 1);
90-
assert.ok(fs.existsSync(setupOnlyFlagPath), 'Setup-only flag file should exist');
91-
const content = fs.readFileSync(setupOnlyFlagPath, 'utf8');
92-
assert.strictEqual(content, 'Setup-only was executed');
93-
});
89+
const { results } = await runTestWithGlobalHooks({
90+
globalSetupFile: 'setup-only.js',
91+
runnerEnv: {
92+
SETUP_ONLY_FLAG_PATH: setupOnlyFlagPath,
93+
SETUP_FLAG_PATH: setupOnlyFlagPath
94+
},
95+
isolation
96+
});
9497

95-
it('should run teardown-only module', async () => {
96-
const teardownOnlyFlagPath = tmpdir.resolve('teardown-only-executed.tmp');
97-
const setupFlagPath = tmpdir.resolve('setup-for-teardown-only.tmp');
98+
assert.strictEqual(results.passed, 1);
99+
assert.strictEqual(results.failed, 1);
100+
assert.ok(fs.existsSync(setupOnlyFlagPath), 'Setup-only flag file should exist');
101+
const content = fs.readFileSync(setupOnlyFlagPath, 'utf8');
102+
assert.strictEqual(content, 'Setup-only was executed');
103+
});
98104

99-
// Create a setup file for test-file.js to find
100-
fs.writeFileSync(setupFlagPath, 'Setup was executed');
105+
it('should run teardown-only module', async () => {
106+
const teardownOnlyFlagPath = tmpdir.resolve('teardown-only-executed.tmp');
107+
const setupFlagPath = tmpdir.resolve('setup-for-teardown-only.tmp');
101108

102-
const { results } = await runTestWithGlobalHooks({
103-
globalSetupFile: 'teardown-only.js',
104-
runnerEnv: {
105-
TEARDOWN_ONLY_FLAG_PATH: teardownOnlyFlagPath,
106-
SETUP_FLAG_PATH: setupFlagPath
107-
}
108-
});
109+
// Create a setup file for test-file.js to find
110+
fs.writeFileSync(setupFlagPath, 'Setup was executed');
109111

110-
assert.strictEqual(results.passed, 2);
111-
assert.strictEqual(results.failed, 0);
112-
assert.ok(fs.existsSync(teardownOnlyFlagPath), 'Teardown-only flag file should exist');
113-
const content = fs.readFileSync(teardownOnlyFlagPath, 'utf8');
114-
assert.strictEqual(content, 'Teardown-only was executed');
115-
});
112+
const { results } = await runTestWithGlobalHooks({
113+
globalSetupFile: 'teardown-only.js',
114+
runnerEnv: {
115+
TEARDOWN_ONLY_FLAG_PATH: teardownOnlyFlagPath,
116+
SETUP_FLAG_PATH: setupFlagPath
117+
},
118+
isolation
119+
});
116120

117-
it('should share context between setup and teardown', async () => {
118-
const contextFlagPath = tmpdir.resolve('context-shared.tmp');
119-
const setupFlagPath = tmpdir.resolve('setup-for-context.tmp');
121+
assert.strictEqual(results.passed, 2);
122+
assert.strictEqual(results.failed, 0);
123+
assert.ok(fs.existsSync(teardownOnlyFlagPath), 'Teardown-only flag file should exist');
124+
const content = fs.readFileSync(teardownOnlyFlagPath, 'utf8');
125+
assert.strictEqual(content, 'Teardown-only was executed');
126+
});
120127

121-
// Create a setup file for test-file.js to find
122-
fs.writeFileSync(setupFlagPath, 'Setup was executed');
128+
it('should share context between setup and teardown', async () => {
129+
const contextFlagPath = tmpdir.resolve('context-shared.tmp');
130+
const setupFlagPath = tmpdir.resolve('setup-for-context.tmp');
123131

124-
await runTestWithGlobalHooks({
125-
globalSetupFile: 'context-sharing.js',
126-
runnerEnv: {
127-
CONTEXT_FLAG_PATH: contextFlagPath,
128-
SETUP_FLAG_PATH: setupFlagPath
129-
}
130-
});
132+
// Create a setup file for test-file.js to find
133+
fs.writeFileSync(setupFlagPath, 'Setup was executed');
131134

132-
assert.ok(fs.existsSync(contextFlagPath), 'Context sharing flag file should exist');
133-
const contextData = JSON.parse(fs.readFileSync(contextFlagPath, 'utf8'));
135+
await runTestWithGlobalHooks({
136+
globalSetupFile: 'context-sharing.js',
137+
runnerEnv: {
138+
CONTEXT_FLAG_PATH: contextFlagPath,
139+
SETUP_FLAG_PATH: setupFlagPath
140+
},
141+
isolation
142+
});
134143

135-
assert.strictEqual(typeof contextData.timestamp, 'number');
136-
assert.strictEqual(contextData.message, 'Hello from setup');
137-
assert.deepStrictEqual(contextData.complexData, { key: 'value', nested: { data: true } });
138-
});
144+
assert.ok(fs.existsSync(contextFlagPath), 'Context sharing flag file should exist');
145+
const contextData = JSON.parse(fs.readFileSync(contextFlagPath, 'utf8'));
139146

140-
it('should handle async setup and teardown', async () => {
141-
const asyncFlagPath = tmpdir.resolve('async-executed.tmp');
142-
const setupFlagPath = tmpdir.resolve('setup-for-async.tmp');
147+
assert.strictEqual(typeof contextData.timestamp, 'number');
148+
assert.strictEqual(contextData.message, 'Hello from setup');
149+
assert.deepStrictEqual(contextData.complexData, { key: 'value', nested: { data: true } });
150+
});
143151

144-
// Create a setup file for test-file.js to find
145-
fs.writeFileSync(setupFlagPath, 'Setup was executed');
152+
it('should handle async setup and teardown', async () => {
153+
const asyncFlagPath = tmpdir.resolve('async-executed.tmp');
154+
const setupFlagPath = tmpdir.resolve('setup-for-async.tmp');
146155

147-
const { results } = await runTestWithGlobalHooks({
148-
globalSetupFile: 'async-setup-teardown.js',
149-
runnerEnv: {
150-
ASYNC_FLAG_PATH: asyncFlagPath,
151-
SETUP_FLAG_PATH: setupFlagPath
152-
}
153-
});
156+
// Create a setup file for test-file.js to find
157+
fs.writeFileSync(setupFlagPath, 'Setup was executed');
154158

155-
assert.strictEqual(results.passed, 2);
156-
assert.strictEqual(results.failed, 0);
157-
assert.ok(fs.existsSync(asyncFlagPath), 'Async flag file should exist');
158-
const content = fs.readFileSync(asyncFlagPath, 'utf8');
159-
assert.strictEqual(content, 'Setup part, Teardown part');
160-
});
159+
const { results } = await runTestWithGlobalHooks({
160+
globalSetupFile: 'async-setup-teardown.js',
161+
runnerEnv: {
162+
ASYNC_FLAG_PATH: asyncFlagPath,
163+
SETUP_FLAG_PATH: setupFlagPath
164+
},
165+
isolation
166+
});
161167

162-
it('should run TypeScript globalSetup and globalTeardown functions', async () => {
163-
const setupFlagPath = tmpdir.resolve('setup-executed-ts.tmp');
164-
const teardownFlagPath = tmpdir.resolve('teardown-executed-ts.tmp');
168+
assert.strictEqual(results.passed, 2);
169+
assert.strictEqual(results.failed, 0);
170+
assert.ok(fs.existsSync(asyncFlagPath), 'Async flag file should exist');
171+
const content = fs.readFileSync(asyncFlagPath, 'utf8');
172+
assert.strictEqual(content, 'Setup part, Teardown part');
173+
});
165174

166-
const { results } = await runTestWithGlobalHooks({
167-
globalSetupFile: 'basic-setup-teardown.ts',
168-
runnerEnv: {
169-
SETUP_FLAG_PATH: setupFlagPath,
170-
TEARDOWN_FLAG_PATH: teardownFlagPath
171-
}
172-
});
175+
it('should run TypeScript globalSetup and globalTeardown functions', async () => {
176+
const setupFlagPath = tmpdir.resolve('setup-executed-ts.tmp');
177+
const teardownFlagPath = tmpdir.resolve('teardown-executed-ts.tmp');
178+
179+
const { results } = await runTestWithGlobalHooks({
180+
globalSetupFile: 'basic-setup-teardown.ts',
181+
runnerEnv: {
182+
SETUP_FLAG_PATH: setupFlagPath,
183+
TEARDOWN_FLAG_PATH: teardownFlagPath
184+
},
185+
isolation
186+
});
173187

174-
assert.strictEqual(results.passed, 2);
175-
assert.strictEqual(results.failed, 0);
176-
// After all tests complete, the teardown should have run
177-
assert.ok(fs.existsSync(teardownFlagPath), 'Teardown flag file should exist');
178-
const content = fs.readFileSync(teardownFlagPath, 'utf8');
179-
assert.strictEqual(content, 'Teardown was executed');
188+
assert.strictEqual(results.passed, 2);
189+
assert.strictEqual(results.failed, 0);
190+
// After all tests complete, the teardown should have run
191+
assert.ok(fs.existsSync(teardownFlagPath), 'Teardown flag file should exist');
192+
const content = fs.readFileSync(teardownFlagPath, 'utf8');
193+
assert.strictEqual(content, 'Teardown was executed');
180194

181-
// Setup flag should have been removed by teardown
182-
assert.ok(!fs.existsSync(setupFlagPath), 'Setup flag file should have been removed');
183-
});
195+
// Setup flag should have been removed by teardown
196+
assert.ok(!fs.existsSync(setupFlagPath), 'Setup flag file should have been removed');
197+
});
184198

185-
it('should run ESM globalSetup and globalTeardown functions', async () => {
186-
const setupFlagPath = tmpdir.resolve('setup-executed-esm.tmp');
187-
const teardownFlagPath = tmpdir.resolve('teardown-executed-esm.tmp');
199+
it('should run ESM globalSetup and globalTeardown functions', async () => {
200+
const setupFlagPath = tmpdir.resolve('setup-executed-esm.tmp');
201+
const teardownFlagPath = tmpdir.resolve('teardown-executed-esm.tmp');
202+
203+
const { results } = await runTestWithGlobalHooks({
204+
globalSetupFile: 'basic-setup-teardown.mjs',
205+
runnerEnv: {
206+
SETUP_FLAG_PATH: setupFlagPath,
207+
TEARDOWN_FLAG_PATH: teardownFlagPath
208+
},
209+
isolation
210+
});
188211

189-
const { results } = await runTestWithGlobalHooks({
190-
globalSetupFile: 'basic-setup-teardown.mjs',
191-
runnerEnv: {
192-
SETUP_FLAG_PATH: setupFlagPath,
193-
TEARDOWN_FLAG_PATH: teardownFlagPath
194-
}
212+
assert.strictEqual(results.passed, 2);
213+
assert.strictEqual(results.failed, 0);
214+
// After all tests complete, the teardown should have run
215+
assert.ok(fs.existsSync(teardownFlagPath), 'Teardown flag file should exist');
216+
const content = fs.readFileSync(teardownFlagPath, 'utf8');
217+
assert.strictEqual(content, 'Teardown was executed');
218+
// Setup flag should have been removed by teardown
219+
assert.ok(!fs.existsSync(setupFlagPath), 'Setup flag file should have been removed');
220+
});
195221
});
196-
197-
assert.strictEqual(results.passed, 2);
198-
assert.strictEqual(results.failed, 0);
199-
// After all tests complete, the teardown should have run
200-
assert.ok(fs.existsSync(teardownFlagPath), 'Teardown flag file should exist');
201-
const content = fs.readFileSync(teardownFlagPath, 'utf8');
202-
assert.strictEqual(content, 'Teardown was executed');
203-
// Setup flag should have been removed by teardown
204-
assert.ok(!fs.existsSync(setupFlagPath), 'Setup flag file should have been removed');
205-
});
222+
}
206223

207224
it('should validate that globalSetupPath is a string', () => {
208225
[123, {}, [], true, false].forEach((invalidValue) => {

0 commit comments

Comments
 (0)