-
Notifications
You must be signed in to change notification settings - Fork 120
/
Copy pathjest.setup.js
51 lines (46 loc) · 1.46 KB
/
jest.setup.js
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
43
44
45
46
47
48
49
50
51
const fc = require("fast-check");
function sanitizeObject(
obj,
options = {
excludeKeys: [],
valueTransformers: {},
},
) {
const { excludeKeys, valueTransformers } = options;
if (Array.isArray(obj)) {
return obj.map((item) => sanitizeObject(item, options));
} else if (obj !== null && typeof obj === "object") {
const newObj = {};
for (const [key, value] of Object.entries(obj)) {
if (!excludeKeys.includes(key)) {
const transformer = valueTransformers[key];
newObj[key] = transformer
? transformer(value)
: sanitizeObject(value, options);
}
}
return newObj;
}
return obj;
}
fc.configureGlobal({
reporter: (log) => {
if (log.failed) {
const sanitizedCounterexample = sanitizeObject(log.counterexample, {
excludeKeys: ["id", "loc"],
valueTransformers: {
value: (val) =>
typeof val === "bigint" ? val.toString() : val,
},
});
const errorMessage = `
Property failed after ${log.numRuns} tests
Seed: ${log.seed}
Path: ${log.counterexamplePath}
Counterexample: ${JSON.stringify(sanitizedCounterexample, null, 0)}
Errors: ${log.error ? log.error : "Unknown error"}
`;
throw new Error(errorMessage);
}
},
});