Skip to content

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

fix(runner): import export assignment from ts (#19559)

Han Yeong-woo00ffd747270d
diff --git a/packages/playwright-test/bundles/babel/src/babelBundleImpl.ts b/packages/playwright-test/bundles/babel/src/babelBundleImpl.ts
index 8b13eb680d..6980b4de85 100644
--- a/packages/playwright-test/bundles/babel/src/babelBundleImpl.ts
+++ b/packages/playwright-test/bundles/babel/src/babelBundleImpl.ts
@@ -14,7 +14,9 @@
* limitations under the License.
*/
-import type { BabelFileResult } from '@babel/core';
+import type { BabelFileResult, NodePath, PluginObj } from '@babel/core';
+import type { TSExportAssignment } from '@babel/types';
+import type { TemplateBuilder } from '@babel/template';
import * as babel from '@babel/core';
export { codeFrameColumns } from '@babel/code-frame';
@@ -39,7 +41,22 @@ export function babelTransform(filename: string, isTypeScript: boolean, isModule
[require('@babel/plugin-syntax-optional-catch-binding')],
[require('@babel/plugin-syntax-async-generators')],
[require('@babel/plugin-syntax-object-rest-spread')],
- [require('@babel/plugin-proposal-export-namespace-from')]
+ [require('@babel/plugin-proposal-export-namespace-from')],
+ [
+ // From https://github.com/G-Rath/babel-plugin-replace-ts-export-assignment/blob/8dfdca32c8aa428574b0cae341444fc5822f2dc6/src/index.ts
+ (
+ { template }: { template: TemplateBuilder<TSExportAssignment> }
+ ): PluginObj => ({
+ name: 'replace-ts-export-assignment',
+ visitor: {
+ TSExportAssignment(path: NodePath<TSExportAssignment>) {
+ path.replaceWith(template('module.exports = ASSIGNMENT;')({
+ ASSIGNMENT: path.node.expression
+ }));
+ }
+ }
+ })
+ ]
);
}
diff --git a/tests/playwright-test/loader.spec.ts b/tests/playwright-test/loader.spec.ts
index b4766f40ff..8d25ce6624 100644
--- a/tests/playwright-test/loader.spec.ts
+++ b/tests/playwright-test/loader.spec.ts
@@ -507,3 +507,20 @@ test('should resolve .js import to .ts file in non-ESM mode', async ({ runInline
expect(result.passed).toBe(1);
expect(result.exitCode).toBe(0);
});
+
+test('should import export assignment from ts', async ({ runInlineTest }) => {
+ const result = await runInlineTest({
+ 'a.test.ts': `
+ const { test } = pwt;
+ import number from './utils.js';
+ test('pass', () => {
+ expect(number).toBe(1);
+ });
+ `,
+ 'utils.ts': `
+ export = 1;
+ `
+ });
+ expect(result.passed).toBe(1);
+ expect(result.exitCode).toBe(0);
+});