-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
vitest.setup.ts
54 lines (48 loc) · 2.01 KB
/
vitest.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
43
44
45
46
47
48
49
50
51
52
53
54
import { format } from 'prettier';
import type { SnapshotSerializer } from 'vitest';
// eslint-disable-next-line no-restricted-imports
import { FIXTURE_DIR_PATH } from './packages/happy-css-modules/src/test-util/util.js';
const jsonSerializer: SnapshotSerializer = {
serialize(val) {
const json = JSON.stringify(val);
const replacedJson = json.replace(new RegExp(FIXTURE_DIR_PATH, 'gu'), '<fixtures>');
return format(replacedJson, { parser: 'json5', printWidth: 120 }).trimEnd();
},
test(val) {
const isLoadResult =
val &&
Object.prototype.hasOwnProperty.call(val, 'tokens') &&
Object.prototype.hasOwnProperty.call(val, 'dependencies');
const isLocation =
val &&
Object.prototype.hasOwnProperty.call(val, 'filePath') &&
Object.prototype.hasOwnProperty.call(val, 'start') &&
Object.prototype.hasOwnProperty.call(val, 'end');
const isSourceMap =
val &&
Object.prototype.hasOwnProperty.call(val, 'file') &&
Object.prototype.hasOwnProperty.call(val, 'mappings') &&
Object.prototype.hasOwnProperty.call(val, 'names') &&
Object.prototype.hasOwnProperty.call(val, 'sourceRoot') &&
Object.prototype.hasOwnProperty.call(val, 'version') &&
Object.prototype.hasOwnProperty.call(val, 'sources');
const isDefinition =
val &&
Object.prototype.hasOwnProperty.call(val, 'file') &&
Object.prototype.hasOwnProperty.call(val, 'text') &&
Object.prototype.hasOwnProperty.call(val, 'start') &&
Object.prototype.hasOwnProperty.call(val, 'end');
return isLoadResult || isLocation || isSourceMap || isDefinition;
},
};
const errorSerializer: SnapshotSerializer = {
serialize(val) {
if (!(val instanceof Error)) throw new Error('unreachable');
return val.message.replace(new RegExp(FIXTURE_DIR_PATH, 'gu'), '<fixtures>');
},
test(val) {
return val instanceof Error && val.message.includes(FIXTURE_DIR_PATH);
},
};
expect.addSnapshotSerializer(jsonSerializer);
expect.addSnapshotSerializer(errorSerializer);