fix(matchers): repeating values lead to no error (#15559)

This commit is contained in:
Max Schmitt 2022-07-12 20:03:27 +02:00 committed by GitHub
parent f3d3231b29
commit 7e1801bd30
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 13 additions and 11 deletions

View File

@ -1122,18 +1122,14 @@ export class InjectedScript {
return { received, matches: false };
// Each matcher should get a "received" that matches it, in order.
let i = 0;
const matchers = options.expectedText.map(e => new ExpectedTextMatcher(e));
let allMatchesFound = true;
for (const matcher of matchers) {
while (i < received.length && !matcher.matches(received[i]))
i++;
if (i >= received.length) {
allMatchesFound = false;
break;
}
let mIndex = 0, rIndex = 0;
while (mIndex < matchers.length && rIndex < received.length) {
if (matchers[mIndex].matches(received[rIndex]))
++mIndex;
++rIndex;
}
return { received, matches: allMatchesFound };
return { received, matches: mIndex === matchers.length };
}
throw this.createStacklessError('Unknown expect matcher: ' + expression);
}

View File

@ -222,6 +222,12 @@ test('should support toHaveText w/ array', async ({ runInlineTest }) => {
const locator = page.locator('div');
await expect(locator).toHaveText(['Text 1', /Text \\d/, 'Extra'], { timeout: 1000 });
});
test('fail on repeating array matchers', async ({ page }) => {
await page.setContent('<div>KekFoo</div>');
const locator = page.locator('div');
await expect(locator).toContainText(['KekFoo', 'KekFoo', 'KekFoo'], { timeout: 1000 });
});
`,
}, { workers: 1 });
const output = stripAnsi(result.output);
@ -231,7 +237,7 @@ test('should support toHaveText w/ array', async ({ runInlineTest }) => {
expect(output).toContain('waiting for selector "div"');
expect(output).toContain('selector resolved to 2 elements');
expect(result.passed).toBe(6);
expect(result.failed).toBe(2);
expect(result.failed).toBe(3);
expect(result.exitCode).toBe(1);
});