-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathnormalize.js
More file actions
38 lines (30 loc) · 914 Bytes
/
normalize.js
File metadata and controls
38 lines (30 loc) · 914 Bytes
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
const path = require('node:path');
const CURRENT_CWD = process.cwd();
const ROOT = path.resolve(__dirname, '../../');
const RSPACK = path.dirname(require.resolve('@rspack/core/package.json'));
const normalize = (str) => {
let normalizedStr = str.replace(/(\\)+/g, '/');
normalizedStr = normalizedStr
.split(RSPACK.replace(/(\\)+/g, '/'))
.join('<rspack>');
normalizedStr = normalizedStr
.split(ROOT.replace(/(\\)+/g, '/'))
.join('<root>');
normalizedStr = normalizedStr
.split(CURRENT_CWD.replace(/(\\)+/g, '/'))
.join('<cwd>');
normalizedStr = normalizedStr.replace(/:\d+:\d+/g, ':<line>:<row>');
normalizedStr = normalizedStr.replace(
/@@ -\d+,\d+ \+\d+,\d+ @@/g,
'@@ ... @@',
);
return normalizedStr;
};
expect.addSnapshotSerializer({
test(value) {
return typeof value === 'string';
},
print(received) {
return normalize(received);
},
});