@cronn/lib-file-snapshots 0.2.0 → 0.4.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 -24
- package/dist/index.js +14 -44
- package/package.json +7 -7
package/dist/index.d.ts
CHANGED
|
@@ -24,13 +24,6 @@ interface SnapshotSerializerResult {
|
|
|
24
24
|
fileExtension: string;
|
|
25
25
|
}
|
|
26
26
|
|
|
27
|
-
declare class CompositeSerializer implements SnapshotSerializer {
|
|
28
|
-
private readonly serializers;
|
|
29
|
-
constructor(serializers: SnapshotSerializer[]);
|
|
30
|
-
canSerialize(value: unknown): boolean;
|
|
31
|
-
serialize(value: unknown): SnapshotSerializerResult;
|
|
32
|
-
}
|
|
33
|
-
|
|
34
27
|
interface JsonSerializerOptions {
|
|
35
28
|
/**
|
|
36
29
|
* Serializes `undefined` properties in objects. By default, they are omitted.
|
|
@@ -63,13 +56,6 @@ declare class TextSerializer implements SnapshotSerializer {
|
|
|
63
56
|
}
|
|
64
57
|
|
|
65
58
|
interface ValidationFileMatcherConfig {
|
|
66
|
-
/**
|
|
67
|
-
* Base directory for tests
|
|
68
|
-
*
|
|
69
|
-
* The paths of snapshot files will be relative to this directory.
|
|
70
|
-
* @default "."
|
|
71
|
-
*/
|
|
72
|
-
baseDir?: string;
|
|
73
59
|
/**
|
|
74
60
|
* Directory in which golden masters are stored
|
|
75
61
|
*
|
|
@@ -85,21 +71,23 @@ interface ValidationFileMatcherConfig {
|
|
|
85
71
|
}
|
|
86
72
|
interface MatchValidationFileOptions {
|
|
87
73
|
/**
|
|
88
|
-
* The full
|
|
74
|
+
* The full path to the current test file
|
|
89
75
|
*
|
|
90
|
-
* @example
|
|
76
|
+
* @example "src/tests/feature.test.ts"
|
|
91
77
|
*/
|
|
92
|
-
|
|
78
|
+
testPath: string;
|
|
93
79
|
/**
|
|
94
|
-
* The
|
|
80
|
+
* The full path of titles describing the current test, including nested blocks
|
|
81
|
+
*
|
|
82
|
+
* @example ["test A", "when x, then y"]
|
|
95
83
|
*/
|
|
96
|
-
|
|
84
|
+
titlePath: string[];
|
|
97
85
|
/**
|
|
98
|
-
*
|
|
86
|
+
* Unique name of the file snapshot
|
|
99
87
|
*
|
|
100
|
-
*
|
|
88
|
+
* Used to distinguish multiple file snapshots within the same `test`.
|
|
101
89
|
*/
|
|
102
|
-
|
|
90
|
+
name?: string;
|
|
103
91
|
/**
|
|
104
92
|
* The serializer to use for the snapshot
|
|
105
93
|
*/
|
|
@@ -110,10 +98,10 @@ interface ValidationFileMatcherResult {
|
|
|
110
98
|
expected: string;
|
|
111
99
|
actualFile: string;
|
|
112
100
|
validationFile: string;
|
|
101
|
+
message: () => string;
|
|
113
102
|
}
|
|
114
103
|
|
|
115
104
|
declare class ValidationFileMatcher {
|
|
116
|
-
private readonly baseDir;
|
|
117
105
|
private readonly validationDir;
|
|
118
106
|
private readonly outputDir;
|
|
119
107
|
constructor(config?: ValidationFileMatcherConfig);
|
|
@@ -122,4 +110,4 @@ declare class ValidationFileMatcher {
|
|
|
122
110
|
private writeFileSnapshots;
|
|
123
111
|
}
|
|
124
112
|
|
|
125
|
-
export {
|
|
113
|
+
export { JsonSerializer, type SnapshotSerializer, TextSerializer, ValidationFileMatcher };
|
package/dist/index.js
CHANGED
|
@@ -1,24 +1,3 @@
|
|
|
1
|
-
// src/serializers/composite-serializer.ts
|
|
2
|
-
var CompositeSerializer = class {
|
|
3
|
-
serializers;
|
|
4
|
-
constructor(serializers) {
|
|
5
|
-
this.serializers = serializers;
|
|
6
|
-
}
|
|
7
|
-
canSerialize(value) {
|
|
8
|
-
return this.serializers.some(
|
|
9
|
-
(serializer) => serializer.canSerialize(value)
|
|
10
|
-
);
|
|
11
|
-
}
|
|
12
|
-
serialize(value) {
|
|
13
|
-
for (const serializer of this.serializers) {
|
|
14
|
-
if (serializer.canSerialize(value)) {
|
|
15
|
-
return serializer.serialize(value);
|
|
16
|
-
}
|
|
17
|
-
}
|
|
18
|
-
throw new Error(`Missing serializer for value of type ${typeof value}.`);
|
|
19
|
-
}
|
|
20
|
-
};
|
|
21
|
-
|
|
22
1
|
// src/utils/guards.ts
|
|
23
2
|
function isArray(value) {
|
|
24
3
|
return Array.isArray(value);
|
|
@@ -204,7 +183,6 @@ var TextSerializer = class {
|
|
|
204
183
|
// src/matcher/validation-file-matcher.ts
|
|
205
184
|
import * as fs2 from "fs";
|
|
206
185
|
import * as path from "path";
|
|
207
|
-
import "vitest";
|
|
208
186
|
|
|
209
187
|
// src/utils/file.ts
|
|
210
188
|
import fs from "fs";
|
|
@@ -234,24 +212,22 @@ function addMissingFileMarker(data) {
|
|
|
234
212
|
|
|
235
213
|
// src/matcher/validation-file-matcher.ts
|
|
236
214
|
var ValidationFileMatcher = class {
|
|
237
|
-
baseDir;
|
|
238
215
|
validationDir;
|
|
239
216
|
outputDir;
|
|
240
217
|
constructor(config = {}) {
|
|
241
|
-
this.baseDir = config.baseDir ?? ".";
|
|
242
218
|
this.validationDir = config.validationDir ?? "data/test/validation";
|
|
243
219
|
this.outputDir = config.outputDir ?? "data/test/output";
|
|
244
220
|
}
|
|
245
221
|
matchFileSnapshot(actual, options) {
|
|
246
|
-
const {
|
|
222
|
+
const { testPath, titlePath, name, serializer } = options;
|
|
247
223
|
if (!serializer.canSerialize(actual)) {
|
|
248
224
|
throw new Error(`Cannot serialize value of type ${typeof actual}`);
|
|
249
225
|
}
|
|
250
226
|
const serializerResult = serializer.serialize(actual);
|
|
251
227
|
const validationFilePath = this.buildValidationFilePath({
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
228
|
+
titlePath,
|
|
229
|
+
testPath,
|
|
230
|
+
name,
|
|
255
231
|
fileExtension: serializerResult.fileExtension
|
|
256
232
|
});
|
|
257
233
|
return this.writeFileSnapshots(
|
|
@@ -260,20 +236,13 @@ var ValidationFileMatcher = class {
|
|
|
260
236
|
);
|
|
261
237
|
}
|
|
262
238
|
buildValidationFilePath(options) {
|
|
263
|
-
const {
|
|
264
|
-
const
|
|
265
|
-
const
|
|
266
|
-
const normalizedTestName =
|
|
267
|
-
const
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
);
|
|
271
|
-
const relativeValidationFilePath = path.relative(
|
|
272
|
-
this.baseDir,
|
|
273
|
-
absoluteValidationFilePath
|
|
274
|
-
);
|
|
275
|
-
const validationFileName = `${normalizedTestName}${normalizedFileSuffix}.${fileExtension}`;
|
|
276
|
-
return path.join(relativeValidationFilePath, validationFileName);
|
|
239
|
+
const { testPath, titlePath, name, fileExtension } = options;
|
|
240
|
+
const normalizedTitlePath = titlePath.map(normalizeFileName);
|
|
241
|
+
const normalizedFileName = name !== void 0 ? `_${normalizeFileName(name)}` : "";
|
|
242
|
+
const normalizedTestName = normalizedTitlePath.pop();
|
|
243
|
+
const validationFilePath = path.join(testPath, ...normalizedTitlePath);
|
|
244
|
+
const validationFileName = `${normalizedTestName}${normalizedFileName}.${fileExtension}`;
|
|
245
|
+
return path.join(validationFilePath, validationFileName);
|
|
277
246
|
}
|
|
278
247
|
writeFileSnapshots(actual, validationFilePath) {
|
|
279
248
|
const validationFileDir = path.dirname(validationFilePath);
|
|
@@ -291,12 +260,13 @@ var ValidationFileMatcher = class {
|
|
|
291
260
|
actual: readSnapshotFile(actualFile),
|
|
292
261
|
expected: readSnapshotFile(validationFile),
|
|
293
262
|
actualFile,
|
|
294
|
-
validationFile
|
|
263
|
+
validationFile,
|
|
264
|
+
message: () => `Actual file '${actualFile}'
|
|
265
|
+
does not match validation file '${validationFile}'`
|
|
295
266
|
};
|
|
296
267
|
}
|
|
297
268
|
};
|
|
298
269
|
export {
|
|
299
|
-
CompositeSerializer,
|
|
300
270
|
JsonSerializer,
|
|
301
271
|
TextSerializer,
|
|
302
272
|
ValidationFileMatcher
|
package/package.json
CHANGED
|
@@ -1,21 +1,21 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cronn/lib-file-snapshots",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.0",
|
|
4
4
|
"description": "The library agnostic core for testing file snapshots",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"file snapshots"
|
|
7
7
|
],
|
|
8
8
|
"bugs": {
|
|
9
|
-
"url": "https://github.com/cronn/
|
|
9
|
+
"url": "https://github.com/cronn/file-snapshots/issues"
|
|
10
10
|
},
|
|
11
11
|
"author": "cronn",
|
|
12
12
|
"license": "Apache-2.0",
|
|
13
13
|
"repository": {
|
|
14
14
|
"type": "git",
|
|
15
|
-
"url": "https://github.com/cronn/
|
|
15
|
+
"url": "https://github.com/cronn/file-snapshots.git",
|
|
16
16
|
"directory": "packages/lib-file-snapshots"
|
|
17
17
|
},
|
|
18
|
-
"homepage": "https://github.com/cronn/
|
|
18
|
+
"homepage": "https://github.com/cronn/file-snapshots",
|
|
19
19
|
"engines": {
|
|
20
20
|
"node": ">=22",
|
|
21
21
|
"pnpm": ">=10"
|
|
@@ -33,11 +33,11 @@
|
|
|
33
33
|
],
|
|
34
34
|
"devDependencies": {
|
|
35
35
|
"@biomejs/biome": "1.9.4",
|
|
36
|
-
"@types/node": "22.15.
|
|
37
|
-
"@vitest/coverage-istanbul": "3.
|
|
36
|
+
"@types/node": "22.15.30",
|
|
37
|
+
"@vitest/coverage-istanbul": "3.2.4",
|
|
38
38
|
"tsup": "8.5.0",
|
|
39
39
|
"typescript": "5.8.3",
|
|
40
|
-
"vitest": "3.
|
|
40
|
+
"vitest": "3.2.4",
|
|
41
41
|
"@cronn/shared-configs": "0.0.0"
|
|
42
42
|
},
|
|
43
43
|
"scripts": {
|