chore: support jsx/tsx syntax in test files (#18123)

Fixes https://github.com/microsoft/playwright/issues/17964
This commit is contained in:
Pavel Feldman 2022-10-18 15:45:33 -04:00 committed by GitHub
parent 928f7c8766
commit 7ab4c17519
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 25 additions and 3 deletions

View file

@ -41,10 +41,11 @@ export function babelTransform(filename: string, isTypeScript: boolean, isModule
[require('@babel/plugin-syntax-object-rest-spread')],
[require('@babel/plugin-proposal-export-namespace-from')]
);
} else {
plugins.push([require('@babel/plugin-syntax-jsx')]);
}
// Support JSX/TSX at all times, regardless of the file extension.
plugins.push([require('@babel/plugin-syntax-jsx')]);
if (!isModule) {
plugins.push([require('@babel/plugin-transform-modules-commonjs')]);
plugins.push([require('@babel/plugin-proposal-dynamic-import')]);

View file

@ -450,3 +450,24 @@ test('should load web server w/o esm loader in ems module', async ({ runInlineTe
expect(result.passed).toBe(0);
expect(result.output).toContain('NODE_OPTIONS undefined');
});
test('should load a jsx/tsx files', async ({ runInlineTest }) => {
const { exitCode, passed } = await runInlineTest({
'a.spec.tsx': `
const { test } = pwt;
const component = () => <div></div>;
test('succeeds', () => {
expect(1 + 1).toBe(2);
});
`,
'b.spec.jsx': `
const { test } = pwt;
const component = () => <div></div>;
test('succeeds', () => {
expect(1 + 1).toBe(2);
});
`
});
expect(passed).toBe(2);
expect(exitCode).toBe(0);
});

View file

@ -88,7 +88,7 @@ async function writeFiles(testInfo: TestInfo, files: Files) {
const header = isTypeScriptSourceFile ? headerTS : (isJSModule ? headerESM : headerJS);
if (typeof files[name] === 'string' && files[name].includes('//@no-header')) {
await fs.promises.writeFile(fullName, files[name]);
} else if (/(spec|test)\.(js|ts|mjs)$/.test(name)) {
} else if (/(spec|test)\.(js|ts|jsx|tsx|mjs)$/.test(name)) {
const fileHeader = header + 'const { expect } = pwt;\n';
await fs.promises.writeFile(fullName, fileHeader + files[name]);
} else if (/\.(js|ts)$/.test(name) && !name.endsWith('d.ts')) {