diff --git a/e2e/global.d.ts b/e2e/global.d.ts new file mode 100644 index 00000000000..1a9353aeea6 --- /dev/null +++ b/e2e/global.d.ts @@ -0,0 +1,15 @@ +export {}; + +declare global { + namespace PlaywrightTest { + interface Matchers { + toMatchDataSnapshot: (options?: { name?: string }) => R; + toMatchImageSnapshot: (options?: { + maxDiffPixelRatio?: number; + maxDiffPixels?: number; + name?: string | string[]; + threshold?: number; + }) => R; + } + } +} diff --git a/e2e/matchers.ts b/e2e/matchers.ts new file mode 100644 index 00000000000..c992ea1ee5a --- /dev/null +++ b/e2e/matchers.ts @@ -0,0 +1,43 @@ +import type { Expect } from '@playwright/test'; +import { expect } from '@playwright/test'; + +expect.extend({ + toMatchDataSnapshot(received: object, options?: { name?: string }) { + try { + expect(JSON.stringify(received, null, 2)).toMatchSnapshot(options); + return { + message: () => 'passed', + pass: true, + }; + } catch (err) { + return { + message: () => 'failed', + pass: false, + }; + } + }, + toMatchImageSnapshot( + received, + options: Parameters['toMatchSnapshot']>[0] + ) { + let data = received; + if (typeof received === 'string' && received.startsWith('data:image')) { + const [type, content] = received + .replace(/^data:image\/([^;]+);([^,]+),(.+)/, '$2 $3') + .split(' ') as [BufferEncoding, string]; + data = new Uint8Array(Buffer.from(content, type).buffer); + } + try { + expect(data).toMatchSnapshot(options); + return { + message: () => 'passed', + pass: true, + }; + } catch (err) { + return { + message: () => 'failed', + pass: false, + }; + } + }, +}); diff --git a/playwright.config.ts b/playwright.config.ts index c6f5f187590..08cc71b6b33 100644 --- a/playwright.config.ts +++ b/playwright.config.ts @@ -1,6 +1,9 @@ import type { PlaywrightTestConfig } from '@playwright/test'; import { devices } from '@playwright/test'; +// extend `expect` +import './e2e/matchers'; + /** * See https://playwright.dev/docs/test-configuration. */