Skip to content

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

fix(ct): tolerate compiled react components (#15422)

Pavel Feldman1b264010db0a
diff --git a/packages/playwright-test/src/plugins/vitePlugin.ts b/packages/playwright-test/src/plugins/vitePlugin.ts
index 42675d017d..019f97618d 100644
--- a/packages/playwright-test/src/plugins/vitePlugin.ts
+++ b/packages/playwright-test/src/plugins/vitePlugin.ts
@@ -27,7 +27,7 @@ import { assert, calculateSha1 } from 'playwright-core/lib/utils';
import type { AddressInfo } from 'net';
let previewServer: PreviewServer;
-const VERSION = 4;
+const VERSION = 5;
type CtConfig = {
ctPort?: number;
@@ -37,6 +37,7 @@ type CtConfig = {
};
const importReactRE = /(^|\n)import\s+(\*\s+as\s+)?React(,|\s+)/;
+const compiledReactRE = /(const|var)\s+React\s*=/;
export function createPlugin(
registerSourceFile: string,
@@ -275,7 +276,7 @@ function vitePlugin(registerSource: string, relativeTemplateDir: string, buildIn
}
// Vite React plugin will do this for .jsx files, but not .js files.
- if (id.endsWith('.js') && content.includes('React.createElement') && !content.match(importReactRE)) {
+ if (id.endsWith('.js') && content.includes('React.createElement') && !content.match(importReactRE) && !content.match(compiledReactRE)) {
const code = `import React from 'react';\n${content}`;
return { code, map: { mappings: '' } };
}
diff --git a/packages/playwright-test/src/transform.ts b/packages/playwright-test/src/transform.ts
index 3afee40c80..85b17ae16b 100644
--- a/packages/playwright-test/src/transform.ts
+++ b/packages/playwright-test/src/transform.ts
@@ -26,7 +26,7 @@ import { tsConfigLoader } from './third_party/tsconfig-loader';
import Module from 'module';
import type { BabelTransformFunction } from './babelBundle';
-const version = 12;
+const version = 13;
const cacheDir = process.env.PWTEST_CACHE_DIR || path.join(os.tmpdir(), 'playwright-transform-cache');
const sourceMaps: Map<string, string> = new Map();