Skip to content

Commit f710750

Browse files
committed
feat: add custom matcher for playwright
1 parent a19f9a8 commit f710750

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
"use strict";
2+
3+
const { expect, mergeExpects } = require('@playwright/test');
4+
5+
// TODO: clean and refactor it, check with the team about bypassing the undefined
6+
const toMatchSnapshotWithArray = expect.extend({
7+
async toMatchSnapshotWithArray(received) {
8+
const assertionName = "toMatchSnapshotWithArray";
9+
let pass;
10+
let matcherResult;
11+
try {
12+
const serialized = JSON.stringify(received);
13+
await expect(serialized).toMatchSnapshot();
14+
pass = true;
15+
} catch (e) {
16+
matcherResult = e.matcherResult;
17+
pass = false;
18+
}
19+
20+
const message = pass
21+
// eslint-disable-next-line no-undefined
22+
? () => `${this.utils.matcherHint(assertionName, undefined, undefined, { isNot: this.isNot })
23+
}\n\n` +
24+
`Expected: ${this.isNot ? 'not' : ''}${this.utils.printExpected(matcherResult.actual)}\n${
25+
matcherResult ? `Received: ${this.utils.printReceived(received)}` : ''}`
26+
// eslint-disable-next-line no-undefined
27+
: () => `${this.utils.matcherHint(assertionName, undefined, undefined, { isNot: this.isNot })
28+
}\n\n` +
29+
`Expected: ${this.utils.printExpected(matcherResult.actual)}\n${
30+
matcherResult ? `Received: ${this.utils.printReceived(received)}` : ''}`;
31+
32+
return {
33+
message,
34+
pass,
35+
name: assertionName,
36+
expected: received,
37+
actual: matcherResult?.actual
38+
}
39+
},
40+
})
41+
42+
module.exports = {
43+
expect: mergeExpects(toMatchSnapshotWithArray)
44+
};

0 commit comments

Comments
 (0)