Skip to content

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

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

Dmitry Gozman58a23bc7a0c2
diff --git a/packages/playwright-test/src/common/compilationCache.ts b/packages/playwright-test/src/common/compilationCache.ts
index 898c8a53b0..9f438266fd 100644
--- a/packages/playwright-test/src/common/compilationCache.ts
+++ b/packages/playwright-test/src/common/compilationCache.ts
@@ -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);
diff --git a/packages/playwright-test/src/common/transform.ts b/packages/playwright-test/src/common/transform.ts
index 24ad15ae42..7bedda954f 100644
--- a/packages/playwright-test/src/common/transform.ts
+++ b/packages/playwright-test/src/common/transform.ts
@@ -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;
diff --git a/tests/playwright-test/loader.spec.ts b/tests/playwright-test/loader.spec.ts
index 160b78c724..53b949ed6a 100644
--- a/tests/playwright-test/loader.spec.ts
+++ b/tests/playwright-test/loader.spec.ts
@@ -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);
+});