forked from fabricjs/fabric.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
toMatchRoundedSnapshot.ts
66 lines (62 loc) · 1.88 KB
/
toMatchRoundedSnapshot.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
55
56
57
58
59
60
61
62
63
64
65
66
/* eslint-disable @typescript-eslint/no-namespace */
import { expect } from '@jest/globals';
import { toMatchSnapshot } from 'jest-snapshot';
import { cloneDeep } from './src/util/internals/cloneDeep';
// https://github.com/jestjs/jest/blob/70b17d2e3d2bf40dd27fecdcdd06c159d26a7c6c/website/versioned_docs/version-25.x/ExpectAPI.md?plain=1#L241-L262
function roundDeep(received: any, key: string | number) {
const value = received[key];
Array.isArray(value)
? value.forEach((entry, index, array) => roundDeep(array, index))
: typeof value === 'object'
? Object.keys(value).forEach((key) => roundDeep(value, key))
: typeof value === 'number' && (received[key] = Math.round(value));
}
expect.extend({
toMatchRoundedSnapshot(
received: Record<string, unknown>,
keys?: string[],
...propertiesOrHint
) {
const clone = cloneDeep(received);
(keys ?? Object.keys(clone)).forEach((key) => {
roundDeep(clone, key);
});
return toMatchSnapshot.call(this, clone, ...propertiesOrHint);
},
});
// // written in official docs but DOESN'T work
// declare module 'expect' {
// interface AsymmetricMatchers {
// toMatchRoundedSnapshot(
// keys?: string[],
// propertiesOrHint?: object | string,
// hint?: string
// ): void;
// }
// interface Matchers<R, T> {
// toMatchRoundedSnapshot(
// keys?: (keyof T)[],
// propertiesOrHint?: object | string,
// hint?: string
// ): R;
// }
// }
// not written in official docs but DOES work
declare global {
namespace jest {
interface AsymmetricMatchers {
toMatchRoundedSnapshot(
keys?: string[],
propertiesOrHint?: object | string,
hint?: string
): void;
}
interface Matchers<R, T> {
toMatchRoundedSnapshot(
keys?: (keyof T)[],
propertiesOrHint?: object | string,
hint?: string
): R;
}
}
}