@cronn/lib-file-snapshots 0.11.0 → 0.13.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 +8 -1
- package/dist/index.js +19 -7
- package/package.json +3 -4
package/dist/index.d.ts
CHANGED
|
@@ -30,6 +30,12 @@ interface JsonSerializerOptions {
|
|
|
30
30
|
* Custom normalizers to apply before serialization
|
|
31
31
|
*/
|
|
32
32
|
normalizers?: Array<JsonNormalizer>;
|
|
33
|
+
/**
|
|
34
|
+
* Indentation size in spaces
|
|
35
|
+
*
|
|
36
|
+
* @default 2
|
|
37
|
+
*/
|
|
38
|
+
indentSize?: number;
|
|
33
39
|
}
|
|
34
40
|
type JsonNormalizer = (value: unknown, context: JsonNormalizerContext) => unknown;
|
|
35
41
|
interface JsonNormalizerContext {
|
|
@@ -40,6 +46,7 @@ declare class JsonSerializer implements SnapshotSerializer {
|
|
|
40
46
|
readonly fileExtension = "json";
|
|
41
47
|
private readonly includeUndefinedObjectProperties;
|
|
42
48
|
private readonly normalizers;
|
|
49
|
+
private readonly indentSize;
|
|
43
50
|
constructor(options?: JsonSerializerOptions);
|
|
44
51
|
canSerialize(_value: unknown): boolean;
|
|
45
52
|
serialize(value: unknown): string;
|
|
@@ -115,7 +122,6 @@ interface ValidationFileMatcherConfig {
|
|
|
115
122
|
}
|
|
116
123
|
type SnapshotNamingStrategy = "file" | "fileSuffix";
|
|
117
124
|
interface ValidationFileMatcherResult {
|
|
118
|
-
isValidationFileMissing: boolean;
|
|
119
125
|
actual: string;
|
|
120
126
|
expected: string;
|
|
121
127
|
outputFilePath: string;
|
|
@@ -129,6 +135,7 @@ declare class ValidationFileMatcher {
|
|
|
129
135
|
private readonly filePaths;
|
|
130
136
|
private readonly validationFile;
|
|
131
137
|
constructor(config: ValidationFileMatcherConfig);
|
|
138
|
+
get isValidationFileMissing(): boolean;
|
|
132
139
|
matchFileSnapshot(actual: unknown): ValidationFileMatcherResult;
|
|
133
140
|
private buildFilePaths;
|
|
134
141
|
private applyNamingStrategy;
|
package/dist/index.js
CHANGED
|
@@ -41,21 +41,33 @@ function isPlainObject(value) {
|
|
|
41
41
|
return proto === null || proto === Object.prototype;
|
|
42
42
|
}
|
|
43
43
|
|
|
44
|
+
// src/utils/validators.ts
|
|
45
|
+
function isPositiveInteger(value) {
|
|
46
|
+
return Number.isInteger(value) && value > 0;
|
|
47
|
+
}
|
|
48
|
+
|
|
44
49
|
// src/serializers/json-serializer.ts
|
|
45
50
|
var JsonSerializer = class {
|
|
46
51
|
fileExtension = "json";
|
|
47
52
|
includeUndefinedObjectProperties;
|
|
48
53
|
normalizers;
|
|
54
|
+
indentSize;
|
|
49
55
|
constructor(options = {}) {
|
|
56
|
+
if (options.indentSize !== void 0 && !isPositiveInteger(options.indentSize)) {
|
|
57
|
+
throw new Error(
|
|
58
|
+
"Invalid option indentSize: value must be a positive integer."
|
|
59
|
+
);
|
|
60
|
+
}
|
|
50
61
|
this.includeUndefinedObjectProperties = options.includeUndefinedObjectProperties ?? false;
|
|
51
62
|
this.normalizers = options.normalizers ?? [];
|
|
63
|
+
this.indentSize = options.indentSize ?? 2;
|
|
52
64
|
}
|
|
53
65
|
canSerialize(_value) {
|
|
54
66
|
return true;
|
|
55
67
|
}
|
|
56
68
|
serialize(value) {
|
|
57
69
|
const jsonValue = this.normalizeValueRecursive(value);
|
|
58
|
-
const jsonString = JSON.stringify(jsonValue, void 0,
|
|
70
|
+
const jsonString = JSON.stringify(jsonValue, void 0, this.indentSize);
|
|
59
71
|
return addTrailingNewLine(jsonString);
|
|
60
72
|
}
|
|
61
73
|
normalizeValueRecursive(value, context) {
|
|
@@ -240,6 +252,9 @@ var ValidationFileMatcher = class {
|
|
|
240
252
|
this.filePaths = this.buildFilePaths(config);
|
|
241
253
|
this.validationFile = this.readValidationFile();
|
|
242
254
|
}
|
|
255
|
+
get isValidationFileMissing() {
|
|
256
|
+
return this.validationFile === void 0;
|
|
257
|
+
}
|
|
243
258
|
matchFileSnapshot(actual) {
|
|
244
259
|
if (!this.serializer.canSerialize(actual)) {
|
|
245
260
|
throw new Error(`Cannot serialize value of type ${typeof actual}`);
|
|
@@ -247,13 +262,11 @@ var ValidationFileMatcher = class {
|
|
|
247
262
|
const serializedActual = this.serializer.serialize(actual);
|
|
248
263
|
if (this.validationFile === void 0) {
|
|
249
264
|
return this.createMatcherResult({
|
|
250
|
-
isValidationFileMissing: true,
|
|
251
265
|
actual: serializedActual,
|
|
252
266
|
expected: addMissingFileMarker(serializedActual)
|
|
253
267
|
});
|
|
254
268
|
}
|
|
255
269
|
return this.createMatcherResult({
|
|
256
|
-
isValidationFileMissing: false,
|
|
257
270
|
actual: serializedActual,
|
|
258
271
|
expected: this.validationFile
|
|
259
272
|
});
|
|
@@ -295,15 +308,14 @@ var ValidationFileMatcher = class {
|
|
|
295
308
|
return readSnapshotFile(validationFilePath);
|
|
296
309
|
}
|
|
297
310
|
createMatcherResult(params) {
|
|
298
|
-
const {
|
|
311
|
+
const { actual, expected } = params;
|
|
299
312
|
const { outputFilePath, validationFilePath } = this.filePaths;
|
|
300
313
|
return {
|
|
301
|
-
isValidationFileMissing,
|
|
302
314
|
actual,
|
|
303
315
|
expected,
|
|
304
316
|
outputFilePath,
|
|
305
317
|
validationFilePath,
|
|
306
|
-
message: () => isValidationFileMissing ? `Missing validation file '${validationFilePath}'` : `Output file '${outputFilePath}'
|
|
318
|
+
message: () => this.isValidationFileMissing ? `Missing validation file '${validationFilePath}'` : `Output file '${outputFilePath}'
|
|
307
319
|
does not match validation file '${validationFilePath}'`,
|
|
308
320
|
writeFileSnapshots: () => this.writeFileSnapshots(params)
|
|
309
321
|
};
|
|
@@ -312,7 +324,7 @@ does not match validation file '${validationFilePath}'`,
|
|
|
312
324
|
const { actual, expected } = matcherResult;
|
|
313
325
|
const { outputFilePath, validationFilePath } = this.filePaths;
|
|
314
326
|
writeSnapshotFile(outputFilePath, actual);
|
|
315
|
-
if (
|
|
327
|
+
if (this.isValidationFileMissing) {
|
|
316
328
|
writeSnapshotFile(validationFilePath, expected);
|
|
317
329
|
}
|
|
318
330
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cronn/lib-file-snapshots",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.13.0",
|
|
4
4
|
"description": "The library agnostic core for testing file snapshots",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"file snapshots"
|
|
@@ -17,7 +17,7 @@
|
|
|
17
17
|
},
|
|
18
18
|
"homepage": "https://github.com/cronn/file-snapshots",
|
|
19
19
|
"engines": {
|
|
20
|
-
"node": ">=22",
|
|
20
|
+
"node": ">=22.18",
|
|
21
21
|
"pnpm": ">=10"
|
|
22
22
|
},
|
|
23
23
|
"type": "module",
|
|
@@ -33,12 +33,11 @@
|
|
|
33
33
|
],
|
|
34
34
|
"devDependencies": {
|
|
35
35
|
"@trivago/prettier-plugin-sort-imports": "5.2.2",
|
|
36
|
-
"@types/node": "22.
|
|
36
|
+
"@types/node": "22.17.1",
|
|
37
37
|
"@vitest/coverage-istanbul": "3.2.4",
|
|
38
38
|
"eslint": "9.32.0",
|
|
39
39
|
"eslint-config-prettier": "10.1.8",
|
|
40
40
|
"eslint-plugin-unused-imports": "4.1.4",
|
|
41
|
-
"jiti": "2.5.1",
|
|
42
41
|
"prettier": "3.6.2",
|
|
43
42
|
"tsup": "8.5.0",
|
|
44
43
|
"typescript": "5.8.3",
|