mirror of
https://github.com/microsoft/playwright.git
synced 2024-11-28 09:23:42 +03:00
fix(expect): adjust normalization for regex values in toHaveText matcher (#33533)
This commit is contained in:
parent
e534fba60f
commit
a501232bf0
@ -19,6 +19,7 @@ import { matcherHint } from './matcherHint';
|
||||
import type { MatcherResult } from './matcherHint';
|
||||
import type { ExpectMatcherState } from '../../types/test';
|
||||
import type { Locator } from 'playwright-core';
|
||||
import { isRegExp } from 'playwright-core/lib/utils';
|
||||
|
||||
// Omit colon and one or more spaces, so can call getLabelPrinter.
|
||||
const EXPECTED_LABEL = 'Expected';
|
||||
@ -59,6 +60,21 @@ export async function toEqual<T>(
|
||||
if (pass) {
|
||||
printedExpected = `Expected: not ${this.utils.printExpected(expected)}`;
|
||||
printedReceived = `Received: ${this.utils.printReceived(received)}`;
|
||||
} else if (Array.isArray(expected) && Array.isArray(received)) {
|
||||
const normalizedExpected = expected.map((exp, index) => {
|
||||
const rec = received[index];
|
||||
if (isRegExp(exp))
|
||||
return exp.test(rec) ? rec : exp;
|
||||
|
||||
return exp;
|
||||
});
|
||||
printedDiff = this.utils.printDiffOrStringify(
|
||||
normalizedExpected,
|
||||
received,
|
||||
EXPECTED_LABEL,
|
||||
RECEIVED_LABEL,
|
||||
false,
|
||||
);
|
||||
} else {
|
||||
printedDiff = this.utils.printDiffOrStringify(
|
||||
expected,
|
||||
|
@ -616,6 +616,33 @@ test('should print pending operations for toHaveText', async ({ runInlineTest })
|
||||
expect(output).toContain('waiting for locator(\'no-such-thing\')');
|
||||
});
|
||||
|
||||
test('should only highlight unmatched regex in diff message for toHaveText with array', async ({ runInlineTest }) => {
|
||||
const result = await runInlineTest({
|
||||
'a.spec.ts': `
|
||||
import { test, expect } from '@playwright/test';
|
||||
|
||||
test('toHaveText with mixed strings and regexes (array)', async ({ page }) => {
|
||||
await page.setContent(\`
|
||||
<ul>
|
||||
<li>Coffee</li>
|
||||
<li>Tea</li>
|
||||
<li>Milk</li>
|
||||
</ul>
|
||||
\`);
|
||||
|
||||
const items = page.locator('li');
|
||||
await expect(items).toHaveText(['Coffee', /\\d+/, /Milk/]);
|
||||
});
|
||||
`,
|
||||
});
|
||||
expect(result.exitCode).toBe(1);
|
||||
const output = result.output;
|
||||
expect(output).toContain('- /\\d+/,');
|
||||
expect(output).toContain('+ "Tea",');
|
||||
expect(output).not.toContain('- /Milk/,');
|
||||
expect(output).not.toContain('- "Coffee",');
|
||||
});
|
||||
|
||||
test('should print expected/received on Ctrl+C', async ({ interactWithTestRunner }) => {
|
||||
test.skip(process.platform === 'win32', 'No sending SIGINT on Windows');
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user