forked from jestjs/jest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
State.js
175 lines (154 loc) · 4.57 KB
/
State.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
164
165
166
167
168
169
170
171
172
173
174
175
/**
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
* @flow
*/
'use strict';
import type {Path} from 'types/Config';
const {
saveSnapshotFile,
getSnapshotData,
getSnapshotPath,
keyToTestName,
serialize,
testNameToKey,
unescape,
} = require('./utils');
const fileExists = require('jest-file-exists');
const fs = require('fs');
class SnapshotState {
_counters: Map<string, number>;
_dirty: boolean;
_index: number;
_snapshotData: {[key: string]: string};
_snapshotPath: Path;
_uncheckedKeys: Set<string>;
added: number;
expand: boolean;
failedTests: Set<string>;
matched: number;
skippedTests: Set<string>;
unmatched: number;
update: boolean;
updated: number;
constructor(
testPath: Path,
update: boolean,
snapshotPath?: string,
expand?: boolean,
) {
this._dirty = false;
this._snapshotPath = snapshotPath || getSnapshotPath(testPath);
this._snapshotData = getSnapshotData(this._snapshotPath, update);
this._uncheckedKeys = new Set(Object.keys(this._snapshotData));
this._counters = new Map();
this._index = 0;
this.expand = expand || false;
this.added = 0;
this.matched = 0;
this.unmatched = 0;
this.update = update;
this.updated = 0;
this.skippedTests = new Set();
this.failedTests = new Set();
}
markSnapshotsAsCheckedForTest(testName: string) {
this._uncheckedKeys.forEach(uncheckedKey => {
if (keyToTestName(uncheckedKey) === testName) {
this._uncheckedKeys.delete(uncheckedKey);
}
});
}
_addSnapshot(key: string, receivedSerialized: string) {
this._dirty = true;
this._snapshotData[key] = receivedSerialized;
}
save(update: boolean) {
const status = {
deleted: false,
saved: false,
};
const isEmpty = Object.keys(this._snapshotData).length === 0;
if ((this._dirty || this._uncheckedKeys.size || update) && !isEmpty) {
saveSnapshotFile(this._snapshotData, this._snapshotPath);
status.saved = true;
} else if (isEmpty && fileExists(this._snapshotPath)) {
if (update) {
fs.unlinkSync(this._snapshotPath);
}
status.deleted = true;
}
return status;
}
getUncheckedCount(): number {
return this._uncheckedKeys.size || 0;
}
removeUncheckedKeys(): void {
if (this._uncheckedKeys.size) {
this._dirty = true;
this._uncheckedKeys.forEach(key => delete this._snapshotData[key]);
this._uncheckedKeys.clear();
}
}
match(testName: string, received: any, key?: string) {
this._counters.set(testName, (this._counters.get(testName) || 0) + 1);
const count = Number(this._counters.get(testName));
if (!key) {
key = testNameToKey(testName, count);
}
this._uncheckedKeys.delete(key);
const receivedSerialized = serialize(received);
const expected = this._snapshotData[key];
const pass = expected === receivedSerialized;
const hasSnapshot = this._snapshotData[key] !== undefined;
if (pass) {
// Executing a snapshot file as JavaScript and writing the strings back
// when other snapshots have changed loses the proper escaping for some
// characters. Since we check every snapshot in every test, use the newly
// generated formatted string.
// Note that this is only relevant when a snapshot is added and the dirty
// flag is set.
this._snapshotData[key] = receivedSerialized;
}
if (
!fileExists(this._snapshotPath) || // there's no snapshot file
(hasSnapshot && this.update) || // there is a file, but we're updating
!hasSnapshot // there is a file, but it doesn't have this snaphsot
) {
if (this.update) {
if (!pass) {
if (hasSnapshot) {
this.updated++;
} else {
this.added++;
}
this._addSnapshot(key, receivedSerialized);
} else {
this.matched++;
}
} else {
this._addSnapshot(key, receivedSerialized);
this.added++;
}
return {pass: true};
} else {
if (!pass) {
this.unmatched++;
return {
actual: unescape(receivedSerialized),
count,
expected: unescape(expected),
pass: false,
};
} else {
this.matched++;
return {pass: true};
}
}
}
}
module.exports = SnapshotState;