-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathindex.js
137 lines (116 loc) · 3.55 KB
/
index.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
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
// @flow
'use strict';
const { diff } = require('jest-diff');
const snapshot = require('jest-snapshot');
const reactSerializer = require('./react-serializer');
type Options = {|
expand?: boolean,
colors?: boolean,
contextLines?: number,
stablePatchmarks?: boolean,
aAnnotation?: string,
bAnnotation?: string,
|};
const defaultOptions = {
expand: false,
colors: false,
contextLines: -1, // Forces to use default from Jest
stablePatchmarks: false,
aAnnotation: 'First value',
bAnnotation: 'Second value',
};
const SNAPSHOT_TITLE = 'Snapshot Diff:\n';
const identity = (value) => value;
const defaultSerializers = [reactSerializer];
let serializers = defaultSerializers;
const snapshotDiff = (valueA: any, valueB: any, options?: Options): string => {
let difference;
const mergedOptions = { ...defaultOptions, ...options };
const matchingSerializer = serializers.find(
({ test }) => test(valueA) && test(valueB)
);
if (matchingSerializer) {
const { print, diffOptions } = matchingSerializer;
const serializerOptions = diffOptions
? diffOptions(valueA, valueB) || undefined
: undefined;
difference = diffStrings(print(valueA, identity), print(valueB, identity), {
...mergedOptions,
...serializerOptions,
});
} else {
difference = diffStrings(valueA, valueB, mergedOptions);
}
if (mergedOptions.stablePatchmarks && !mergedOptions.expand) {
difference = difference.replace(
/^@@ -[0-9]+,[0-9]+ \+[0-9]+,[0-9]+ @@$/gm,
'@@ --- --- @@'
);
}
return SNAPSHOT_TITLE + difference;
};
// https://github.com/facebook/jest/tree/d81464622dc8857ba995ed04e121af2b3e8e33bc/packages/jest-diff#example-of-options-for-no-colors
const noDiffColors = {
aColor: identity,
bColor: identity,
changeColor: identity,
commonColor: identity,
patchColor: identity,
};
function diffStrings(valueA: any, valueB: any, options: Options) {
return diff(valueA, valueB, {
expand: options.expand,
contextLines: options.contextLines,
aAnnotation: options.aAnnotation,
bAnnotation: options.bAnnotation,
...(!options.colors ? noDiffColors : {}),
});
}
function toMatchDiffSnapshot(
valueA: any,
valueB: any,
options?: Options,
testName?: string
) {
const difference = snapshotDiff(valueA, valueB, options);
return snapshot.toMatchSnapshot.call(this, difference, testName || '');
}
function toMatchInlineDiffSnapshot(
valueA: any,
valueB: any,
optionsOrSnapshot?: Options | string,
inlineSnapshot?: string
) {
let options = undefined;
if (typeof optionsOrSnapshot === 'string') {
inlineSnapshot = optionsOrSnapshot;
} else {
options = optionsOrSnapshot;
}
const difference = snapshotDiff(valueA, valueB, options);
if (inlineSnapshot) {
return snapshot.toMatchInlineSnapshot.call(this, difference, inlineSnapshot);
} else {
return snapshot.toMatchInlineSnapshot.call(this, difference)
}
}
function getSnapshotDiffSerializer() {
return {
test(value: any) {
return typeof value === 'string' && value.indexOf(SNAPSHOT_TITLE) === 0;
},
print(value: any) {
return value;
},
};
}
function setSerializers(customSerializers) {
serializers = customSerializers;
}
module.exports = snapshotDiff;
module.exports.snapshotDiff = snapshotDiff;
module.exports.toMatchDiffSnapshot = toMatchDiffSnapshot;
module.exports.toMatchInlineDiffSnapshot = toMatchInlineDiffSnapshot;
module.exports.getSnapshotDiffSerializer = getSnapshotDiffSerializer;
module.exports.setSerializers = setSerializers;
module.exports.defaultSerializers = defaultSerializers;