Skip to content

Commit

Permalink
chery-pick(#16733): chore: do not use experimental loader for web server
Browse files Browse the repository at this point in the history
  • Loading branch information
pavelfeldman committed Aug 23, 2022
1 parent 403b723 commit 9bc72cc
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 3 deletions.
14 changes: 13 additions & 1 deletion packages/playwright-test/src/cli.ts
Expand Up @@ -243,7 +243,7 @@ function restartWithExperimentalTsEsm(configFile: string | null): boolean {
return false;
if (!fileIsModule(configFile))
return false;
const NODE_OPTIONS = (process.env.NODE_OPTIONS || '') + ` --experimental-loader=${url.pathToFileURL(require.resolve('@playwright/test/lib/experimentalLoader')).toString()}`;
const NODE_OPTIONS = (process.env.NODE_OPTIONS || '') + experimentalLoaderOption();
const innerProcess = require('child_process').fork(require.resolve('playwright-core/cli'), process.argv.slice(2), {
env: {
...process.env,
Expand All @@ -259,4 +259,16 @@ function restartWithExperimentalTsEsm(configFile: string | null): boolean {
return true;
}

export function experimentalLoaderOption() {
return ` --experimental-loader=${url.pathToFileURL(require.resolve('@playwright/test/lib/experimentalLoader')).toString()}`;
}

export function envWithoutExperimentalLoaderOptions(): NodeJS.ProcessEnv {
const substring = experimentalLoaderOption();
const result = { ...process.env };
if (result.NODE_OPTIONS)
result.NODE_OPTIONS = result.NODE_OPTIONS.replace(substring, '').trim() || undefined;
return result;
}

const kTraceModes: TraceMode[] = ['on', 'off', 'on-first-retry', 'retain-on-failure'];
5 changes: 3 additions & 2 deletions packages/playwright-test/src/plugins/webServerPlugin.ts
Expand Up @@ -25,6 +25,7 @@ import { launchProcess } from 'playwright-core/lib/utils/processLauncher';
import type { FullConfig, Reporter } from '../../types/testReporter';
import type { TestRunnerPlugin } from '.';
import type { FullConfigInternal } from '../types';
import { envWithoutExperimentalLoaderOptions } from '../cli';


export type WebServerPluginOptions = {
Expand Down Expand Up @@ -91,15 +92,15 @@ export class WebServerPlugin implements TestRunnerPlugin {
command: this._options.command,
env: {
...DEFAULT_ENVIRONMENT_VARIABLES,
...process.env,
...envWithoutExperimentalLoaderOptions(),
...this._options.env,
},
cwd: this._options.cwd,
stdio: 'stdin',
shell: true,
attemptToGracefullyClose: async () => {},
log: () => {},
onExit: code => processExitedReject(new Error(`Process from config.webServer was not able to start. Exit code: ${code}`)),
onExit: code => processExitedReject(new Error(code ? `Process from config.webServer was not able to start. Exit code: ${code}` : 'Process from config.webServer exited early.')),
tempDirectories: [],
});
this._killProcess = kill;
Expand Down
31 changes: 31 additions & 0 deletions tests/playwright-test/loader.spec.ts
Expand Up @@ -411,3 +411,34 @@ test('should work with cross-imports - 2', async ({ runInlineTest }) => {
expect(result.output).toContain('TEST-1');
expect(result.output).toContain('TEST-2');
});

test('should load web server w/o esm loader in ems module', async ({ runInlineTest, nodeVersion }) => {
// We only support experimental esm mode on Node 16+
test.skip(nodeVersion.major < 16);
const result = await runInlineTest({
'playwright.config.ts': `
//@no-header
export default {
webServer: {
command: 'node ws.js',
port: 9876,
timeout: 100,
},
projects: [{name: 'foo'}]
}`,
'package.json': `{ "type": "module" }`,
'ws.js': `
//@no-header
console.log('NODE_OPTIONS ' + process.env.NODE_OPTIONS);
setTimeout(() => {}, 100000);
`,
'a.test.ts': `
const { test } = pwt;
test('passes', () => {});
`
}, {}, { ...process.env, DEBUG: 'pw:webserver' });

expect(result.exitCode).toBe(1);
expect(result.passed).toBe(0);
expect(result.output).toContain('NODE_OPTIONS undefined');
});

0 comments on commit 9bc72cc

Please sign in to comment.