@cronn/lib-file-snapshots 0.2.0 → 0.3.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 +11 -16
- package/dist/index.js +14 -22
- package/package.json +4 -4
package/dist/index.d.ts
CHANGED
|
@@ -63,13 +63,6 @@ declare class TextSerializer implements SnapshotSerializer {
|
|
|
63
63
|
}
|
|
64
64
|
|
|
65
65
|
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
66
|
/**
|
|
74
67
|
* Directory in which golden masters are stored
|
|
75
68
|
*
|
|
@@ -85,21 +78,23 @@ interface ValidationFileMatcherConfig {
|
|
|
85
78
|
}
|
|
86
79
|
interface MatchValidationFileOptions {
|
|
87
80
|
/**
|
|
88
|
-
* The full
|
|
81
|
+
* The full path to the current test file
|
|
89
82
|
*
|
|
90
|
-
* @example
|
|
83
|
+
* @example "src/tests/feature.test.ts"
|
|
91
84
|
*/
|
|
92
|
-
|
|
85
|
+
testPath: string;
|
|
93
86
|
/**
|
|
94
|
-
* The
|
|
87
|
+
* The full path of titles describing the current test, including nested blocks
|
|
88
|
+
*
|
|
89
|
+
* @example ["test A", "when x, then y"]
|
|
95
90
|
*/
|
|
96
|
-
|
|
91
|
+
titlePath: string[];
|
|
97
92
|
/**
|
|
98
|
-
*
|
|
93
|
+
* Unique name of the file snapshot
|
|
99
94
|
*
|
|
100
|
-
*
|
|
95
|
+
* Used to distinguish multiple file snapshots within the same `test`.
|
|
101
96
|
*/
|
|
102
|
-
|
|
97
|
+
name?: string;
|
|
103
98
|
/**
|
|
104
99
|
* The serializer to use for the snapshot
|
|
105
100
|
*/
|
|
@@ -110,10 +105,10 @@ interface ValidationFileMatcherResult {
|
|
|
110
105
|
expected: string;
|
|
111
106
|
actualFile: string;
|
|
112
107
|
validationFile: string;
|
|
108
|
+
message: () => string;
|
|
113
109
|
}
|
|
114
110
|
|
|
115
111
|
declare class ValidationFileMatcher {
|
|
116
|
-
private readonly baseDir;
|
|
117
112
|
private readonly validationDir;
|
|
118
113
|
private readonly outputDir;
|
|
119
114
|
constructor(config?: ValidationFileMatcherConfig);
|
package/dist/index.js
CHANGED
|
@@ -204,7 +204,6 @@ var TextSerializer = class {
|
|
|
204
204
|
// src/matcher/validation-file-matcher.ts
|
|
205
205
|
import * as fs2 from "fs";
|
|
206
206
|
import * as path from "path";
|
|
207
|
-
import "vitest";
|
|
208
207
|
|
|
209
208
|
// src/utils/file.ts
|
|
210
209
|
import fs from "fs";
|
|
@@ -234,24 +233,22 @@ function addMissingFileMarker(data) {
|
|
|
234
233
|
|
|
235
234
|
// src/matcher/validation-file-matcher.ts
|
|
236
235
|
var ValidationFileMatcher = class {
|
|
237
|
-
baseDir;
|
|
238
236
|
validationDir;
|
|
239
237
|
outputDir;
|
|
240
238
|
constructor(config = {}) {
|
|
241
|
-
this.baseDir = config.baseDir ?? ".";
|
|
242
239
|
this.validationDir = config.validationDir ?? "data/test/validation";
|
|
243
240
|
this.outputDir = config.outputDir ?? "data/test/output";
|
|
244
241
|
}
|
|
245
242
|
matchFileSnapshot(actual, options) {
|
|
246
|
-
const {
|
|
243
|
+
const { testPath, titlePath, name, serializer } = options;
|
|
247
244
|
if (!serializer.canSerialize(actual)) {
|
|
248
245
|
throw new Error(`Cannot serialize value of type ${typeof actual}`);
|
|
249
246
|
}
|
|
250
247
|
const serializerResult = serializer.serialize(actual);
|
|
251
248
|
const validationFilePath = this.buildValidationFilePath({
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
249
|
+
titlePath,
|
|
250
|
+
testPath,
|
|
251
|
+
name,
|
|
255
252
|
fileExtension: serializerResult.fileExtension
|
|
256
253
|
});
|
|
257
254
|
return this.writeFileSnapshots(
|
|
@@ -260,20 +257,13 @@ var ValidationFileMatcher = class {
|
|
|
260
257
|
);
|
|
261
258
|
}
|
|
262
259
|
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);
|
|
260
|
+
const { testPath, titlePath, name, fileExtension } = options;
|
|
261
|
+
const normalizedTitlePath = titlePath.map(normalizeFileName);
|
|
262
|
+
const normalizedFileName = name !== void 0 ? `_${normalizeFileName(name)}` : "";
|
|
263
|
+
const normalizedTestName = normalizedTitlePath.pop();
|
|
264
|
+
const validationFilePath = path.join(testPath, ...normalizedTitlePath);
|
|
265
|
+
const validationFileName = `${normalizedTestName}${normalizedFileName}.${fileExtension}`;
|
|
266
|
+
return path.join(validationFilePath, validationFileName);
|
|
277
267
|
}
|
|
278
268
|
writeFileSnapshots(actual, validationFilePath) {
|
|
279
269
|
const validationFileDir = path.dirname(validationFilePath);
|
|
@@ -291,7 +281,9 @@ var ValidationFileMatcher = class {
|
|
|
291
281
|
actual: readSnapshotFile(actualFile),
|
|
292
282
|
expected: readSnapshotFile(validationFile),
|
|
293
283
|
actualFile,
|
|
294
|
-
validationFile
|
|
284
|
+
validationFile,
|
|
285
|
+
message: () => `Actual file '${actualFile}'
|
|
286
|
+
does not match validation file '${validationFile}'`
|
|
295
287
|
};
|
|
296
288
|
}
|
|
297
289
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cronn/lib-file-snapshots",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"description": "The library agnostic core for testing file snapshots",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"file snapshots"
|
|
@@ -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.3",
|
|
38
38
|
"tsup": "8.5.0",
|
|
39
39
|
"typescript": "5.8.3",
|
|
40
|
-
"vitest": "3.
|
|
40
|
+
"vitest": "3.2.3",
|
|
41
41
|
"@cronn/shared-configs": "0.0.0"
|
|
42
42
|
},
|
|
43
43
|
"scripts": {
|