Skip to content

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

test: pin module semantics of transpiled code (#43293)

Pavel Feldman8baa5c2d2cd5
diff --git a/tests/playwright-test/babel.spec.ts b/tests/playwright-test/babel.spec.ts
index 5843ab7384..e454c8410d 100644
--- a/tests/playwright-test/babel.spec.ts
+++ b/tests/playwright-test/babel.spec.ts
@@ -215,3 +215,82 @@ for (const type of ['module', undefined]) {
expect(result.stdout).toContain('imported value: bar');
});
}
+
+test('should allow overwriting exports of a transpiled module', async ({ runInlineTest }) => {
+ const result = await runInlineTest({
+ 'util.ts': `
+ export function greet() { return 'real'; }
+ `,
+ 'greeter.ts': `
+ import { greet } from './util';
+ export function run() { return greet(); }
+ `,
+ 'example.spec.ts': `
+ import { test, expect } from '@playwright/test';
+ import * as util from './util';
+ import { run } from './greeter';
+
+ test('works', () => {
+ (util as any).greet = () => 'stub';
+ expect(run()).toBe('stub');
+ });
+ `,
+ });
+ expect(result.exitCode).toBe(0);
+ expect(result.passed).toBe(1);
+});
+
+test('should read undefined from an uninitialized import in a cycle', async ({ runInlineTest }) => {
+ const result = await runInlineTest({
+ 'a.ts': `
+ import './b';
+ export const value = 'a';
+ `,
+ 'b.ts': `
+ import { value } from './a';
+ export const valueSeenByB = value;
+ `,
+ 'example.spec.ts': `
+ import { test, expect } from '@playwright/test';
+ import { value } from './a';
+ import { valueSeenByB } from './b';
+
+ test('works', () => {
+ expect(value).toBe('a');
+ expect(valueSeenByB).toBe(undefined);
+ });
+ `,
+ });
+ expect(result.exitCode).toBe(0);
+ expect(result.passed).toBe(1);
+});
+
+test('should parse generic arrow functions in tsx', async ({ runInlineTest }) => {
+ const result = await runInlineTest({
+ 'example.spec.tsx': `
+ import { test, expect } from '@playwright/test';
+
+ const identity = <T>(value: T) => value;
+
+ test('works', () => {
+ expect(identity('value')).toBe('value');
+ });
+ `,
+ });
+ expect(result.exitCode).toBe(0);
+ expect(result.passed).toBe(1);
+});
+
+test('should reject import.meta in commonjs', async ({ runInlineTest }) => {
+ const result = await runInlineTest({
+ 'example.spec.ts': `
+ import { test, expect } from '@playwright/test';
+
+ test('works', () => {
+ expect(import.meta.dirname).toBeTruthy();
+ });
+ `,
+ });
+ expect(result.exitCode).toBe(1);
+ expect(result.output).toMatch(/(SyntaxError: Cannot use 'import.meta' outside a module)|(ReferenceError: require is not defined in ES module scope)/);
+});