|
1 | 1 | "use strict";
|
2 | 2 |
|
3 |
| -const { expect, mergeExpects } = require('@playwright/test'); |
| 3 | +const path = require("path"); |
| 4 | +const { test, expect, mergeExpects } = require("@playwright/test"); |
4 | 5 |
|
5 | 6 | // TODO: clean and refactor it, check with the team about bypassing the undefined
|
6 | 7 | const toMatchSnapshotWithArray = expect.extend({
|
7 |
| - async toMatchSnapshotWithArray(received) { |
| 8 | + async toMatchSnapshotWithArray(received, name) { |
8 | 9 | const assertionName = "toMatchSnapshotWithArray";
|
9 | 10 | let pass;
|
10 | 11 | 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 | + |
11 | 21 | try {
|
12 | 22 | const serialized = JSON.stringify(received);
|
13 |
| - await expect(serialized).toMatchSnapshot(); |
| 23 | + await expect(serialized).toMatchSnapshot({ |
| 24 | + name: snapshotFilePath, |
| 25 | + }); |
14 | 26 | pass = true;
|
15 | 27 | } catch (e) {
|
16 | 28 | matcherResult = e.matcherResult;
|
17 | 29 | pass = false;
|
18 | 30 | }
|
19 | 31 |
|
| 32 | + /* eslint-disable no-undefined */ |
20 | 33 | 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 | + }`; |
31 | 52 |
|
32 | 53 | return {
|
33 | 54 | message,
|
34 | 55 | pass,
|
35 | 56 | name: assertionName,
|
36 | 57 | expected: received,
|
37 |
| - actual: matcherResult?.actual |
38 |
| - } |
| 58 | + actual: matcherResult?.actual, |
| 59 | + }; |
39 | 60 | },
|
40 |
| -}) |
| 61 | +}); |
41 | 62 |
|
42 | 63 | module.exports = {
|
43 |
| - expect: mergeExpects(toMatchSnapshotWithArray) |
| 64 | + expect: mergeExpects(toMatchSnapshotWithArray), |
44 | 65 | };
|
0 commit comments