-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjest.setup.ts
42 lines (36 loc) · 1.17 KB
/
jest.setup.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
// Disable chalk colors since GitHub Actions doesn't support them (would conflict with e2e snapshots).
process.env.FORCE_COLOR = "0";
import path from "path";
import process from "process";
import { expect } from "@jest/globals"; // Import Jest globals
import { toMatchSnapshot } from "jest-snapshot"; // Import Jest snapshot matcher
// TODO. Find some courage some day to refactor this properly to jest snapshotSerializers. If jest+ESM+typescript is less a burden by then.
expect.extend({
toMatchSnapshotWithNormalizedPaths(received) {
return toMatchSnapshot.call(
this,
normalizeReceived(received),
"",
);
},
});
const normalizeReceived = (received: string | string[]) => {
let normalized: string | string[] = received;
if (typeof received === "string") {
normalized = received.replaceAll(
new RegExp(path.resolve(process.cwd()), "gm"),
"<ROOT>",
);
} else if (
Array.isArray(received) &&
received.every(item => typeof item === "string")
) {
normalized = received.map(item =>
item.replaceAll(
new RegExp(path.resolve(process.cwd()), "gm"),
"<ROOT>",
),
);
}
return normalized;
};