fix(expect): fix toHaveCount(0) (#9690)

This commit is contained in:
Pavel Feldman 2021-10-21 13:46:54 -08:00 committed by GitHub
parent da464cfe0c
commit 299dffbdb3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 38 additions and 2 deletions

View File

@ -1181,6 +1181,11 @@ export class Frame extends SdkObject {
if (options.expression.endsWith('.array') && expectsEmptyList !== options.isNot)
return { matches: expectsEmptyList };
// 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 };
// When none of the above applies, keep waiting for the element.
return continuePolling;
}

View File

@ -33,10 +33,41 @@ test('should support toHaveCount', async ({ runInlineTest }) => {
await promise;
expect(done).toBe(true);
});
test('pass zero', async ({ page }) => {
await page.setContent('<div></div>');
const locator = page.locator('span');
await expect(locator).toHaveCount(0);
await expect(locator).not.toHaveCount(1);
});
test('eventually pass zero', async ({ page }) => {
await page.setContent('<div></div>');
const locator = page.locator('span');
setTimeout(() => page.evaluate(() => div.textContent = '').catch(() => {}), 200);
await expect(locator).toHaveCount(0);
await expect(locator).not.toHaveCount(1);
});
test('fail zero', async ({ page }) => {
await page.setContent('<div><span></span></div>');
const locator = page.locator('span');
await expect(locator).toHaveCount(0, { timeout: 500 });
});
test('fail zero 2', async ({ page }) => {
await page.setContent('<div><span></span></div>');
const locator = page.locator('span');
await expect(locator).not.toHaveCount(1, { timeout: 500 });
});
`,
}, { workers: 1 });
expect(result.passed).toBe(1);
expect(result.exitCode).toBe(0);
const output = stripAscii(result.output);
expect(result.passed).toBe(3);
expect(result.failed).toBe(2);
expect(result.exitCode).toBe(1);
expect(output).toContain('Expected: 0');
expect(output).toContain('Received: 1');
});
test('should support toHaveJSProperty', async ({ runInlineTest }) => {