Skip to content

Commit 81cd13e

Browse files
committed
chore: pass custom extended name to snapshots generated by toMatchSnapshotWithArray
1 parent 2296ca0 commit 81cd13e

File tree

1 file changed

+38
-17
lines changed

1 file changed

+38
-17
lines changed

Diff for: test/helpers/playwright-custom-expects.js

+38-17
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,65 @@
11
"use strict";
22

3-
const { expect, mergeExpects } = require('@playwright/test');
3+
const path = require("path");
4+
const { test, expect, mergeExpects } = require("@playwright/test");
45

56
// TODO: clean and refactor it, check with the team about bypassing the undefined
67
const toMatchSnapshotWithArray = expect.extend({
7-
async toMatchSnapshotWithArray(received) {
8+
async toMatchSnapshotWithArray(received, name) {
89
const assertionName = "toMatchSnapshotWithArray";
910
let pass;
1011
let matcherResult;
12+
13+
const testInfo = test.info();
14+
15+
const snapshotFileName = `${name}.snap.webpack5`;
16+
const snapshotFilePath = path.join(
17+
testInfo.titlePath.slice(1).join("."),
18+
snapshotFileName,
19+
);
20+
1121
try {
1222
const serialized = JSON.stringify(received);
13-
await expect(serialized).toMatchSnapshot();
23+
await expect(serialized).toMatchSnapshot({
24+
name: snapshotFilePath,
25+
});
1426
pass = true;
1527
} catch (e) {
1628
matcherResult = e.matcherResult;
1729
pass = false;
1830
}
1931

32+
/* eslint-disable no-undefined */
2033
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)}` : ''}`;
34+
? () =>
35+
`${this.utils.matcherHint(assertionName, undefined, undefined, {
36+
isNot: this.isNot,
37+
})}\n\n` +
38+
`Expected: ${this.isNot ? "not" : ""}${this.utils.printExpected(matcherResult.actual)}\n${
39+
matcherResult
40+
? `Received: ${this.utils.printReceived(received)}`
41+
: ""
42+
}`
43+
: () =>
44+
`${this.utils.matcherHint(assertionName, undefined, undefined, {
45+
isNot: this.isNot,
46+
})}\n\n` +
47+
`Expected: ${this.utils.printExpected(matcherResult.actual)}\n${
48+
matcherResult
49+
? `Received: ${this.utils.printReceived(received)}`
50+
: ""
51+
}`;
3152

3253
return {
3354
message,
3455
pass,
3556
name: assertionName,
3657
expected: received,
37-
actual: matcherResult?.actual
38-
}
58+
actual: matcherResult?.actual,
59+
};
3960
},
40-
})
61+
});
4162

4263
module.exports = {
43-
expect: mergeExpects(toMatchSnapshotWithArray)
64+
expect: mergeExpects(toMatchSnapshotWithArray),
4465
};

0 commit comments

Comments
 (0)