-
Notifications
You must be signed in to change notification settings - Fork 242
/
utils.ts
53 lines (46 loc) 路 1.52 KB
/
utils.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
import diff, { DiffOptions } from 'jest-diff';
import { EXPECTED_COLOR, RECEIVED_COLOR } from 'jest-matcher-utils';
import prettyFormat from 'pretty-format';
import {
queryPlanSerializer,
astSerializer,
typeSerializer,
} from '../snapshotSerializers';
const defaultFormatOptions: prettyFormat.OptionsReceived = {
plugins: [queryPlanSerializer, astSerializer, typeSerializer],
};
export function diffFormatted(
expected: unknown,
received: unknown,
diffOptions?: DiffOptions,
formatOptions: prettyFormat.OptionsReceived = defaultFormatOptions,
) {
const expectedString = prettyFormat(expected, formatOptions);
const receivedString = prettyFormat(received, formatOptions);
return diff(expectedString, receivedString, diffOptions);
}
export function indentLines(
text: string,
depth: number = 1,
indent: string = ' ',
) {
const indentation = indent.repeat(depth);
return text
.split('\n')
.map((line) => indentation + line)
.join('\n');
}
// The corresponding functions in `jest-matcher-utils` call their own `stringify` function,
// and that doesn't allow passing in custom pretty-format plugins.
export function printReceivedFormatted(
value: unknown,
formatOptions: prettyFormat.OptionsReceived = defaultFormatOptions,
): string {
return RECEIVED_COLOR(prettyFormat(value, formatOptions));
}
export function printExpectedFormatted(
value: unknown,
formatOptions: prettyFormat.OptionsReceived = defaultFormatOptions,
): string {
return EXPECTED_COLOR(prettyFormat(value, formatOptions));
}