fix(test runner): allow worker-only dynamic imports (#21545)

Fixes #21409.
This commit is contained in:
Dmitry Gozman 2023-03-10 08:58:26 -08:00 committed by GitHub
parent 0794cb1486
commit 58a23bc7a0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 26 additions and 5 deletions

View File

@ -19,7 +19,6 @@ import fs from 'fs';
import os from 'os';
import path from 'path';
import { sourceMapSupport } from '../utilsBundle';
import { isWorkerProcess } from './globals';
export type MemoryCache = {
codePath: string;
@ -67,9 +66,6 @@ export function getFromCompilationCache(filename: string, code: string, moduleUr
if (cache?.codePath)
return { cachedCode: fs.readFileSync(cache.codePath, 'utf-8') };
if (isWorkerProcess())
throw new Error('Internal error: unexpected file imported in the worker: ' + filename);
// Then do the disk cache, this cache works between the Playwright Test runs.
const isModule = !!moduleUrl;
const cachePath = calculateCachePath(code, filename, isModule);

View File

@ -145,7 +145,6 @@ export function js2ts(resolved: string): string | undefined {
}
export function transformHook(code: string, filename: string, moduleUrl?: string): string {
// If we are not TypeScript and there is no applicable preprocessor - bail out.
const { cachedCode, addToCache } = getFromCompilationCache(filename, code, moduleUrl);
if (cachedCode)
return cachedCode;

View File

@ -663,3 +663,29 @@ test('should complain when one test file imports another', async ({ runInlineTes
expect(result.exitCode).toBe(1);
expect(result.output).toContain(`test file "a.test.ts" should not import test file "b.test.ts"`);
});
test('should support dynamic import', async ({ runInlineTest, nodeVersion }) => {
const result = await runInlineTest({
'helper.ts': `
module.exports.foo = 'foo';
`,
'a.test.ts': `
import { test, expect } from '@playwright/test';
test('pass', async () => {
const { foo } = await import('./helper');
expect(foo).toBe('foo');
});
`,
'b.test.ts': `
import { test, expect } from '@playwright/test';
test('pass', async () => {
const { foo } = await import('./helper');
expect(foo).toBe('foo');
});
`,
}, { workers: 1 });
expect(result.passed).toBe(2);
expect(result.exitCode).toBe(0);
});