fix(toHaveCount): another edge case fix (#9860)

This commit is contained in:
Pavel Feldman 2021-10-28 15:04:21 -08:00 committed by GitHub
parent 21c4435060
commit 75ac579fac
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 3 deletions

View file

@ -1183,8 +1183,9 @@ export class Frame extends SdkObject {
// expect(listLocator).toHaveCount(0) passes when there are no elements matching.
// expect(listLocator).not.toHaveCount(1) passes when there are no elements matching.
if (options.expression === 'to.have.count')
return { matches: options.expectedNumber === 0, received: options.expectedNumber };
const expectsEmptyCount = options.expectedNumber === 0;
if (options.expression === 'to.have.count' && expectsEmptyCount !== options.isNot)
return { matches: expectsEmptyCount, received: 0 };
// When none of the above applies, keep waiting for the element.
return continuePolling;

View file

@ -49,6 +49,24 @@ test('should support toHaveCount', async ({ runInlineTest }) => {
await expect(locator).not.toHaveCount(1);
});
test('eventually pass non-zero', async ({ page }) => {
await page.setContent('<ul></ul>');
setTimeout(async () => {
await page.setContent("<ul><li>one</li><li>two</li></ul>");
}, 500);
const locator = page.locator('li');
await expect(locator).toHaveCount(2);
});
test('eventually pass not non-zero', async ({ page }) => {
await page.setContent('<ul><li>one</li><li>two</li></ul>');
setTimeout(async () => {
await page.setContent("<ul></ul>");
}, 500);
const locator = page.locator('li');
await expect(locator).not.toHaveCount(2);
});
test('fail zero', async ({ page }) => {
await page.setContent('<div><span></span></div>');
const locator = page.locator('span');
@ -63,7 +81,7 @@ test('should support toHaveCount', async ({ runInlineTest }) => {
`,
}, { workers: 1 });
const output = stripAscii(result.output);
expect(result.passed).toBe(3);
expect(result.passed).toBe(5);
expect(result.failed).toBe(2);
expect(result.exitCode).toBe(1);
expect(output).toContain('Expected: 0');