Skip to content

Commit

Permalink
matchers
Browse files Browse the repository at this point in the history
Update playwright.config.ts
  • Loading branch information
ShaMan123 committed Jul 20, 2023
1 parent 1849a03 commit cca2de5
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 0 deletions.
15 changes: 15 additions & 0 deletions e2e/global.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
export {};

declare global {
namespace PlaywrightTest {
interface Matchers<R, T> {
toMatchDataSnapshot: (options?: { name?: string }) => R;
toMatchImageSnapshot: (options?: {
maxDiffPixelRatio?: number;
maxDiffPixels?: number;
name?: string | string[];
threshold?: number;
}) => R;
}
}
}
43 changes: 43 additions & 0 deletions e2e/matchers.ts
Original file line number Diff line number Diff line change
@@ -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<ReturnType<Expect>['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,
};
}
},
});
3 changes: 3 additions & 0 deletions playwright.config.ts
Original file line number Diff line number Diff line change
@@ -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.
*/
Expand Down

0 comments on commit cca2de5

Please sign in to comment.