-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathvite.global.setup.mjs
More file actions
109 lines (94 loc) · 3.14 KB
/
vite.global.setup.mjs
File metadata and controls
109 lines (94 loc) · 3.14 KB
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import { beforeAll } from 'vitest'
beforeAll(() => {
expect.addSnapshotSerializer(
replaceProperties({ property: [
// Replace asset storage locations in Lambda function snapshots
'Code.S3Bucket',
'Code.S3Key',
// Replace asset storage locations in Step Function snapshots
'DefinitionS3Location.Bucket',
'DefinitionS3Location.Key'
] })
);
})
const PLACEHOLDER = '[SNAPSHOT_PLACEHOLDER]';
const isObject = (val) => !!val && typeof val === 'object';
// Helper function to traverse object and find properties to replace
const findPropertiesToReplace = (
obj,
property,
path = []
) => {
const results = [];
for (const [key, value] of Object.entries(obj)) {
const currentPath = [...path, key];
const fullPath = currentPath.join('.');
let shouldReplace = false;
if (property instanceof RegExp) {
shouldReplace = property.test(fullPath);
} else if (Array.isArray(property)) {
shouldReplace = property.includes(fullPath);
} else {
shouldReplace = fullPath === property;
}
if (shouldReplace && value !== PLACEHOLDER) {
results.push({ path: currentPath, value });
}
if (isObject(value)) {
results.push(...findPropertiesToReplace(value, property, currentPath));
}
}
return results;
};
// Helper function to set value at nested path
const setValueAtPath = (obj, path, value) => {
let current = obj;
for (let i = 0; i < path.length - 1; i++) {
const key = path[i];
if (!isObject(current[key])) {
current[key] = {};
}
current = current[key];
}
current[path[path.length - 1]] = value;
};
/**
* Custom serializer for vitest snapshot tests
* Allows replacing properties in a snapshot with placeholder values.
*
* Properties to replace can be specified as a string, array of strings, or a single regular expression
* Nested properties can be specified using dot notation.
*
* @example
* ```
* beforeAll(() => {
* expect.addSnapshotSerializer(
* // Will replace the value of Code: { S3Bucket: '...', S3Key: '...' } anywhere in the object structure
* replaceProperties({ property: ['Code.S3Bucket', 'Code.S3Key'] })
* );
* })
* ```
*
* @param {{ property: string | string[] | RegExp, placeholder?: string }} input
* @returns
*/
export const replaceProperties = ({
property,
placeholder = PLACEHOLDER,
}) => {
return {
test(val) {
if (!isObject(val)) return false;
const propertiesToReplace = findPropertiesToReplace(val, property);
return propertiesToReplace.length > 0;
},
serialize(val, config, indentation, depth, refs, printer) {
const clone = { ...val };
const propertiesToReplace = findPropertiesToReplace(clone, property);
for (const { path } of propertiesToReplace) {
setValueAtPath(clone, path, placeholder);
}
return printer(clone, config, indentation, depth, refs);
},
}
};