@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 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 name of the test, including the names of nested describe blocks
81
+ * The full path to the current test file
89
82
  *
90
- * @example ["test feature", "when x, then y"]
83
+ * @example "src/tests/feature.test.ts"
91
84
  */
92
- testName: string[];
85
+ testPath: string;
93
86
  /**
94
- * The directory in which the current test is located
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
- testDir: string;
91
+ titlePath: string[];
97
92
  /**
98
- * Appends `fileSuffix` to the generated snapshot file
93
+ * Unique name of the file snapshot
99
94
  *
100
- * Should be used whenever having multiple snapshot assertions in a single `test`.
95
+ * Used to distinguish multiple file snapshots within the same `test`.
101
96
  */
102
- fileSuffix?: string;
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 { testName, testDir, fileSuffix, serializer } = options;
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
- testName,
253
- testDir,
254
- fileSuffix,
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 { testName, testDir, fileSuffix, fileExtension } = options;
264
- const normalizedTestNames = testName.map(normalizeFileName);
265
- const normalizedFileSuffix = fileSuffix !== void 0 ? `_${normalizeFileName(fileSuffix)}` : "";
266
- const normalizedTestName = normalizedTestNames.pop();
267
- const absoluteValidationFilePath = path.join(
268
- testDir,
269
- ...normalizedTestNames
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.2.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.29",
37
- "@vitest/coverage-istanbul": "3.1.4",
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.1.4",
40
+ "vitest": "3.2.3",
41
41
  "@cronn/shared-configs": "0.0.0"
42
42
  },
43
43
  "scripts": {