From 4f72a895e91c63ba7920484743076b1ddc7ed4bd Mon Sep 17 00:00:00 2001 From: Dmitry Gozman Date: Mon, 28 Nov 2022 20:50:16 -0800 Subject: [PATCH] fix(inspector): render expect.not correctly (#19125) Also fixes the same in expect logs. References #19083. --- packages/playwright-test/src/matchers/toBeTruthy.ts | 2 +- packages/playwright-test/src/matchers/toMatchSnapshot.ts | 2 +- packages/playwright-test/src/matchers/toMatchText.ts | 2 +- packages/recorder/src/callLog.tsx | 2 +- tests/library/inspector/pause.spec.ts | 2 ++ tests/page/expect-boolean.spec.ts | 6 +++--- tests/page/expect-misc.spec.ts | 4 ++-- 7 files changed, 11 insertions(+), 9 deletions(-) diff --git a/packages/playwright-test/src/matchers/toBeTruthy.ts b/packages/playwright-test/src/matchers/toBeTruthy.ts index 58644408dc..cb8802e879 100644 --- a/packages/playwright-test/src/matchers/toBeTruthy.ts +++ b/packages/playwright-test/src/matchers/toBeTruthy.ts @@ -36,7 +36,7 @@ export async function toBeTruthy( const timeout = currentExpectTimeout(options); - const { matches, log, timedOut } = await query(this.isNot, timeout, captureStackTrace('expect.' + matcherName)); + const { matches, log, timedOut } = await query(this.isNot, timeout, captureStackTrace(`expect.${this.isNot ? 'not.' : ''}${matcherName}`)); const message = () => { return matcherHint(this, matcherName, undefined, '', matcherOptions, timedOut ? timeout : undefined) + callLogText(log); diff --git a/packages/playwright-test/src/matchers/toMatchSnapshot.ts b/packages/playwright-test/src/matchers/toMatchSnapshot.ts index bd6e0eeba3..e07764ae66 100644 --- a/packages/playwright-test/src/matchers/toMatchSnapshot.ts +++ b/packages/playwright-test/src/matchers/toMatchSnapshot.ts @@ -327,7 +327,7 @@ export async function toHaveScreenshot( maxDiffPixelRatio: undefined, }; - const customStackTrace = captureStackTrace(`expect.toHaveScreenshot`); + const customStackTrace = captureStackTrace(`expect.${this.isNot ? 'not.' : ''}toHaveScreenshot`); const hasSnapshot = fs.existsSync(helper.snapshotPath); if (this.isNot) { if (!hasSnapshot) diff --git a/packages/playwright-test/src/matchers/toMatchText.ts b/packages/playwright-test/src/matchers/toMatchText.ts index 47ceca878a..7987b92358 100644 --- a/packages/playwright-test/src/matchers/toMatchText.ts +++ b/packages/playwright-test/src/matchers/toMatchText.ts @@ -59,7 +59,7 @@ export async function toMatchText( const timeout = currentExpectTimeout(options); - const { matches: pass, received, log, timedOut } = await query(this.isNot, timeout, captureStackTrace('expect.' + matcherName)); + const { matches: pass, received, log, timedOut } = await query(this.isNot, timeout, captureStackTrace(`expect.${this.isNot ? 'not.' : ''}${matcherName}`)); const stringSubstring = options.matchSubstring ? 'substring' : 'string'; const receivedString = received || ''; const message = pass diff --git a/packages/recorder/src/callLog.tsx b/packages/recorder/src/callLog.tsx index aa73853e74..a40be416dc 100644 --- a/packages/recorder/src/callLog.tsx +++ b/packages/recorder/src/callLog.tsx @@ -44,7 +44,7 @@ export const CallLogView: React.FC = ({ const locatorCall = `page.${locator}`; let titlePrefix = callLog.title; let titleSuffix = ''; - if (callLog.title.startsWith('expect.to')) { + if (callLog.title.startsWith('expect.to') || callLog.title.startsWith('expect.not.to')) { titlePrefix = 'expect('; titleSuffix = `).${callLog.title.substring('expect.'.length)}()`; } else if (callLog.title.startsWith('locator.')) { diff --git a/tests/library/inspector/pause.spec.ts b/tests/library/inspector/pause.spec.ts index 63694cb15f..1580d326e6 100644 --- a/tests/library/inspector/pause.spec.ts +++ b/tests/library/inspector/pause.spec.ts @@ -230,6 +230,7 @@ it.describe('pause', () => { const scriptPromise = (async () => { await page.pause(); await expect(page.locator('button')).toHaveText('Submit'); + await expect(page.locator('button')).not.toHaveText('Submit2'); await page.pause(); // 2 })(); const recorderPage = await recorderPageGetter(); @@ -238,6 +239,7 @@ it.describe('pause', () => { expect(await sanitizeLog(recorderPage)).toEqual([ 'page.pause- XXms', 'expect(page.locator(\'button\')).toHaveText()- XXms', + 'expect(page.locator(\'button\')).not.toHaveText()- XXms', 'page.pause', ]); await recorderPage.click('[title="Resume (F8)"]'); diff --git a/tests/page/expect-boolean.spec.ts b/tests/page/expect-boolean.spec.ts index 3cf89c10b5..f8d0974cef 100644 --- a/tests/page/expect-boolean.spec.ts +++ b/tests/page/expect-boolean.spec.ts @@ -58,7 +58,7 @@ test.describe('toBeChecked', () => { await page.setContent(''); const locator = page.locator('input'); const error = await expect(locator).not.toBeChecked({ timeout: 1000 }).catch(e => e); - expect(error.message).toContain(`expect.toBeChecked with timeout 1000ms`); + expect(error.message).toContain(`expect.not.toBeChecked with timeout 1000ms`); expect(error.message).toContain(`locator resolved to `); }); @@ -73,7 +73,7 @@ test.describe('toBeChecked', () => { await page.setContent('
no inputs here
'); const locator2 = page.locator('input2'); const error = await expect(locator2).not.toBeChecked({ timeout: 1000 }).catch(e => e); - expect(error.message).toContain(`expect.toBeChecked with timeout 1000ms`); + expect(error.message).toContain(`expect.not.toBeChecked with timeout 1000ms`); expect(error.message).toContain('waiting for locator(\'input2\')'); }); @@ -368,7 +368,7 @@ test.describe('toBeHidden', () => { await page.setContent('
'); const locator = page.locator('button'); const error = await expect(locator).not.toBeHidden({ timeout: 1000 }).catch(e => e); - expect(error.message).toContain(`expect.toBeHidden with timeout 1000ms`); + expect(error.message).toContain(`expect.not.toBeHidden with timeout 1000ms`); }); test('with impossible timeout .not', async ({ page }) => { diff --git a/tests/page/expect-misc.spec.ts b/tests/page/expect-misc.spec.ts index 87f900295f..37d3916d45 100644 --- a/tests/page/expect-misc.spec.ts +++ b/tests/page/expect-misc.spec.ts @@ -255,11 +255,11 @@ test.describe('toHaveAttribute', () => { await expect(locator).toHaveAttribute('checked', /.*/); { const error = await expect(locator).not.toHaveAttribute('checked', '', { timeout: 1000 }).catch(e => e); - expect(error.message).toContain('expect.toHaveAttribute with timeout 1000ms'); + expect(error.message).toContain('expect.not.toHaveAttribute with timeout 1000ms'); } { const error = await expect(locator).not.toHaveAttribute('checked', /.*/, { timeout: 1000 }).catch(e => e); - expect(error.message).toContain('expect.toHaveAttribute with timeout 1000ms'); + expect(error.message).toContain('expect.not.toHaveAttribute with timeout 1000ms'); } }); });