Skip to content

Commit

Permalink
feat: add custom matcher for playwright
Browse files Browse the repository at this point in the history
  • Loading branch information
mahdikhashan committed Jul 19, 2024
1 parent a19f9a8 commit f710750
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions test/helpers/playwright-custom-expects.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
"use strict";

const { expect, mergeExpects } = require('@playwright/test');

// TODO: clean and refactor it, check with the team about bypassing the undefined
const toMatchSnapshotWithArray = expect.extend({
async toMatchSnapshotWithArray(received) {
const assertionName = "toMatchSnapshotWithArray";
let pass;
let matcherResult;
try {
const serialized = JSON.stringify(received);
await expect(serialized).toMatchSnapshot();
pass = true;
} catch (e) {
matcherResult = e.matcherResult;
pass = false;
}

const message = pass
// eslint-disable-next-line no-undefined
? () => `${this.utils.matcherHint(assertionName, undefined, undefined, { isNot: this.isNot })
}\n\n` +
`Expected: ${this.isNot ? 'not' : ''}${this.utils.printExpected(matcherResult.actual)}\n${
matcherResult ? `Received: ${this.utils.printReceived(received)}` : ''}`
// eslint-disable-next-line no-undefined
: () => `${this.utils.matcherHint(assertionName, undefined, undefined, { isNot: this.isNot })
}\n\n` +
`Expected: ${this.utils.printExpected(matcherResult.actual)}\n${
matcherResult ? `Received: ${this.utils.printReceived(received)}` : ''}`;

return {
message,
pass,
name: assertionName,
expected: received,
actual: matcherResult?.actual
}
},
})

module.exports = {
expect: mergeExpects(toMatchSnapshotWithArray)
};

0 comments on commit f710750

Please sign in to comment.