@cronn/lib-file-snapshots 0.17.0 → 0.19.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/dist/index.d.ts +12 -2
- package/dist/index.js +19 -10
- package/package.json +22 -11
package/dist/index.d.ts
CHANGED
|
@@ -103,7 +103,14 @@ interface ValidationFileMatcherConfig {
|
|
|
103
103
|
* The serializer to use for the snapshot
|
|
104
104
|
*/
|
|
105
105
|
serializer: SnapshotSerializer;
|
|
106
|
+
/**
|
|
107
|
+
* Whether to update golden masters with the actual result.
|
|
108
|
+
*
|
|
109
|
+
* @default "missing"
|
|
110
|
+
*/
|
|
111
|
+
updateSnapshots?: UpdateSnapshotsType;
|
|
106
112
|
}
|
|
113
|
+
type UpdateSnapshotsType = "all" | "missing" | "none";
|
|
107
114
|
interface ValidationFileMatcherResult {
|
|
108
115
|
actual: string;
|
|
109
116
|
expected: string;
|
|
@@ -120,16 +127,19 @@ interface FilePathResolverParams {
|
|
|
120
127
|
}
|
|
121
128
|
|
|
122
129
|
declare class ValidationFileMatcher {
|
|
130
|
+
private readonly updateSnapshots;
|
|
123
131
|
private readonly serializer;
|
|
124
132
|
private readonly filePaths;
|
|
125
|
-
private
|
|
133
|
+
private validationFile;
|
|
126
134
|
constructor(config: ValidationFileMatcherConfig);
|
|
127
135
|
get isValidationFileMissing(): boolean;
|
|
136
|
+
get isUpdate(): boolean;
|
|
128
137
|
matchFileSnapshot(actual: unknown): ValidationFileMatcherResult;
|
|
129
138
|
private buildFilePaths;
|
|
130
139
|
private readValidationFile;
|
|
131
140
|
private createMatcherResult;
|
|
132
141
|
private writeFileSnapshots;
|
|
142
|
+
private resolveExpected;
|
|
133
143
|
}
|
|
134
144
|
|
|
135
145
|
declare function resolveNameAsFile(params: FilePathResolverParams): string;
|
|
@@ -142,4 +152,4 @@ declare function isArray(value: unknown): value is Array<unknown>;
|
|
|
142
152
|
type PlainObject = Record<PropertyKey, unknown>;
|
|
143
153
|
declare function isPlainObject(value: unknown): value is PlainObject;
|
|
144
154
|
|
|
145
|
-
export { type FilePathResolver, type FilePathResolverParams, type JsonNormalizer, type JsonNormalizerContext, JsonSerializer, type PlainObject, type SnapshotSerializer, type TextNormalizer, TextSerializer, ValidationFileMatcher, type ValidationFileMatcherResult, isArray, isPlainObject, isString, normalizeFileName, resolveNameAsFile, resolveNameAsFileSuffix };
|
|
155
|
+
export { type FilePathResolver, type FilePathResolverParams, type JsonNormalizer, type JsonNormalizerContext, JsonSerializer, type PlainObject, type SnapshotSerializer, type TextNormalizer, TextSerializer, type UpdateSnapshotsType, ValidationFileMatcher, type ValidationFileMatcherResult, isArray, isPlainObject, isString, normalizeFileName, resolveNameAsFile, resolveNameAsFileSuffix };
|
package/dist/index.js
CHANGED
|
@@ -250,10 +250,12 @@ var TextSerializer = class {
|
|
|
250
250
|
import * as fs2 from "fs";
|
|
251
251
|
import * as path2 from "path";
|
|
252
252
|
var ValidationFileMatcher = class {
|
|
253
|
+
updateSnapshots;
|
|
253
254
|
serializer;
|
|
254
255
|
filePaths;
|
|
255
256
|
validationFile;
|
|
256
257
|
constructor(config) {
|
|
258
|
+
this.updateSnapshots = config.updateSnapshots ?? "missing";
|
|
257
259
|
this.serializer = config.serializer;
|
|
258
260
|
this.filePaths = this.buildFilePaths(config);
|
|
259
261
|
this.validationFile = this.readValidationFile();
|
|
@@ -261,20 +263,18 @@ var ValidationFileMatcher = class {
|
|
|
261
263
|
get isValidationFileMissing() {
|
|
262
264
|
return this.validationFile === void 0;
|
|
263
265
|
}
|
|
266
|
+
get isUpdate() {
|
|
267
|
+
return this.updateSnapshots === "all" || this.isValidationFileMissing && this.updateSnapshots === "missing";
|
|
268
|
+
}
|
|
264
269
|
matchFileSnapshot(actual) {
|
|
265
270
|
if (!this.serializer.canSerialize(actual)) {
|
|
266
271
|
throw new Error(`Cannot serialize value of type ${typeof actual}`);
|
|
267
272
|
}
|
|
268
273
|
const serializedActual = this.serializer.serialize(actual);
|
|
269
|
-
|
|
270
|
-
return this.createMatcherResult({
|
|
271
|
-
actual: serializedActual,
|
|
272
|
-
expected: addMissingFileMarker(serializedActual)
|
|
273
|
-
});
|
|
274
|
-
}
|
|
274
|
+
const expected = this.resolveExpected(serializedActual);
|
|
275
275
|
return this.createMatcherResult({
|
|
276
276
|
actual: serializedActual,
|
|
277
|
-
expected
|
|
277
|
+
expected
|
|
278
278
|
});
|
|
279
279
|
}
|
|
280
280
|
buildFilePaths(config) {
|
|
@@ -295,12 +295,13 @@ var ValidationFileMatcher = class {
|
|
|
295
295
|
createMatcherResult(params) {
|
|
296
296
|
const { actual, expected } = params;
|
|
297
297
|
const { outputFilePath, validationFilePath } = this.filePaths;
|
|
298
|
+
const isValidationFileMissing = this.isValidationFileMissing;
|
|
298
299
|
return {
|
|
299
300
|
actual,
|
|
300
301
|
expected,
|
|
301
302
|
outputFilePath,
|
|
302
303
|
validationFilePath,
|
|
303
|
-
message: () =>
|
|
304
|
+
message: () => isValidationFileMissing ? `Missing validation file '${validationFilePath}'` : `Output file '${outputFilePath}'
|
|
304
305
|
does not match validation file '${validationFilePath}'`,
|
|
305
306
|
writeFileSnapshots: () => this.writeFileSnapshots(params)
|
|
306
307
|
};
|
|
@@ -309,9 +310,17 @@ does not match validation file '${validationFilePath}'`,
|
|
|
309
310
|
const { actual, expected } = matcherResult;
|
|
310
311
|
const { outputFilePath, validationFilePath } = this.filePaths;
|
|
311
312
|
writeSnapshotFile(outputFilePath, actual);
|
|
312
|
-
if (this.
|
|
313
|
-
|
|
313
|
+
if (this.isUpdate) {
|
|
314
|
+
const validationFileData = this.isValidationFileMissing ? expected : actual;
|
|
315
|
+
writeSnapshotFile(validationFilePath, validationFileData);
|
|
316
|
+
this.validationFile = validationFileData;
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
resolveExpected(actual) {
|
|
320
|
+
if (this.validationFile === void 0) {
|
|
321
|
+
return addMissingFileMarker(actual);
|
|
314
322
|
}
|
|
323
|
+
return this.validationFile;
|
|
315
324
|
}
|
|
316
325
|
};
|
|
317
326
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cronn/lib-file-snapshots",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.19.0",
|
|
4
4
|
"description": "The library agnostic core for testing file snapshots",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"file snapshots"
|
|
@@ -17,8 +17,19 @@
|
|
|
17
17
|
},
|
|
18
18
|
"homepage": "https://github.com/cronn/file-snapshots",
|
|
19
19
|
"engines": {
|
|
20
|
-
"node": ">=
|
|
21
|
-
|
|
20
|
+
"node": "^20 || ^22 || >=24"
|
|
21
|
+
},
|
|
22
|
+
"devEngines": {
|
|
23
|
+
"runtime": {
|
|
24
|
+
"name": "node",
|
|
25
|
+
"version": ">=24.13 <25",
|
|
26
|
+
"onFail": "error"
|
|
27
|
+
},
|
|
28
|
+
"packageManager": {
|
|
29
|
+
"name": "pnpm",
|
|
30
|
+
"version": ">=10.28.2 <11",
|
|
31
|
+
"onFail": "error"
|
|
32
|
+
}
|
|
22
33
|
},
|
|
23
34
|
"type": "module",
|
|
24
35
|
"exports": {
|
|
@@ -32,17 +43,17 @@
|
|
|
32
43
|
"dist"
|
|
33
44
|
],
|
|
34
45
|
"devDependencies": {
|
|
35
|
-
"@trivago/prettier-plugin-sort-imports": "
|
|
36
|
-
"@types/node": "
|
|
37
|
-
"@vitest/coverage-
|
|
38
|
-
"eslint": "9.
|
|
46
|
+
"@trivago/prettier-plugin-sort-imports": "6.0.2",
|
|
47
|
+
"@types/node": "24.10.9",
|
|
48
|
+
"@vitest/coverage-v8": "4.0.17",
|
|
49
|
+
"eslint": "9.39.2",
|
|
39
50
|
"eslint-config-prettier": "10.1.8",
|
|
40
51
|
"eslint-plugin-unused-imports": "4.3.0",
|
|
41
|
-
"prettier": "3.
|
|
42
|
-
"tsup": "8.5.
|
|
52
|
+
"prettier": "3.8.0",
|
|
53
|
+
"tsup": "8.5.1",
|
|
43
54
|
"typescript": "5.9.3",
|
|
44
|
-
"typescript-eslint": "8.
|
|
45
|
-
"vitest": "
|
|
55
|
+
"typescript-eslint": "8.53.1",
|
|
56
|
+
"vitest": "4.0.17",
|
|
46
57
|
"@cronn/shared-configs": "0.0.0"
|
|
47
58
|
},
|
|
48
59
|
"scripts": {
|