@provino/snapshot 0.0.0-stage → 0.1.0-rc.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +8 -2
- package/THIRD_PARTY_NOTICES.md +266 -0
- package/dist/index.d.ts +94 -0
- package/dist/index.js +188 -0
- package/dist/node/index.d.ts +100 -0
- package/dist/node/index.js +198 -0
- package/dist/runtime/index.d.ts +241 -0
- package/dist/runtime/index.js +589 -0
- package/package.json +49 -3
|
@@ -0,0 +1,589 @@
|
|
|
1
|
+
import { dirname } from "node:path";
|
|
2
|
+
import { existsSync, mkdirSync, readFileSync, renameSync, unlinkSync, writeFileSync } from "node:fs";
|
|
3
|
+
import { SnapshotError, SnapshotError as SnapshotError$1, addExtraLineBreaks, checkSnapHeader, deepMerge, defaultSnapshotPath, defaultSnapshotPath as defaultSnapshotPath$1, evaluateSnapFile, keyToTestName, normalizeNewlines, removeExtraLineBreaks, removeExtraLineBreaks as removeExtraLineBreaks$1, serializeSnapFile } from "@provino/snapshot";
|
|
4
|
+
import { fileURLToPath } from "node:url";
|
|
5
|
+
|
|
6
|
+
// src/runtime/lifecycle.ts
|
|
7
|
+
/**
|
|
8
|
+
* Builds the lifecycle of a file's state. `keyJoin` joins a test's title path into the name its
|
|
9
|
+
* snapshots' keys start with.
|
|
10
|
+
* @public
|
|
11
|
+
*/
|
|
12
|
+
function trackSnapshots(state, options) {
|
|
13
|
+
const tests = /* @__PURE__ */ new Map();
|
|
14
|
+
/** Tests that ended well and may be the reason a snapshot is obsolete. */
|
|
15
|
+
const passed = /* @__PURE__ */ new Set();
|
|
16
|
+
let collected = false;
|
|
17
|
+
return {
|
|
18
|
+
observe(event) {
|
|
19
|
+
switch (event.type) {
|
|
20
|
+
case "file-collect-end":
|
|
21
|
+
if (event.errors.length === 0) collected = true;
|
|
22
|
+
for (const task of event.tasks) if (task.type === "test") tests.set(task.id, task);
|
|
23
|
+
break;
|
|
24
|
+
case "test-attempt-start":
|
|
25
|
+
if (event.attempt > 0 || event.repeat > 0) state.clearTest(event.id);
|
|
26
|
+
break;
|
|
27
|
+
case "test-end": {
|
|
28
|
+
const task = tests.get(event.id);
|
|
29
|
+
if (event.result.state === "pass" && task?.expectFail !== true) passed.add(event.id);
|
|
30
|
+
state.forgetTest(event.id);
|
|
31
|
+
break;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
},
|
|
35
|
+
finish() {
|
|
36
|
+
if (!collected) return null;
|
|
37
|
+
for (const [id, task] of tests) if (!passed.has(id)) state.markCheckedForTest(task.fullName.join(options.keyJoin));
|
|
38
|
+
return state.finish();
|
|
39
|
+
}
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
// src/runtime/matcher-state.ts
|
|
44
|
+
/** Each view `matcherSnapshotStateOf` built, to the state it was built over. */
|
|
45
|
+
const stateOfView = /* @__PURE__ */ new WeakMap();
|
|
46
|
+
/**
|
|
47
|
+
* The value for the matcher's `snapshotState`: the state itself when the policy has no view, else
|
|
48
|
+
* the view the factory built over it.
|
|
49
|
+
*/
|
|
50
|
+
function matcherSnapshotStateOf(state, view) {
|
|
51
|
+
if (view === null) return state;
|
|
52
|
+
const built = view(state);
|
|
53
|
+
stateOfView.set(built, state);
|
|
54
|
+
return built;
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* What the snapshot matchers work on, from a matcher's `snapshotState`: the state a view
|
|
58
|
+
* `openFileSnapshots` built was built over, else the value as it is (the state itself, or whatever
|
|
59
|
+
* else a test put there, which the matchers then refuse).
|
|
60
|
+
* @public
|
|
61
|
+
*/
|
|
62
|
+
function unwrapMatcherSnapshotState(value) {
|
|
63
|
+
return stateOfView.get(value) ?? value;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
// src/runtime/files.ts
|
|
67
|
+
/**
|
|
68
|
+
* Reads a snapshot file. A missing file is empty data. A file whose header isn't `header` is
|
|
69
|
+
* rewritten when the mode allows writes, and is an error under `none`, which writes nothing.
|
|
70
|
+
* @internal
|
|
71
|
+
*/
|
|
72
|
+
function readSnapFile(path, update, header) {
|
|
73
|
+
const exists = existsSync(path);
|
|
74
|
+
const text = exists ? readFileSync(path, "utf8") : "";
|
|
75
|
+
const data = evaluateSnapFile(text);
|
|
76
|
+
const problem = text === "" ? null : checkSnapHeader(text, header);
|
|
77
|
+
if (problem !== null && update === "none") throw new SnapshotError$1("SNAPSHOT_HEADER_MISMATCH", problem);
|
|
78
|
+
return {
|
|
79
|
+
data,
|
|
80
|
+
exists,
|
|
81
|
+
dirty: problem !== null
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Writes a file through a temp file in its directory, then a rename.
|
|
86
|
+
* @internal
|
|
87
|
+
*/
|
|
88
|
+
function writeFileAtomically(path, text) {
|
|
89
|
+
mkdirSync(dirname(path), { recursive: true });
|
|
90
|
+
const temp = `${path}.${process.pid}.${Math.random().toString(36).slice(2)}.tmp`;
|
|
91
|
+
try {
|
|
92
|
+
writeFileSync(temp, text);
|
|
93
|
+
renameSync(temp, path);
|
|
94
|
+
} catch (error) {
|
|
95
|
+
try {
|
|
96
|
+
unlinkSync(temp);
|
|
97
|
+
} catch {}
|
|
98
|
+
throw error;
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
// src/runtime/frame.ts
|
|
103
|
+
/**
|
|
104
|
+
* A frame is `at fn (file:line:col)` or, with no function name, `at file:line:col`, each with an
|
|
105
|
+
* optional `async` or `new` before the name. The two are told apart by the last character of the
|
|
106
|
+
* line, not by looking for ` (` anywhere in it: a path may hold one (`My Project (1)`), and a
|
|
107
|
+
* frame with no name has no parenthesis of its own. (stack-utils's `parseLine`, which Jest uses,
|
|
108
|
+
* splits at the first ` (` it finds, so it reads such a path wrong and `async` as part of the
|
|
109
|
+
* file.)
|
|
110
|
+
*/
|
|
111
|
+
const NAMED_FRAME = /^\s*at (?:async |new )?.*? \((.+):(\d+):(\d+)\)$/;
|
|
112
|
+
const BARE_FRAME = /^\s*at (?:async )?(.+):(\d+):(\d+)$/;
|
|
113
|
+
/**
|
|
114
|
+
* The first frame of `stack` with a file outside `node_modules` and Node's own modules, or
|
|
115
|
+
* `undefined` when there is none.
|
|
116
|
+
*/
|
|
117
|
+
function topFrame(stack) {
|
|
118
|
+
for (const line of stack.split("\n")) {
|
|
119
|
+
if (line.includes("/node_modules/")) continue;
|
|
120
|
+
const match = (line.endsWith(")") ? NAMED_FRAME : BARE_FRAME).exec(line);
|
|
121
|
+
if (match === null) continue;
|
|
122
|
+
const [, location, lineText, columnText] = match;
|
|
123
|
+
if (location === void 0 || location.startsWith("node:")) continue;
|
|
124
|
+
return {
|
|
125
|
+
file: location.startsWith("file://") ? fileURLToPath(location) : location,
|
|
126
|
+
line: Number(lineText),
|
|
127
|
+
column: Number(columnText)
|
|
128
|
+
};
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
// src/runtime/state.ts
|
|
133
|
+
/**
|
|
134
|
+
* The snapshot state of one test file.
|
|
135
|
+
* @public
|
|
136
|
+
*/
|
|
137
|
+
var SnapshotState = class {
|
|
138
|
+
testPath;
|
|
139
|
+
snapshotPath;
|
|
140
|
+
update;
|
|
141
|
+
expand;
|
|
142
|
+
/** The pretty-format options every snapshot of the file is serialized with (the policy's `format`). */
|
|
143
|
+
serializeFormat;
|
|
144
|
+
added = 0;
|
|
145
|
+
matched = 0;
|
|
146
|
+
unmatched = 0;
|
|
147
|
+
updated = 0;
|
|
148
|
+
/**
|
|
149
|
+
* The count of snapshots taken per test name, as a live map: the next key of a name follows the
|
|
150
|
+
* count set here. It is there for a dialect's view (`snapshot.contextView`), whose matchers count
|
|
151
|
+
* snapshots of their own; Provino's matchers never read it. A write here, like one to the four
|
|
152
|
+
* counts, belongs to no attempt: `clearTest` keeps it, and puts back only the counters the
|
|
153
|
+
* test's own snapshots touched, each to its value before the test's first snapshot of that name.
|
|
154
|
+
*/
|
|
155
|
+
liveCounters = /* @__PURE__ */ new Map();
|
|
156
|
+
dirty;
|
|
157
|
+
fileExists;
|
|
158
|
+
data;
|
|
159
|
+
uncheckedKeys;
|
|
160
|
+
rawSnapshots = [];
|
|
161
|
+
inlineEdits = [];
|
|
162
|
+
attempts = /* @__PURE__ */ new Map();
|
|
163
|
+
writeCount = 0;
|
|
164
|
+
plugins;
|
|
165
|
+
options;
|
|
166
|
+
constructor(options) {
|
|
167
|
+
this.options = options;
|
|
168
|
+
this.testPath = options.testPath;
|
|
169
|
+
this.snapshotPath = options.snapshotPath;
|
|
170
|
+
this.update = options.update;
|
|
171
|
+
this.expand = options.expand;
|
|
172
|
+
const { escapeString, printBasicPrototype, maxOutputLength, extra } = options.policy.format;
|
|
173
|
+
this.serializeFormat = Object.freeze({
|
|
174
|
+
escapeString,
|
|
175
|
+
printBasicPrototype,
|
|
176
|
+
...maxOutputLength === null ? {} : { maxOutputLength },
|
|
177
|
+
...extra
|
|
178
|
+
});
|
|
179
|
+
const read = readSnapFile(options.snapshotPath, options.update, options.policy.header);
|
|
180
|
+
this.data = read.data;
|
|
181
|
+
this.dirty = read.dirty;
|
|
182
|
+
this.fileExists = read.exists;
|
|
183
|
+
this.uncheckedKeys = new Set(Object.keys(this.data));
|
|
184
|
+
this.plugins = [...options.serializers ?? []];
|
|
185
|
+
}
|
|
186
|
+
/** The serializers in the order they are tried: the one added last first. */
|
|
187
|
+
get serializers() {
|
|
188
|
+
return this.plugins;
|
|
189
|
+
}
|
|
190
|
+
/** How a thrown error is stored by `toThrowErrorMatchingSnapshot` (`SnapshotPolicy.throwErrorBody`). */
|
|
191
|
+
get throwErrorBody() {
|
|
192
|
+
return this.options.policy.throwErrorBody;
|
|
193
|
+
}
|
|
194
|
+
/**
|
|
195
|
+
* A snapshot's test name: the title path joined by the key join, then the hint by the hint
|
|
196
|
+
* join. Jest: `outer inner: hint`. Vitest: `outer > inner > hint`. A hint with no name is
|
|
197
|
+
* dropped, as in Jest (`currentTestName && hint ? ... : currentTestName || ''`).
|
|
198
|
+
*/
|
|
199
|
+
testName(titlePath, hint) {
|
|
200
|
+
const { keyJoin, hintJoin } = this.options.policy;
|
|
201
|
+
const name = titlePath.join(keyJoin);
|
|
202
|
+
return name !== "" && hint !== void 0 && hint !== "" ? `${name}${hintJoin}${hint}` : name;
|
|
203
|
+
}
|
|
204
|
+
/** The dialect's words, for the matchers. */
|
|
205
|
+
get messages() {
|
|
206
|
+
return this.options.printer.messages;
|
|
207
|
+
}
|
|
208
|
+
/**
|
|
209
|
+
* Adds a serializer ahead of the others (`expect.addSnapshotSerializer`). The printer's own
|
|
210
|
+
* plugins come after all of them.
|
|
211
|
+
*/
|
|
212
|
+
addSerializer(plugin) {
|
|
213
|
+
if (typeof plugin !== "object" || plugin === null || typeof plugin.test !== "function" || !("serialize" in plugin ? typeof plugin.serialize === "function" : typeof plugin.print === "function")) throw new SnapshotError$1("SNAPSHOT_BAD_SERIALIZER", "A snapshot serializer must be an object with a `test` function and a `serialize` (or `print`) function");
|
|
214
|
+
this.plugins = [plugin, ...this.plugins];
|
|
215
|
+
}
|
|
216
|
+
/**
|
|
217
|
+
* Marks the snapshots of a test that did not run to its end (skipped, todo or failed) as
|
|
218
|
+
* checked, so they are not reported obsolete. A hint is joined to the name before the key is
|
|
219
|
+
* built, so the name recovered from a hinted key is longer than the test's.
|
|
220
|
+
*/
|
|
221
|
+
markCheckedForTest(testName) {
|
|
222
|
+
const { hintJoin } = this.options.policy;
|
|
223
|
+
for (const uncheckedKey of this.uncheckedKeys) {
|
|
224
|
+
const keyTestName = keyToTestName(uncheckedKey);
|
|
225
|
+
if (keyTestName === testName || keyTestName.startsWith(`${testName}${hintJoin}`)) this.uncheckedKeys.delete(uncheckedKey);
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
/**
|
|
229
|
+
* Takes back what a test's earlier attempt did (a retry, or a repeat): its counters, the
|
|
230
|
+
* snapshots it added, the keys it checked and the totals. Other tests' work stays.
|
|
231
|
+
*/
|
|
232
|
+
clearTest(testId) {
|
|
233
|
+
const record = this.attempts.get(testId);
|
|
234
|
+
if (record === void 0) return;
|
|
235
|
+
this.attempts.delete(testId);
|
|
236
|
+
this.added -= record.counts.added;
|
|
237
|
+
this.matched -= record.counts.matched;
|
|
238
|
+
this.unmatched -= record.counts.unmatched;
|
|
239
|
+
this.updated -= record.counts.updated;
|
|
240
|
+
for (const [key, previous] of record.fileSnapshots) if (previous === void 0) delete this.data[key];
|
|
241
|
+
else this.data[key] = previous;
|
|
242
|
+
for (const raw of record.rawSnapshots) this.rawSnapshots.splice(this.rawSnapshots.indexOf(raw), 1);
|
|
243
|
+
for (const edit of record.inlineEdits) this.inlineEdits.splice(this.inlineEdits.indexOf(edit), 1);
|
|
244
|
+
for (const key of record.checkedKeys) this.uncheckedKeys.add(key);
|
|
245
|
+
for (const [testName, previous] of record.counters) if (previous === void 0) this.liveCounters.delete(testName);
|
|
246
|
+
else this.liveCounters.set(testName, previous);
|
|
247
|
+
const { writes } = record;
|
|
248
|
+
if (writes !== void 0 && this.writeCount - writes.writeCountBefore === writes.ownWrites) {
|
|
249
|
+
this.dirty = writes.dirtyBefore;
|
|
250
|
+
this.writeCount = writes.writeCountBefore;
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
/** The test is over: its attempt record is no longer needed. */
|
|
254
|
+
forgetTest(testId) {
|
|
255
|
+
this.attempts.delete(testId);
|
|
256
|
+
}
|
|
257
|
+
/**
|
|
258
|
+
* A snapshot that couldn't be taken (the property matchers failed): the count moves on and
|
|
259
|
+
* the key is checked, so the test's other snapshots keep their numbers.
|
|
260
|
+
*/
|
|
261
|
+
fail(testName, testId, key) {
|
|
262
|
+
const count = this.bumpCounter(testName, testId);
|
|
263
|
+
const stored = key || this.options.key(testName, count);
|
|
264
|
+
this.markKeyChecked(stored, testId);
|
|
265
|
+
this.incrementCount("unmatched", testId);
|
|
266
|
+
return stored;
|
|
267
|
+
}
|
|
268
|
+
/** The attempt running now, as the worker's current-test lookup names it (`SnapshotViewSource`). */
|
|
269
|
+
currentTestId() {
|
|
270
|
+
return this.options.currentTestId?.();
|
|
271
|
+
}
|
|
272
|
+
/** Compares one value with its stored snapshot, and writes it when the mode says to. */
|
|
273
|
+
match(options) {
|
|
274
|
+
const { testName, testId, received, inlineSnapshot, isInline, testFailing = false, stack } = options;
|
|
275
|
+
const count = this.bumpCounter(testName, testId);
|
|
276
|
+
const key = options.key || this.options.key(testName, count);
|
|
277
|
+
if (!(isInline && this.data[key] !== void 0)) this.markKeyChecked(key, testId);
|
|
278
|
+
const receivedSerialized = addExtraLineBreaks(this.serialize(received));
|
|
279
|
+
const expected = isInline ? inlineSnapshot : this.data[key];
|
|
280
|
+
const pass = expected === receivedSerialized;
|
|
281
|
+
const hasSnapshot = expected !== void 0;
|
|
282
|
+
const snapshotIsPersisted = isInline || this.fileExists;
|
|
283
|
+
if (pass && !isInline) this.data[key] = receivedSerialized;
|
|
284
|
+
if (testFailing) {
|
|
285
|
+
if (hasSnapshot && !isInline) this.addSnapshot(key, expected, testId);
|
|
286
|
+
return this.result(pass, count, key, receivedSerialized, expected, false);
|
|
287
|
+
}
|
|
288
|
+
if (hasSnapshot && this.update === "all" || (!hasSnapshot || !snapshotIsPersisted) && (this.update === "new" || this.update === "all")) {
|
|
289
|
+
if (isInline && this.options.inlineWrites !== true && !(pass && this.update === "all")) {
|
|
290
|
+
this.incrementCount("unmatched", testId);
|
|
291
|
+
return this.result(false, count, key, receivedSerialized, expected, true);
|
|
292
|
+
}
|
|
293
|
+
const write = () => isInline ? this.addInline(receivedSerialized, stack, testId) : this.addSnapshot(key, receivedSerialized, testId);
|
|
294
|
+
if (this.update === "all") {
|
|
295
|
+
if (pass) this.incrementCount("matched", testId);
|
|
296
|
+
else {
|
|
297
|
+
this.incrementCount(hasSnapshot ? "updated" : "added", testId);
|
|
298
|
+
write();
|
|
299
|
+
}
|
|
300
|
+
} else {
|
|
301
|
+
write();
|
|
302
|
+
this.incrementCount("added", testId);
|
|
303
|
+
}
|
|
304
|
+
return {
|
|
305
|
+
pass: true,
|
|
306
|
+
count,
|
|
307
|
+
key,
|
|
308
|
+
actual: "",
|
|
309
|
+
expected: "",
|
|
310
|
+
inlineNotWritten: false
|
|
311
|
+
};
|
|
312
|
+
}
|
|
313
|
+
if (pass) {
|
|
314
|
+
this.incrementCount("matched", testId);
|
|
315
|
+
return {
|
|
316
|
+
pass: true,
|
|
317
|
+
count,
|
|
318
|
+
key,
|
|
319
|
+
actual: "",
|
|
320
|
+
expected: "",
|
|
321
|
+
inlineNotWritten: false
|
|
322
|
+
};
|
|
323
|
+
}
|
|
324
|
+
this.incrementCount("unmatched", testId);
|
|
325
|
+
return this.result(false, count, key, receivedSerialized, expected, false);
|
|
326
|
+
}
|
|
327
|
+
/**
|
|
328
|
+
* Compares a raw string with the contents of a file (`toMatchFileSnapshot`): no serializing, no
|
|
329
|
+
* extra line breaks, an exact compare. `existing` is the file's text, or `undefined` if it
|
|
330
|
+
* doesn't exist.
|
|
331
|
+
*/
|
|
332
|
+
matchRaw(options) {
|
|
333
|
+
const { testName, testId, file, existing } = options;
|
|
334
|
+
const count = this.bumpCounter(testName, testId);
|
|
335
|
+
const key = this.options.key(testName, count);
|
|
336
|
+
this.markKeyChecked(key, testId);
|
|
337
|
+
const received = typeof options.received === "string" ? options.received : this.serialize(options.received);
|
|
338
|
+
const pass = existing === received;
|
|
339
|
+
const hasSnapshot = existing !== void 0;
|
|
340
|
+
const result = (outcome) => ({
|
|
341
|
+
pass: outcome,
|
|
342
|
+
count,
|
|
343
|
+
key,
|
|
344
|
+
actual: outcome ? "" : received,
|
|
345
|
+
expected: outcome ? "" : existing,
|
|
346
|
+
inlineNotWritten: false
|
|
347
|
+
});
|
|
348
|
+
if (hasSnapshot && this.update === "all" || !hasSnapshot && (this.update === "new" || this.update === "all")) {
|
|
349
|
+
if (this.update === "all") {
|
|
350
|
+
if (pass) this.incrementCount("matched", testId);
|
|
351
|
+
else {
|
|
352
|
+
this.incrementCount(hasSnapshot ? "updated" : "added", testId);
|
|
353
|
+
this.addRaw(file, received, testId);
|
|
354
|
+
}
|
|
355
|
+
} else {
|
|
356
|
+
this.addRaw(file, received, testId);
|
|
357
|
+
this.incrementCount("added", testId);
|
|
358
|
+
}
|
|
359
|
+
return result(true);
|
|
360
|
+
}
|
|
361
|
+
if (pass) {
|
|
362
|
+
this.incrementCount("matched", testId);
|
|
363
|
+
return result(true);
|
|
364
|
+
}
|
|
365
|
+
this.incrementCount("unmatched", testId);
|
|
366
|
+
return result(false);
|
|
367
|
+
}
|
|
368
|
+
getUncheckedCount() {
|
|
369
|
+
return this.uncheckedKeys.size;
|
|
370
|
+
}
|
|
371
|
+
getUncheckedKeys() {
|
|
372
|
+
return [...this.uncheckedKeys];
|
|
373
|
+
}
|
|
374
|
+
/** Under `all`, forgets the obsolete snapshots, so the next save drops them. */
|
|
375
|
+
removeUncheckedKeys() {
|
|
376
|
+
if (this.update === "all" && this.uncheckedKeys.size > 0) {
|
|
377
|
+
this.dirty = true;
|
|
378
|
+
for (const key of this.uncheckedKeys) delete this.data[key];
|
|
379
|
+
this.uncheckedKeys.clear();
|
|
380
|
+
}
|
|
381
|
+
}
|
|
382
|
+
/**
|
|
383
|
+
* Writes the `.snap` file (and any raw file) if something changed or something is obsolete,
|
|
384
|
+
* and deletes it under `all` when nothing is left in it.
|
|
385
|
+
*/
|
|
386
|
+
save() {
|
|
387
|
+
const hasSnapshots = Object.keys(this.data).length > 0;
|
|
388
|
+
const hasRaw = this.rawSnapshots.length > 0;
|
|
389
|
+
const isEmpty = !hasSnapshots && !hasRaw && this.inlineEdits.length === 0;
|
|
390
|
+
let saved = false;
|
|
391
|
+
let deleted = false;
|
|
392
|
+
if ((this.dirty || this.uncheckedKeys.size > 0) && !isEmpty) {
|
|
393
|
+
if (hasSnapshots) {
|
|
394
|
+
writeFileAtomically(this.snapshotPath, serializeSnapFile(this.data, this.options.policy.header));
|
|
395
|
+
this.fileExists = true;
|
|
396
|
+
}
|
|
397
|
+
for (const raw of this.rawSnapshots) writeFileAtomically(raw.file, raw.snapshot);
|
|
398
|
+
saved = true;
|
|
399
|
+
} else if (!hasSnapshots && existsSync(this.snapshotPath)) {
|
|
400
|
+
if (this.update === "all") unlinkSync(this.snapshotPath);
|
|
401
|
+
deleted = true;
|
|
402
|
+
}
|
|
403
|
+
return {
|
|
404
|
+
saved,
|
|
405
|
+
deleted
|
|
406
|
+
};
|
|
407
|
+
}
|
|
408
|
+
/**
|
|
409
|
+
* Ends the file: settles what is obsolete, saves, and reports. Call it after the file's skipped
|
|
410
|
+
* and failed tests were marked with `markCheckedForTest`.
|
|
411
|
+
*/
|
|
412
|
+
finish() {
|
|
413
|
+
const uncheckedCount = this.getUncheckedCount();
|
|
414
|
+
const uncheckedKeys = this.getUncheckedKeys();
|
|
415
|
+
if (uncheckedCount > 0) this.removeUncheckedKeys();
|
|
416
|
+
const status = this.save();
|
|
417
|
+
return {
|
|
418
|
+
file: existsSync(this.snapshotPath) ? this.snapshotPath : null,
|
|
419
|
+
added: this.added,
|
|
420
|
+
matched: this.matched,
|
|
421
|
+
unmatched: this.unmatched,
|
|
422
|
+
updated: this.updated,
|
|
423
|
+
unchecked: status.deleted ? 0 : uncheckedCount,
|
|
424
|
+
uncheckedKeys,
|
|
425
|
+
fileDeleted: status.deleted,
|
|
426
|
+
...status.saved && this.inlineEdits.length > 0 ? { inline: [...this.inlineEdits] } : {}
|
|
427
|
+
};
|
|
428
|
+
}
|
|
429
|
+
/** The contents of a raw snapshot file, or `undefined` when it doesn't exist. */
|
|
430
|
+
static readRaw(file) {
|
|
431
|
+
return existsSync(file) ? readFileSync(file, "utf8") : void 0;
|
|
432
|
+
}
|
|
433
|
+
serialize(received) {
|
|
434
|
+
return normalizeNewlines(this.options.printer.serialize(received, {
|
|
435
|
+
format: this.serializeFormat,
|
|
436
|
+
plugins: this.plugins
|
|
437
|
+
}));
|
|
438
|
+
}
|
|
439
|
+
result(pass, count, key, receivedSerialized, expected, inlineNotWritten) {
|
|
440
|
+
return {
|
|
441
|
+
pass,
|
|
442
|
+
count,
|
|
443
|
+
key,
|
|
444
|
+
actual: removeExtraLineBreaks$1(receivedSerialized),
|
|
445
|
+
expected: expected === void 0 ? void 0 : removeExtraLineBreaks$1(expected),
|
|
446
|
+
inlineNotWritten
|
|
447
|
+
};
|
|
448
|
+
}
|
|
449
|
+
recordFor(testId) {
|
|
450
|
+
if (testId === void 0) return void 0;
|
|
451
|
+
let record = this.attempts.get(testId);
|
|
452
|
+
if (record === void 0) {
|
|
453
|
+
record = {
|
|
454
|
+
checkedKeys: /* @__PURE__ */ new Set(),
|
|
455
|
+
counters: /* @__PURE__ */ new Map(),
|
|
456
|
+
counts: {
|
|
457
|
+
added: 0,
|
|
458
|
+
matched: 0,
|
|
459
|
+
unmatched: 0,
|
|
460
|
+
updated: 0
|
|
461
|
+
},
|
|
462
|
+
fileSnapshots: /* @__PURE__ */ new Map(),
|
|
463
|
+
rawSnapshots: [],
|
|
464
|
+
inlineEdits: [],
|
|
465
|
+
writes: void 0
|
|
466
|
+
};
|
|
467
|
+
this.attempts.set(testId, record);
|
|
468
|
+
}
|
|
469
|
+
return record;
|
|
470
|
+
}
|
|
471
|
+
/** A key only reached by a discarded attempt has to go back to being unchecked. */
|
|
472
|
+
markKeyChecked(key, testId) {
|
|
473
|
+
if (this.uncheckedKeys.delete(key)) this.recordFor(testId)?.checkedKeys.add(key);
|
|
474
|
+
}
|
|
475
|
+
/**
|
|
476
|
+
* Counters number keys per full test name, and full names can collide across tests. Undoing an
|
|
477
|
+
* increment could double-undo a collision, so the record keeps each counter's value from before
|
|
478
|
+
* the test first touched it.
|
|
479
|
+
*/
|
|
480
|
+
bumpCounter(testName, testId) {
|
|
481
|
+
const record = this.recordFor(testId);
|
|
482
|
+
if (record !== void 0 && !record.counters.has(testName)) record.counters.set(testName, this.liveCounters.get(testName));
|
|
483
|
+
const count = (this.liveCounters.get(testName) ?? 0) + 1;
|
|
484
|
+
this.liveCounters.set(testName, count);
|
|
485
|
+
return count;
|
|
486
|
+
}
|
|
487
|
+
incrementCount(status, testId) {
|
|
488
|
+
this[status]++;
|
|
489
|
+
const record = this.recordFor(testId);
|
|
490
|
+
if (record !== void 0) record.counts[status]++;
|
|
491
|
+
}
|
|
492
|
+
noteWrite(testId) {
|
|
493
|
+
const record = this.recordFor(testId);
|
|
494
|
+
if (record !== void 0) {
|
|
495
|
+
record.writes ??= {
|
|
496
|
+
dirtyBefore: this.dirty,
|
|
497
|
+
ownWrites: 0,
|
|
498
|
+
writeCountBefore: this.writeCount
|
|
499
|
+
};
|
|
500
|
+
record.writes.ownWrites++;
|
|
501
|
+
}
|
|
502
|
+
this.writeCount++;
|
|
503
|
+
this.dirty = true;
|
|
504
|
+
}
|
|
505
|
+
addSnapshot(key, receivedSerialized, testId) {
|
|
506
|
+
this.noteWrite(testId);
|
|
507
|
+
const record = this.recordFor(testId);
|
|
508
|
+
if (record !== void 0 && !record.fileSnapshots.has(key)) record.fileSnapshots.set(key, this.data[key]);
|
|
509
|
+
this.data[key] = receivedSerialized;
|
|
510
|
+
}
|
|
511
|
+
/**
|
|
512
|
+
* An inline snapshot to write (State.ts L167-L184): where the call is comes from the top frame
|
|
513
|
+
* of its stack, so the coordinator's writer can find it in the source. The stack is taken only
|
|
514
|
+
* here, the one place it is read, still inside the matcher's call.
|
|
515
|
+
*/
|
|
516
|
+
addInline(snapshot, stack, testId) {
|
|
517
|
+
this.noteWrite(testId);
|
|
518
|
+
const frame = stack === void 0 ? void 0 : topFrame(stack());
|
|
519
|
+
if (frame === void 0) {
|
|
520
|
+
const own = this.messages.noInlineFrame?.();
|
|
521
|
+
if (own !== void 0) throw own;
|
|
522
|
+
throw new SnapshotError$1("SNAPSHOT_NO_INLINE_FRAME", "Couldn't infer the stack frame of an inline snapshot, so it can't be written");
|
|
523
|
+
}
|
|
524
|
+
const edit = {
|
|
525
|
+
file: frame.file,
|
|
526
|
+
line: frame.line,
|
|
527
|
+
column: frame.column - 1,
|
|
528
|
+
snapshot
|
|
529
|
+
};
|
|
530
|
+
this.inlineEdits.push(edit);
|
|
531
|
+
this.recordFor(testId)?.inlineEdits.push(edit);
|
|
532
|
+
}
|
|
533
|
+
addRaw(file, snapshot, testId) {
|
|
534
|
+
this.noteWrite(testId);
|
|
535
|
+
const raw = {
|
|
536
|
+
file,
|
|
537
|
+
snapshot
|
|
538
|
+
};
|
|
539
|
+
this.rawSnapshots.push(raw);
|
|
540
|
+
this.recordFor(testId)?.rawSnapshots.push(raw);
|
|
541
|
+
}
|
|
542
|
+
};
|
|
543
|
+
|
|
544
|
+
// src/runtime/open.ts
|
|
545
|
+
/**
|
|
546
|
+
* A printer that refuses: for a policy that names none. Opening the file's state still works, so
|
|
547
|
+
* a file without snapshot assertions runs as before.
|
|
548
|
+
*/
|
|
549
|
+
function missingPrinter() {
|
|
550
|
+
const refuse = () => {
|
|
551
|
+
throw new SnapshotError$1("SNAPSHOT_NO_PRINTER", "This project has no snapshot printer: the snapshot policy names none (`snapshot.printer`), so a snapshot can neither be written nor compared");
|
|
552
|
+
};
|
|
553
|
+
return {
|
|
554
|
+
serialize: refuse,
|
|
555
|
+
messages: {
|
|
556
|
+
mismatch: refuse,
|
|
557
|
+
notWritten: refuse,
|
|
558
|
+
propertiesMismatch: refuse
|
|
559
|
+
}
|
|
560
|
+
};
|
|
561
|
+
}
|
|
562
|
+
/**
|
|
563
|
+
* Opens a test file's snapshots: its state (reading the `.snap` file), the lifecycle that follows
|
|
564
|
+
* the file's events, and `matcherSnapshotState`, what a matcher finds as `this.snapshotState` (the
|
|
565
|
+
* state, or the policy's view of it; `unwrapMatcherSnapshotState` gives the state back).
|
|
566
|
+
* @public
|
|
567
|
+
*/
|
|
568
|
+
function openFileSnapshots(options) {
|
|
569
|
+
const { testPath, policy } = options;
|
|
570
|
+
const snapshotPath = options.resolvePath === null ? defaultSnapshotPath$1(testPath, policy) : options.resolvePath(testPath, policy.extension);
|
|
571
|
+
const state = new SnapshotState({
|
|
572
|
+
testPath,
|
|
573
|
+
snapshotPath,
|
|
574
|
+
update: options.update,
|
|
575
|
+
expand: options.expand,
|
|
576
|
+
policy,
|
|
577
|
+
key: options.key,
|
|
578
|
+
printer: options.printer ?? missingPrinter(),
|
|
579
|
+
inlineWrites: policy.inline.writer !== null,
|
|
580
|
+
currentTestId: options.currentTestId
|
|
581
|
+
});
|
|
582
|
+
return {
|
|
583
|
+
state,
|
|
584
|
+
lifecycle: trackSnapshots(state, { keyJoin: policy.keyJoin }),
|
|
585
|
+
matcherSnapshotState: matcherSnapshotStateOf(state, options.contextView)
|
|
586
|
+
};
|
|
587
|
+
}
|
|
588
|
+
|
|
589
|
+
export { SnapshotError, SnapshotState, deepMerge, defaultSnapshotPath, openFileSnapshots, removeExtraLineBreaks, trackSnapshots, unwrapMatcherSnapshotState };
|
package/package.json
CHANGED
|
@@ -1,6 +1,52 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@provino/snapshot",
|
|
3
|
-
"version": "0.0.0
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "0.1.0-rc.0",
|
|
4
|
+
"description": "Provino's snapshot engine: the .snap file format and the per-file state machine, with the printer and the words supplied by a dialect.",
|
|
5
|
+
"homepage": "https://github.com/pixel-perfect-software/provino#readme",
|
|
6
|
+
"bugs": {
|
|
7
|
+
"url": "https://github.com/pixel-perfect-software/provino/issues"
|
|
8
|
+
},
|
|
9
|
+
"repository": {
|
|
10
|
+
"type": "git",
|
|
11
|
+
"url": "git+https://github.com/pixel-perfect-software/provino.git",
|
|
12
|
+
"directory": "packages/snapshot"
|
|
13
|
+
},
|
|
14
|
+
"license": "MIT",
|
|
15
|
+
"type": "module",
|
|
16
|
+
"sideEffects": false,
|
|
17
|
+
"exports": {
|
|
18
|
+
".": {
|
|
19
|
+
"provino-source": "./src/index.ts",
|
|
20
|
+
"types": "./dist/index.d.ts",
|
|
21
|
+
"import": "./dist/index.js"
|
|
22
|
+
},
|
|
23
|
+
"./node": {
|
|
24
|
+
"provino-source": "./src/node/index.ts",
|
|
25
|
+
"types": "./dist/node/index.d.ts",
|
|
26
|
+
"import": "./dist/node/index.js"
|
|
27
|
+
},
|
|
28
|
+
"./runtime": {
|
|
29
|
+
"provino-source": "./src/runtime/index.ts",
|
|
30
|
+
"types": "./dist/runtime/index.d.ts",
|
|
31
|
+
"import": "./dist/runtime/index.js"
|
|
32
|
+
}
|
|
33
|
+
},
|
|
34
|
+
"files": [
|
|
35
|
+
"dist",
|
|
36
|
+
"THIRD_PARTY_NOTICES.md"
|
|
37
|
+
],
|
|
38
|
+
"engines": {
|
|
39
|
+
"node": "^22.15.0 || >=24"
|
|
40
|
+
},
|
|
41
|
+
"dependencies": {
|
|
42
|
+
"@provino/types": "0.1.0-rc.0"
|
|
43
|
+
},
|
|
44
|
+
"devDependencies": {
|
|
45
|
+
"@provino/policy": "0.1.0-rc.0",
|
|
46
|
+
"natural-compare": "1.4.0"
|
|
47
|
+
},
|
|
48
|
+
"scripts": {
|
|
49
|
+
"build": "tsdown --config ../../tooling/packaging/tsdown.config.ts",
|
|
50
|
+
"test": "node --test --conditions=provino-source \"src/**/*.test.ts\""
|
|
51
|
+
}
|
|
6
52
|
}
|