Skip to content

Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.

feat(test runner): return new env from `runGlobalSetup()` (#38351)

Dmitry Gozman50d994dafe5d
diff --git a/packages/playwright/src/isomorphic/testServerInterface.ts b/packages/playwright/src/isomorphic/testServerInterface.ts
index fcc34bb0c9..8cc611790d 100644
--- a/packages/playwright/src/isomorphic/testServerInterface.ts
+++ b/packages/playwright/src/isomorphic/testServerInterface.ts
@@ -47,6 +47,7 @@ export interface TestServerInterface {
runGlobalSetup(params: {}): Promise<{
report: ReportEntry[],
+ env: [string, string | null][],
status: reporterTypes.FullResult['status']
}>;
diff --git a/packages/playwright/src/runner/testRunner.ts b/packages/playwright/src/runner/testRunner.ts
index ec2397e31f..b7749748b5 100644
--- a/packages/playwright/src/runner/testRunner.ts
+++ b/packages/playwright/src/runner/testRunner.ts
@@ -100,6 +100,7 @@ export class TestRunner extends EventEmitter<TestRunnerEventMap> {
private _plugins: TestRunnerPluginRegistration[] | undefined;
private _watchTestDirs = false;
private _populateDependenciesOnList = false;
+ private _startingEnv: NodeJS.ProcessEnv = {};
constructor(configLocation: ConfigLocation, configCLIOverrides: ConfigCLIOverrides) {
super();
@@ -119,6 +120,7 @@ export class TestRunner extends EventEmitter<TestRunnerEventMap> {
setPlaywrightTestProcessEnv();
this._watchTestDirs = !!params.watchTestDirs;
this._populateDependenciesOnList = !!params.populateDependenciesOnList;
+ this._startingEnv = { ...process.env };
}
resizeTerminal(params: { cols: number, rows: number }) {
@@ -153,22 +155,29 @@ export class TestRunner extends EventEmitter<TestRunnerEventMap> {
throw new Error('Failed to load config: ' + (error ? error.message : 'Unknown error'));
}
- async runGlobalSetup(userReporters: AnyReporter[]): Promise<{ status: FullResultStatus }> {
+ async runGlobalSetup(userReporters: AnyReporter[]): Promise<{ status: FullResultStatus, env: [string, string | null][] }> {
await this.runGlobalTeardown();
const reporter = new InternalReporter(userReporters);
const config = await this._loadConfigOrReportError(reporter, this._configCLIOverrides);
if (!config)
- return { status: 'failed' };
+ return { status: 'failed', env: [] };
const { status, cleanup } = await runTasksDeferCleanup(new TestRun(config, reporter), [
...createGlobalSetupTasks(config),
]);
+
+ const env: [string, string | null][] = [];
+ for (const key of new Set([...Object.keys(process.env), ...Object.keys(this._startingEnv)])) {
+ if (this._startingEnv[key] !== process.env[key])
+ env.push([key, process.env[key] ?? null]);
+ }
+
if (status !== 'passed')
await cleanup();
else
this._globalSetup = { cleanup };
- return { status };
+ return { status, env };
}
async runGlobalTeardown() {
diff --git a/packages/playwright/src/runner/testServer.ts b/packages/playwright/src/runner/testServer.ts
index c1fd0693ab..627e4f7a25 100644
--- a/packages/playwright/src/runner/testServer.ts
+++ b/packages/playwright/src/runner/testServer.ts
@@ -157,8 +157,8 @@ export class TestServerDispatcher implements TestServerInterface {
async runGlobalSetup(params: Parameters<TestServerInterface['runGlobalSetup']>[0]): ReturnType<TestServerInterface['runGlobalSetup']> {
const { reporter, report } = await this._collectingReporter();
this._globalSetupReport = report;
- const { status } = await this._testRunner.runGlobalSetup([reporter, new ListReporter()]);
- return { report, status };
+ const { status, env } = await this._testRunner.runGlobalSetup([reporter, new ListReporter()]);
+ return { report, status, env };
}
async runGlobalTeardown() {
diff --git a/tests/playwright-test/playwright-test-fixtures.ts b/tests/playwright-test/playwright-test-fixtures.ts
index 641b0f18c2..40cab3922f 100644
--- a/tests/playwright-test/playwright-test-fixtures.ts
+++ b/tests/playwright-test/playwright-test-fixtures.ts
@@ -253,7 +253,7 @@ type Fixtures = {
deleteFile: (file: string) => Promise<void>;
runInlineTest: (files: Files, params?: Params, env?: NodeJS.ProcessEnv, options?: RunOptions) => Promise<RunResult>;
runCLICommand: (files: Files, command: string, args?: string[]) => Promise<{ stdout: string, stderr: string, exitCode: number }>;
- startCLICommand: (files: Files, command: string, args?: string[], options?: RunOptions) => Promise<TestChildProcess>;
+ startCLICommand: (files: Files, command: string, args?: string[], options?: RunOptions, env?: NodeJS.ProcessEnv) => Promise<TestChildProcess>;
runWatchTest: (files: Files, params?: Params, env?: NodeJS.ProcessEnv, options?: RunOptions) => Promise<TestChildProcess>;
interactWithTestRunner: (files: Files, params?: Params, env?: NodeJS.ProcessEnv, options?: RunOptions) => Promise<TestChildProcess>;
runTSC: (files: Files) => Promise<TSCResult>;
@@ -297,9 +297,9 @@ export const test = base
startCLICommand: async ({ childProcess }, use, testInfo: TestInfo) => {
const cacheDir = await fs.promises.mkdtemp(path.join(os.tmpdir(), 'playwright-test-cache-'));
- await use(async (files: Files, command: string, args?: string[], options: RunOptions = {}) => {
+ await use(async (files: Files, command: string, args?: string[], options: RunOptions = {}, env: NodeJS.ProcessEnv = {}) => {
const baseDir = await writeFiles(testInfo, files, true);
- return startPlaywrightChildProcess(childProcess, baseDir, [command, ...(args || [])], { PWTEST_CACHE_DIR: cacheDir }, options);
+ return startPlaywrightChildProcess(childProcess, baseDir, [command, ...(args || [])], { ...env, PWTEST_CACHE_DIR: cacheDir }, options);
});
await removeFolders([cacheDir]);
},
diff --git a/tests/playwright-test/test-server.spec.ts b/tests/playwright-test/test-server.spec.ts
index a83ca3910a..e0ecb51f0b 100644
--- a/tests/playwright-test/test-server.spec.ts
+++ b/tests/playwright-test/test-server.spec.ts
@@ -64,11 +64,11 @@ class TestServerConnectionUnderTest extends TestServerConnection {
}
}
-const test = baseTest.extend<{ startTestServer: () => Promise<TestServerConnectionUnderTest> }>({
+const test = baseTest.extend<{ startTestServer: (options?: { env?: NodeJS.ProcessEnv }) => Promise<TestServerConnectionUnderTest> }>({
startTestServer: async ({ startCLICommand }, use, testInfo) => {
let testServerProcess: TestChildProcess | undefined;
- await use(async () => {
- testServerProcess = await startCLICommand({}, 'test-server');
+ await use(async options => {
+ testServerProcess = await startCLICommand({}, 'test-server', [], {}, options?.env);
await testServerProcess.waitForOutput('Listening on');
const line = testServerProcess.output.split('\n').find(l => l.includes('Listening on'));
const wsEndpoint = line!.split(' ')[2];
@@ -328,3 +328,37 @@ test('pauseOnError no errors', async ({ startTestServer, writeFiles }) => {
expect(await testServerConnection.runTests({ pauseOnError: true, locations: [] })).toEqual({ status: 'passed' });
expect(testServerConnection.events.filter(e => e[0] === 'testPaused')).toEqual([]);
});
+
+test('runGlobalSetup returns env', async ({ startTestServer, writeFiles }) => {
+ await writeFiles({
+ 'playwright.config.ts': `
+ export default { globalSetup: './global-setup.ts' };
+ `,
+ 'global-setup.ts': `
+ export default async function() {
+ delete process.env.MAGIC_BEFORE;
+ process.env.MAGIC_AFTER = '43';
+ }
+ `,
+ 'a.spec.ts': `
+ import { test, expect } from '@playwright/test';
+ test('foo', () => {});
+ `,
+ });
+
+ const testServerConnection = await startTestServer({ env: { 'MAGIC_BEFORE': '42' } });
+ await testServerConnection.initialize({});
+
+ const result1 = await testServerConnection.runGlobalSetup({});
+ expect(result1.status).toBe('passed');
+ expect(result1.env).toContainEqual(['MAGIC_BEFORE', null]);
+ expect(result1.env).toContainEqual(['MAGIC_AFTER', '43']);
+
+ await testServerConnection.runGlobalTeardown({});
+
+ // Second time in the same process still works.
+ const result2 = await testServerConnection.runGlobalSetup({});
+ expect(result2.status).toBe('passed');
+ expect(result2.env).toContainEqual(['MAGIC_BEFORE', null]);
+ expect(result2.env).toContainEqual(['MAGIC_AFTER', '43']);
+});