Skip to content

Commit dd4d0c6

Browse files
feat(test-runner): use require.resolve for globalSetup and globalTeardown (#7752) (#7761)
1 parent 53fcdb8 commit dd4d0c6

File tree

2 files changed

+42
-9
lines changed

2 files changed

+42
-9
lines changed

src/test/loader.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import { Suite } from './test';
2222
import { SerializedLoaderData } from './ipc';
2323
import * as path from 'path';
2424
import * as url from 'url';
25+
import * as fs from 'fs';
2526
import { ProjectImpl } from './project';
2627
import { Reporter } from '../../types/testReporter';
2728
import { LaunchConfig } from '../../types/test';
@@ -74,9 +75,9 @@ export class Loader {
7475

7576
// Resolve script hooks relative to the root dir.
7677
if (this._config.globalSetup)
77-
this._config.globalSetup = path.resolve(rootDir, this._config.globalSetup);
78+
this._config.globalSetup = resolveScript(this._config.globalSetup, rootDir);
7879
if (this._config.globalTeardown)
79-
this._config.globalTeardown = path.resolve(rootDir, this._config.globalTeardown);
80+
this._config.globalTeardown = resolveScript(this._config.globalTeardown, rootDir);
8081

8182
const configUse = mergeObjects(this._defaultConfig.use, this._config.use);
8283
this._config = mergeObjects(mergeObjects(this._defaultConfig, this._config), { use: configUse });
@@ -444,3 +445,10 @@ function resolveReporters(reporters: Config['reporter'], rootDir: string): Repor
444445
return [require.resolve(id, { paths: [ rootDir ] }), arg];
445446
});
446447
}
448+
449+
function resolveScript(id: string, rootDir: string) {
450+
const localPath = path.resolve(rootDir, id);
451+
if (fs.existsSync(localPath))
452+
return localPath;
453+
return require.resolve(id, { paths: [rootDir] });
454+
}

tests/playwright-test/global-setup.spec.ts

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ test('globalSetup and globalTeardown should work', async ({ runInlineTest }) =>
2121
'playwright.config.ts': `
2222
import * as path from 'path';
2323
module.exports = {
24-
globalSetup: './globalSetup.ts',
24+
globalSetup: './globalSetup',
2525
globalTeardown: path.join(__dirname, 'globalTeardown.ts'),
2626
};
2727
`,
@@ -54,7 +54,7 @@ test('globalTeardown runs after failures', async ({ runInlineTest }) => {
5454
import * as path from 'path';
5555
module.exports = {
5656
globalSetup: 'globalSetup.ts',
57-
globalTeardown: 'globalTeardown.ts',
57+
globalTeardown: './globalTeardown.ts',
5858
};
5959
`,
6060
'globalSetup.ts': `
@@ -85,7 +85,7 @@ test('globalTeardown does not run when globalSetup times out', async ({ runInlin
8585
'playwright.config.ts': `
8686
import * as path from 'path';
8787
module.exports = {
88-
globalSetup: 'globalSetup.ts',
88+
globalSetup: './globalSetup.ts',
8989
globalTeardown: 'globalTeardown.ts',
9090
globalTimeout: 1000,
9191
};
@@ -119,7 +119,7 @@ test('globalSetup should be run before requiring tests', async ({ runInlineTest
119119
'playwright.config.ts': `
120120
import * as path from 'path';
121121
module.exports = {
122-
globalSetup: 'globalSetup.ts',
122+
globalSetup: './globalSetup.ts',
123123
};
124124
`,
125125
'globalSetup.ts': `
@@ -143,7 +143,7 @@ test('globalSetup should work with sync function', async ({ runInlineTest }) =>
143143
'playwright.config.ts': `
144144
import * as path from 'path';
145145
module.exports = {
146-
globalSetup: 'globalSetup.ts',
146+
globalSetup: './globalSetup.ts',
147147
};
148148
`,
149149
'globalSetup.ts': `
@@ -167,7 +167,7 @@ test('globalSetup should throw when passed non-function', async ({ runInlineTest
167167
'playwright.config.ts': `
168168
import * as path from 'path';
169169
module.exports = {
170-
globalSetup: 'globalSetup.ts',
170+
globalSetup: './globalSetup.ts',
171171
};
172172
`,
173173
'globalSetup.ts': `
@@ -187,7 +187,7 @@ test('globalSetup should work with default export and run the returned fn', asyn
187187
'playwright.config.ts': `
188188
import * as path from 'path';
189189
module.exports = {
190-
globalSetup: 'globalSetup.ts',
190+
globalSetup: './globalSetup.ts',
191191
};
192192
`,
193193
'globalSetup.ts': `
@@ -212,3 +212,28 @@ test('globalSetup should work with default export and run the returned fn', asyn
212212
expect(output).toContain(`%%setup: 42`);
213213
expect(output).toContain(`%%teardown: 42`);
214214
});
215+
216+
test('globalSetup should allow requiring a package from node_modules', async ({ runInlineTest }) => {
217+
const { results } = await runInlineTest({
218+
'playwright.config.ts': `
219+
import * as path from 'path';
220+
module.exports = {
221+
globalSetup: 'my-global-setup'
222+
};
223+
`,
224+
'node_modules/my-global-setup/index.js': `
225+
module.exports = async () => {
226+
await new Promise(f => setTimeout(f, 100));
227+
global.value = 42;
228+
process.env.FOO = String(global.value);
229+
};
230+
`,
231+
'a.test.js': `
232+
const { test } = pwt;
233+
test('should work', async ({}, testInfo) => {
234+
expect(process.env.FOO).toBe('42');
235+
});
236+
`,
237+
});
238+
expect(results[0].status).toBe('passed');
239+
});

0 commit comments

Comments
 (0)