@cronn/lib-file-snapshots 0.13.0 → 0.14.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
@@ -92,35 +92,16 @@ interface ValidationFileMatcherConfig {
92
92
  */
93
93
  outputDir?: string;
94
94
  /**
95
- * The full path to the current test file
95
+ * The full path to the snapshot file
96
96
  *
97
- * @example "src/tests/feature.test.ts"
97
+ * @example "src/tests/feature/test/when_x_then_y"
98
98
  */
99
- testPath: string;
100
- /**
101
- * The full path of titles describing the current test, including nested blocks
102
- *
103
- * @example ["test A", "when x, then y"]
104
- */
105
- titlePath: Array<string>;
106
- /**
107
- * Unique name of the file snapshot
108
- *
109
- * Used to distinguish multiple file snapshots within the same `test`.
110
- */
111
- name?: string;
112
- /**
113
- * The naming strategy to use for storing the file snapshot
114
- *
115
- * @default "file"
116
- */
117
- namingStrategy?: SnapshotNamingStrategy;
99
+ filePath: string;
118
100
  /**
119
101
  * The serializer to use for the snapshot
120
102
  */
121
103
  serializer: SnapshotSerializer;
122
104
  }
123
- type SnapshotNamingStrategy = "file" | "fileSuffix";
124
105
  interface ValidationFileMatcherResult {
125
106
  actual: string;
126
107
  expected: string;
@@ -129,6 +110,12 @@ interface ValidationFileMatcherResult {
129
110
  message: () => string;
130
111
  writeFileSnapshots: () => void;
131
112
  }
113
+ type FilePathResolver = (params: FilePathResolverParams) => string;
114
+ interface FilePathResolverParams {
115
+ testPath: string;
116
+ titlePath: Array<string>;
117
+ name?: string;
118
+ }
132
119
 
133
120
  declare class ValidationFileMatcher {
134
121
  private readonly serializer;
@@ -138,15 +125,19 @@ declare class ValidationFileMatcher {
138
125
  get isValidationFileMissing(): boolean;
139
126
  matchFileSnapshot(actual: unknown): ValidationFileMatcherResult;
140
127
  private buildFilePaths;
141
- private applyNamingStrategy;
142
128
  private readValidationFile;
143
129
  private createMatcherResult;
144
130
  private writeFileSnapshots;
145
131
  }
146
132
 
133
+ declare function resolveNameAsFile(params: FilePathResolverParams): string;
134
+ declare function resolveNameAsFileSuffix(params: FilePathResolverParams): string;
135
+
136
+ declare function normalizeFileName(testName: string): string;
137
+
147
138
  declare function isString(value: unknown): value is string;
148
139
  declare function isArray(value: unknown): value is Array<unknown>;
149
140
  type PlainObject = Record<PropertyKey, unknown>;
150
141
  declare function isPlainObject(value: unknown): value is PlainObject;
151
142
 
152
- export { type JsonNormalizer, type JsonNormalizerContext, JsonSerializer, type PlainObject, type SnapshotNamingStrategy, type SnapshotSerializer, type TextNormalizer, TextSerializer, ValidationFileMatcher, type ValidationFileMatcherResult, isArray, isPlainObject, isString };
143
+ 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 };
package/dist/index.js CHANGED
@@ -6,15 +6,15 @@ var EMOJI_REGEXP = /(?!(\*|#|\d))[\p{Extended_Pictographic}\p{Emoji_Component}]|
6
6
  function normalizeFileName(testName) {
7
7
  return testName.replaceAll(EMOJI_REGEXP, "").replaceAll(/[+*%~<>?!$#'"`|\\/()[\]{}]/g, "").replaceAll(/[\s.:,;]+/g, "_").replaceAll(/_{2,}/g, "_");
8
8
  }
9
- function readSnapshotFile(path3) {
10
- return fs.readFileSync(path3, { encoding: "utf8" });
9
+ function readSnapshotFile(path4) {
10
+ return fs.readFileSync(path4, { encoding: "utf8" });
11
11
  }
12
12
  function writeSnapshotFile(file, data) {
13
13
  mkdirRecursive(path.dirname(file));
14
14
  fs.writeFileSync(file, data, { encoding: "utf8" });
15
15
  }
16
- function mkdirRecursive(path3) {
17
- fs.mkdirSync(path3, { recursive: true });
16
+ function mkdirRecursive(path4) {
17
+ fs.mkdirSync(path4, { recursive: true });
18
18
  }
19
19
  function addTrailingNewLine(data) {
20
20
  return `${data}${NEW_LINE_SEPARATOR}`;
@@ -274,32 +274,12 @@ var ValidationFileMatcher = class {
274
274
  buildFilePaths(config) {
275
275
  const validationDir = config.validationDir ?? "data/test/validation";
276
276
  const outputDir = config.outputDir ?? "data/test/output";
277
- const normalizedTitlePath = config.titlePath.map(normalizeFileName);
278
- const relativeFilePath = path2.join(config.testPath, ...normalizedTitlePath);
279
- const relativeFilePathWithName = this.applyNamingStrategy(
280
- relativeFilePath,
281
- config.name,
282
- config.namingStrategy
283
- );
284
- const relativeFilePathWithExtension = `${relativeFilePathWithName}.${this.serializer.fileExtension}`;
277
+ const filePathWithExtenion = `${config.filePath}.${config.serializer.fileExtension}`;
285
278
  return {
286
- outputFilePath: path2.join(outputDir, relativeFilePathWithExtension),
287
- validationFilePath: path2.join(
288
- validationDir,
289
- relativeFilePathWithExtension
290
- )
279
+ outputFilePath: path2.join(outputDir, filePathWithExtenion),
280
+ validationFilePath: path2.join(validationDir, filePathWithExtenion)
291
281
  };
292
282
  }
293
- applyNamingStrategy(filePath, snapshotName, namingStrategy = "file") {
294
- if (snapshotName === void 0) {
295
- return filePath;
296
- }
297
- const normalizedSnapshotName = normalizeFileName(snapshotName);
298
- if (namingStrategy === "fileSuffix") {
299
- return `${filePath}_${normalizedSnapshotName}`;
300
- }
301
- return path2.join(filePath, normalizedSnapshotName);
302
- }
303
283
  readValidationFile() {
304
284
  const { validationFilePath } = this.filePaths;
305
285
  if (!fs2.existsSync(validationFilePath)) {
@@ -329,11 +309,43 @@ does not match validation file '${validationFilePath}'`,
329
309
  }
330
310
  }
331
311
  };
312
+
313
+ // src/matcher/file-path-resolver.ts
314
+ import path3 from "path";
315
+ function resolveNameAsFile(params) {
316
+ return resolveFilePath(
317
+ params,
318
+ ({ basePath, normalizedName }) => path3.join(basePath, normalizedName)
319
+ );
320
+ }
321
+ function resolveNameAsFileSuffix(params) {
322
+ return resolveFilePath(
323
+ params,
324
+ ({ basePath, normalizedName }) => `${basePath}_${normalizedName}`
325
+ );
326
+ }
327
+ function resolveBasePath(params) {
328
+ const { testPath, titlePath } = params;
329
+ const normalizedTitlePath = titlePath.map(normalizeFileName);
330
+ return path3.join(testPath, ...normalizedTitlePath);
331
+ }
332
+ function resolveFilePath(params, resolveNamedFilePath) {
333
+ const { name, ...baseParams } = params;
334
+ const basePath = resolveBasePath(baseParams);
335
+ if (name === void 0) {
336
+ return basePath;
337
+ }
338
+ const normalizedName = normalizeFileName(name);
339
+ return resolveNamedFilePath({ basePath, normalizedName });
340
+ }
332
341
  export {
333
342
  JsonSerializer,
334
343
  TextSerializer,
335
344
  ValidationFileMatcher,
336
345
  isArray,
337
346
  isPlainObject,
338
- isString
347
+ isString,
348
+ normalizeFileName,
349
+ resolveNameAsFile,
350
+ resolveNameAsFileSuffix
339
351
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cronn/lib-file-snapshots",
3
- "version": "0.13.0",
3
+ "version": "0.14.0",
4
4
  "description": "The library agnostic core for testing file snapshots",
5
5
  "keywords": [
6
6
  "file snapshots"
@@ -33,15 +33,15 @@
33
33
  ],
34
34
  "devDependencies": {
35
35
  "@trivago/prettier-plugin-sort-imports": "5.2.2",
36
- "@types/node": "22.17.1",
36
+ "@types/node": "22.18.0",
37
37
  "@vitest/coverage-istanbul": "3.2.4",
38
- "eslint": "9.32.0",
38
+ "eslint": "9.34.0",
39
39
  "eslint-config-prettier": "10.1.8",
40
- "eslint-plugin-unused-imports": "4.1.4",
40
+ "eslint-plugin-unused-imports": "4.2.0",
41
41
  "prettier": "3.6.2",
42
42
  "tsup": "8.5.0",
43
- "typescript": "5.8.3",
44
- "typescript-eslint": "8.38.0",
43
+ "typescript": "5.9.2",
44
+ "typescript-eslint": "8.42.0",
45
45
  "vitest": "3.2.4",
46
46
  "@cronn/shared-configs": "0.0.0"
47
47
  },