mirror of
https://github.com/microsoft/playwright.git
synced 2025-06-26 21:40:17 +00:00
chore: support aria snapshots in tsx (#33369)
This commit is contained in:
parent
81e7d9fa25
commit
a43b99368e
@ -23,7 +23,6 @@ import * as babel from '@babel/core';
|
|||||||
export { codeFrameColumns } from '@babel/code-frame';
|
export { codeFrameColumns } from '@babel/code-frame';
|
||||||
export { declare } from '@babel/helper-plugin-utils';
|
export { declare } from '@babel/helper-plugin-utils';
|
||||||
export { types } from '@babel/core';
|
export { types } from '@babel/core';
|
||||||
export { parse } from '@babel/parser';
|
|
||||||
import traverseFunction from '@babel/traverse';
|
import traverseFunction from '@babel/traverse';
|
||||||
export const traverse = traverseFunction;
|
export const traverse = traverseFunction;
|
||||||
|
|
||||||
@ -114,16 +113,25 @@ function babelTransformOptions(isTypeScript: boolean, isModule: boolean, plugins
|
|||||||
|
|
||||||
let isTransforming = false;
|
let isTransforming = false;
|
||||||
|
|
||||||
export function babelTransform(code: string, filename: string, isTypeScript: boolean, isModule: boolean, pluginsPrologue: [string, any?][], pluginsEpilogue: [string, any?][]): BabelFileResult {
|
function isTypeScript(filename: string) {
|
||||||
|
return filename.endsWith('.ts') || filename.endsWith('.tsx') || filename.endsWith('.mts') || filename.endsWith('.cts');
|
||||||
|
}
|
||||||
|
|
||||||
|
export function babelTransform(code: string, filename: string, isModule: boolean, pluginsPrologue: [string, any?][], pluginsEpilogue: [string, any?][]): BabelFileResult {
|
||||||
if (isTransforming)
|
if (isTransforming)
|
||||||
return {};
|
return {};
|
||||||
|
|
||||||
// Prevent reentry while requiring plugins lazily.
|
// Prevent reentry while requiring plugins lazily.
|
||||||
isTransforming = true;
|
isTransforming = true;
|
||||||
try {
|
try {
|
||||||
const options = babelTransformOptions(isTypeScript, isModule, pluginsPrologue, pluginsEpilogue);
|
const options = babelTransformOptions(isTypeScript(filename), isModule, pluginsPrologue, pluginsEpilogue);
|
||||||
return babel.transform(code, { filename, ...options })!;
|
return babel.transform(code, { filename, ...options })!;
|
||||||
} finally {
|
} finally {
|
||||||
isTransforming = false;
|
isTransforming = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function babelParse(code: string, filename: string, isModule: boolean): babel.ParseResult {
|
||||||
|
const options = babelTransformOptions(isTypeScript(filename), isModule, [], []);
|
||||||
|
return babel.parse(code, { filename, ...options })!;
|
||||||
|
}
|
||||||
|
|||||||
@ -17,7 +17,7 @@
|
|||||||
import path from 'path';
|
import path from 'path';
|
||||||
import fs from 'fs';
|
import fs from 'fs';
|
||||||
import type { T } from '../transform/babelBundle';
|
import type { T } from '../transform/babelBundle';
|
||||||
import { types, traverse, parse } from '../transform/babelBundle';
|
import { types, traverse, babelParse } from '../transform/babelBundle';
|
||||||
import { MultiMap } from 'playwright-core/lib/utils';
|
import { MultiMap } from 'playwright-core/lib/utils';
|
||||||
import { generateUnifiedDiff } from 'playwright-core/lib/utils';
|
import { generateUnifiedDiff } from 'playwright-core/lib/utils';
|
||||||
import type { FullConfigInternal } from '../common/config';
|
import type { FullConfigInternal } from '../common/config';
|
||||||
@ -53,7 +53,7 @@ export async function applySuggestedRebaselines(config: FullConfigInternal) {
|
|||||||
const source = await fs.promises.readFile(fileName, 'utf8');
|
const source = await fs.promises.readFile(fileName, 'utf8');
|
||||||
const lines = source.split('\n');
|
const lines = source.split('\n');
|
||||||
const replacements = suggestedRebaselines.get(fileName);
|
const replacements = suggestedRebaselines.get(fileName);
|
||||||
const fileNode = parse(source, { sourceType: 'module' });
|
const fileNode = babelParse(source, fileName, true);
|
||||||
const ranges: { start: number, end: number, oldText: string, newText: string }[] = [];
|
const ranges: { start: number, end: number, oldText: string, newText: string }[] = [];
|
||||||
|
|
||||||
traverse(fileNode, {
|
traverse(fileNode, {
|
||||||
|
|||||||
@ -14,14 +14,15 @@
|
|||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import type { BabelFileResult } from '../../bundles/babel/node_modules/@types/babel__core';
|
import type { BabelFileResult, ParseResult } from '../../bundles/babel/node_modules/@types/babel__core';
|
||||||
export const codeFrameColumns: typeof import('../../bundles/babel/node_modules/@types/babel__code-frame').codeFrameColumns = require('./babelBundleImpl').codeFrameColumns;
|
export const codeFrameColumns: typeof import('../../bundles/babel/node_modules/@types/babel__code-frame').codeFrameColumns = require('./babelBundleImpl').codeFrameColumns;
|
||||||
export const declare: typeof import('../../bundles/babel/node_modules/@types/babel__helper-plugin-utils').declare = require('./babelBundleImpl').declare;
|
export const declare: typeof import('../../bundles/babel/node_modules/@types/babel__helper-plugin-utils').declare = require('./babelBundleImpl').declare;
|
||||||
export const types: typeof import('../../bundles/babel/node_modules/@types/babel__core').types = require('./babelBundleImpl').types;
|
export const types: typeof import('../../bundles/babel/node_modules/@types/babel__core').types = require('./babelBundleImpl').types;
|
||||||
export const parse: typeof import('../../bundles/babel/node_modules/@babel/parser/typings/babel-parser').parse = require('./babelBundleImpl').parse;
|
|
||||||
export const traverse: typeof import('../../bundles/babel/node_modules/@types/babel__traverse').default = require('./babelBundleImpl').traverse;
|
export const traverse: typeof import('../../bundles/babel/node_modules/@types/babel__traverse').default = require('./babelBundleImpl').traverse;
|
||||||
export type BabelPlugin = [string, any?];
|
export type BabelPlugin = [string, any?];
|
||||||
export type BabelTransformFunction = (code: string, filename: string, isTypeScript: boolean, isModule: boolean, pluginsPrefix: BabelPlugin[], pluginsSuffix: BabelPlugin[]) => BabelFileResult;
|
export type BabelTransformFunction = (code: string, filename: string, isModule: boolean, pluginsPrefix: BabelPlugin[], pluginsSuffix: BabelPlugin[]) => BabelFileResult;
|
||||||
export const babelTransform: BabelTransformFunction = require('./babelBundleImpl').babelTransform;
|
export const babelTransform: BabelTransformFunction = require('./babelBundleImpl').babelTransform;
|
||||||
|
export type BabelParseFunction = (code: string, filename: string, isModule: boolean) => ParseResult;
|
||||||
|
export const babelParse: BabelParseFunction = require('./babelBundleImpl').babelParse;
|
||||||
export type { NodePath, types as T, PluginObj } from '../../bundles/babel/node_modules/@types/babel__core';
|
export type { NodePath, types as T, PluginObj } from '../../bundles/babel/node_modules/@types/babel__core';
|
||||||
export type { BabelAPI } from '../../bundles/babel/node_modules/@types/babel__helper-plugin-utils';
|
export type { BabelAPI } from '../../bundles/babel/node_modules/@types/babel__helper-plugin-utils';
|
||||||
|
|||||||
@ -215,7 +215,6 @@ export function setTransformData(pluginName: string, value: any) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export function transformHook(originalCode: string, filename: string, moduleUrl?: string): { code: string, serializedCache?: any } {
|
export function transformHook(originalCode: string, filename: string, moduleUrl?: string): { code: string, serializedCache?: any } {
|
||||||
const isTypeScript = filename.endsWith('.ts') || filename.endsWith('.tsx') || filename.endsWith('.mts') || filename.endsWith('.cts');
|
|
||||||
const hasPreprocessor =
|
const hasPreprocessor =
|
||||||
process.env.PW_TEST_SOURCE_TRANSFORM &&
|
process.env.PW_TEST_SOURCE_TRANSFORM &&
|
||||||
process.env.PW_TEST_SOURCE_TRANSFORM_SCOPE &&
|
process.env.PW_TEST_SOURCE_TRANSFORM_SCOPE &&
|
||||||
@ -233,7 +232,7 @@ export function transformHook(originalCode: string, filename: string, moduleUrl?
|
|||||||
|
|
||||||
const { babelTransform }: { babelTransform: BabelTransformFunction } = require('./babelBundle');
|
const { babelTransform }: { babelTransform: BabelTransformFunction } = require('./babelBundle');
|
||||||
transformData = new Map<string, any>();
|
transformData = new Map<string, any>();
|
||||||
const { code, map } = babelTransform(originalCode, filename, isTypeScript, !!moduleUrl, pluginsPrologue, pluginsEpilogue);
|
const { code, map } = babelTransform(originalCode, filename, !!moduleUrl, pluginsPrologue, pluginsEpilogue);
|
||||||
if (!code)
|
if (!code)
|
||||||
return { code: '', serializedCache };
|
return { code: '', serializedCache };
|
||||||
const added = addToCache!(code, map, transformData);
|
const added = addToCache!(code, map, transformData);
|
||||||
|
|||||||
@ -15,7 +15,7 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
import * as fs from 'fs';
|
import * as fs from 'fs';
|
||||||
import { test, expect } from './playwright-test-fixtures';
|
import { test, expect, playwrightCtConfigText } from './playwright-test-fixtures';
|
||||||
|
|
||||||
test.describe.configure({ mode: 'parallel' });
|
test.describe.configure({ mode: 'parallel' });
|
||||||
|
|
||||||
@ -163,3 +163,42 @@ test('should generate baseline with special characters', async ({ runInlineTest
|
|||||||
|
|
||||||
`);
|
`);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('should update missing snapshots in tsx', async ({ runInlineTest }, testInfo) => {
|
||||||
|
const result = await runInlineTest({
|
||||||
|
'playwright.config.ts': playwrightCtConfigText,
|
||||||
|
'playwright/index.html': `<script type="module" src="./index.ts"></script>`,
|
||||||
|
'playwright/index.ts': ``,
|
||||||
|
|
||||||
|
'src/button.tsx': `
|
||||||
|
export const Button = () => <button>Button</button>;
|
||||||
|
`,
|
||||||
|
|
||||||
|
'src/button.test.tsx': `
|
||||||
|
import { test, expect } from '@playwright/experimental-ct-react';
|
||||||
|
import { Button } from './button.tsx';
|
||||||
|
|
||||||
|
test('pass', async ({ mount }) => {
|
||||||
|
const component = await mount(<Button></Button>);
|
||||||
|
await expect(component).toMatchAriaSnapshot(\`\`);
|
||||||
|
});
|
||||||
|
`,
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(result.exitCode).toBe(0);
|
||||||
|
const patchPath = testInfo.outputPath('test-results/rebaselines.patch');
|
||||||
|
const data = fs.readFileSync(patchPath, 'utf-8');
|
||||||
|
expect(data).toBe(`--- a/src/button.test.tsx
|
||||||
|
+++ b/src/button.test.tsx
|
||||||
|
@@ -4,6 +4,8 @@
|
||||||
|
|
||||||
|
test('pass', async ({ mount }) => {
|
||||||
|
const component = await mount(<Button></Button>);
|
||||||
|
- await expect(component).toMatchAriaSnapshot(\`\`);
|
||||||
|
+ await expect(component).toMatchAriaSnapshot(\`
|
||||||
|
+ - button \"Button\"
|
||||||
|
+ \`);
|
||||||
|
});
|
||||||
|
|
||||||
|
`);
|
||||||
|
});
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user