-
-
Notifications
You must be signed in to change notification settings - Fork 6.4k
/
index.js
163 lines (143 loc) · 4.23 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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/**
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
*/
import type {HasteFS} from 'types/HasteMap';
import type {MatcherState} from 'types/Matchers';
import type {Path, SnapshotUpdateState} from 'types/Config';
import fs from 'fs';
import path from 'path';
import diff from 'jest-diff';
import {EXPECTED_COLOR, matcherHint, RECEIVED_COLOR} from 'jest-matcher-utils';
import SnapshotState from './State';
import {addSerializer, getSerializers} from './plugins';
import * as utils from './utils';
const fileExists = (filePath: Path, hasteFS: HasteFS): boolean =>
hasteFS.exists(filePath) || fs.existsSync(filePath);
const cleanup = (hasteFS: HasteFS, update: SnapshotUpdateState) => {
const pattern = '\\.' + utils.SNAPSHOT_EXTENSION + '$';
const files = hasteFS.matchFiles(pattern);
const filesRemoved = files
.filter(
snapshotFile =>
!fileExists(
path.resolve(
path.dirname(snapshotFile),
'..',
path.basename(snapshotFile, '.' + utils.SNAPSHOT_EXTENSION),
),
hasteFS,
),
)
.map(snapshotFile => {
if (update === 'all') {
fs.unlinkSync(snapshotFile);
}
}).length;
return {
filesRemoved,
};
};
const toMatchSnapshot = function(received: any, testName?: string) {
this.dontThrow && this.dontThrow();
const {currentTestName, isNot, snapshotState}: MatcherState = this;
if (isNot) {
throw new Error('Jest: `.not` cannot be used with `.toMatchSnapshot()`.');
}
if (!snapshotState) {
throw new Error('Jest: snapshot state must be initialized.');
}
const result = snapshotState.match(
testName && currentTestName
? `${currentTestName}: ${testName}`
: currentTestName || '',
received,
);
const {count, pass} = result;
let {actual, expected} = result;
let report;
if (pass) {
return {message: () => '', pass: true};
} else if (!expected) {
report = () =>
`New snapshot was ${RECEIVED_COLOR('not written')}. The update flag ` +
`must be explicitly passed to write a new snapshot.\n\n` +
`This is likely because this test is run in a continuous integration ` +
`(CI) environment in which snapshots are not written by default.\n\n` +
`${RECEIVED_COLOR('Received value')} ` +
`${actual}`;
} else {
expected = (expected || '').trim();
actual = (actual || '').trim();
const diffMessage = diff(expected, actual, {
aAnnotation: 'Snapshot',
bAnnotation: 'Received',
expand: snapshotState.expand,
});
report = () =>
`${RECEIVED_COLOR('Received value')} does not match ` +
`${EXPECTED_COLOR('stored snapshot ' + count)}.\n\n` +
(diffMessage ||
EXPECTED_COLOR('- ' + (expected || '')) +
'\n' +
RECEIVED_COLOR('+ ' + actual));
}
// Passing the the actual and expected objects so that a custom reporter
// could access them, for example in order to display a custom visual diff,
// or create a different error message
return {
actual,
expected,
message: () =>
matcherHint('.toMatchSnapshot', 'value', '') + '\n\n' + report(),
name: 'toMatchSnapshot',
pass: false,
report,
};
};
const toThrowErrorMatchingSnapshot = function(
received: any,
testName?: string,
fromPromise: boolean,
) {
this.dontThrow && this.dontThrow();
const {isNot} = this;
if (isNot) {
throw new Error(
'Jest: `.not` cannot be used with `.toThrowErrorMatchingSnapshot()`.',
);
}
let error;
if (fromPromise) {
error = received;
} else {
try {
received();
} catch (e) {
error = e;
}
}
if (error === undefined) {
throw new Error(
matcherHint('.toThrowErrorMatchingSnapshot', '() => {}', '') +
'\n\n' +
`Expected the function to throw an error.\n` +
`But it didn't throw anything.`,
);
}
return toMatchSnapshot.call(this, error.message, testName);
};
module.exports = {
EXTENSION: utils.SNAPSHOT_EXTENSION,
SnapshotState,
addSerializer,
cleanup,
getSerializers,
toMatchSnapshot,
toThrowErrorMatchingSnapshot,
utils,
};