@cronn/element-snapshot 0.28.0 → 0.30.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.mts CHANGED
@@ -158,7 +158,7 @@ type NodeSnapshot = ElementSnapshot | TextSnapshot;
158
158
  type SnapshotByRole<TRole extends NodeRole> = NodeSnapshot & {
159
159
  role: TRole;
160
160
  };
161
- interface GenericElementSnapshot<TRole extends NodeRole = NodeRole, TAttributes = Record<string, unknown>> {
161
+ interface GenericElementSnapshot<TRole extends ElementRole = ElementRole, TAttributes extends object = object> {
162
162
  role: TRole;
163
163
  name?: string;
164
164
  attributes: TAttributes;
@@ -166,6 +166,15 @@ interface GenericElementSnapshot<TRole extends NodeRole = NodeRole, TAttributes
166
166
  }
167
167
  type ElementSnapshot = ButtonSnapshot | CellSnapshot | ColumnHeaderSnapshot | ComboboxSnapshot | ContainerSnapshot | DialogSnapshot | GroupSnapshot | HeadingSnapshot | ImageSnapshot | InputSnapshot | LinkSnapshot | OptionSnapshot | ProgressbarSnapshot | RegionSnapshot | TabSnapshot | MenuItemSnapshot | RadioGroupSnapshot | SeparatorSnapshot | TreeItemSnapshot;
168
168
  //#endregion
169
+ //#region src/types/transformer.d.ts
170
+ type SnapshotTransformer = (snapshot: NodeSnapshot) => unknown;
171
+ type RoleBasedSnapshotTransformer<TRole extends NodeRole = NodeRole> = (snapshot: SnapshotByRole<TRole>, context: SnapshotTransformerContext) => unknown;
172
+ type DefaultSnapshotTransformer = (snapshot: NodeSnapshot, context: SnapshotTransformerContext) => unknown;
173
+ interface SnapshotTransformerContext {
174
+ transform: SnapshotTransformer;
175
+ }
176
+ type RoleBasedSnapshotTransformers = { [TRole in NodeRole]?: RoleBasedSnapshotTransformer<TRole>; };
177
+ //#endregion
169
178
  //#region src/utils/filter.d.ts
170
179
  type FilterPredicate = (snapshot: NodeSnapshot) => boolean;
171
180
  type GuardedFilterPredicate<TSnapshot extends NodeSnapshot> = (snapshot: NodeSnapshot) => snapshot is TSnapshot;
@@ -224,11 +233,29 @@ interface SemanticSnapshotOptions {
224
233
  */
225
234
  recurseFilter?: boolean;
226
235
  /**
227
- * Include combobox options in the snapshot
236
+ * Replace the default transformation for specific roles
228
237
  *
229
- * @default false
238
+ * Transformers are keyed by role and applied after the filter, but before
239
+ * the default serialization. A transformer may return any JSON-serializable
240
+ * value, which is written to the snapshot verbatim. The `transform` function
241
+ * of its context applies the default transformation to untransformed
242
+ * children.
243
+ *
244
+ * Entries override the built-in transformers for the same role.
245
+ *
246
+ * @default { combobox: comboboxTransformer() }
247
+ */
248
+ transformers?: RoleBasedSnapshotTransformers;
249
+ /**
250
+ * Replace the default transformation for all roles
251
+ *
252
+ * Applied to every snapshot passed to the default transformation, including
253
+ * the ones a transformer delegates via the `transform` function of its
254
+ * context. Transformers registered for a role take precedence.
255
+ *
256
+ * @default semanticSnapshotTransformer
230
257
  */
231
- includeComboboxOptions?: boolean;
258
+ defaultTransformer?: DefaultSnapshotTransformer;
232
259
  }
233
260
  /**
234
261
  * Captures a semantic snapshot of the specified locator's DOM structure.
@@ -269,6 +296,23 @@ interface MatchMarkdownTableSnapshotFileOptions extends Omit<PlaywrightMatchText
269
296
  */
270
297
  declare function defineElementSnapshotMatchers(config?: PlaywrightValidationFileMatcherConfig): Expect<ElementSnapshotMatchers>;
271
298
  //#endregion
299
+ //#region src/transformers/combobox-transformer.d.ts
300
+ interface ComboboxTransformerOptions {
301
+ /**
302
+ * Include combobox options in the snapshot
303
+ *
304
+ * @default false
305
+ */
306
+ includeOptions?: boolean;
307
+ }
308
+ /**
309
+ * Returns a transformer controlling whether the options of a combobox are
310
+ * included in the snapshot
311
+ *
312
+ * Empty option lists are always omitted.
313
+ */
314
+ declare function comboboxTransformer(options?: ComboboxTransformerOptions): RoleBasedSnapshotTransformer<"combobox">;
315
+ //#endregion
272
316
  //#region src/utils/predicates.d.ts
273
317
  /**
274
318
  * Include only elements with the specified role
@@ -285,4 +329,4 @@ declare function excludeRole<TRole extends NodeRole>(role: TRole | Array<TRole>)
285
329
  */
286
330
  declare function getTextContent(snapshots: Array<NodeSnapshot>): string;
287
331
  //#endregion
288
- export { type ButtonSnapshot, type CellSnapshot, type ColumnHeaderSnapshot, type ComboboxSnapshot, type ContainerSnapshot, type DialogSnapshot, type ElementRole, type ElementSnapshot, type ElementSnapshotMatchers, type GroupSnapshot, type HeadingSnapshot, type ImageSnapshot, type InputSnapshot, type LinkSnapshot, type MarkdownTableSnapshotOptions, type MatchMarkdownTableSnapshotFileOptions, type MatchSemanticSnapshotFileOptions, type MenuItemSnapshot, type NodeRole, type NodeSnapshot, type OptionSnapshot, type ProgressbarSnapshot, type RadioGroupSnapshot, type RegionSnapshot, type SemanticSnapshotOptions, type SeparatorSnapshot, type SnapshotByRole, type TabSnapshot, type TextSnapshot, type TreeItemSnapshot, defineElementSnapshotMatchers, excludeRole, filter, filterByRole, getTextContent, includeRole, markdownTableSnapshot, rawSnapshot, semanticSnapshot };
332
+ export { type ButtonSnapshot, type CellSnapshot, type ColumnHeaderSnapshot, type ComboboxSnapshot, type ComboboxTransformerOptions, type ContainerSnapshot, type DefaultSnapshotTransformer, type DialogSnapshot, type ElementRole, type ElementSnapshot, type ElementSnapshotMatchers, type GroupSnapshot, type HeadingSnapshot, type ImageSnapshot, type InputSnapshot, type LinkSnapshot, type MarkdownTableSnapshotOptions, type MatchMarkdownTableSnapshotFileOptions, type MatchSemanticSnapshotFileOptions, type MenuItemSnapshot, type NodeRole, type NodeSnapshot, type OptionSnapshot, type ProgressbarSnapshot, type RadioGroupSnapshot, type RegionSnapshot, type RoleBasedSnapshotTransformer, type RoleBasedSnapshotTransformers, type SemanticSnapshotOptions, type SeparatorSnapshot, type SnapshotByRole, type SnapshotTransformer, type SnapshotTransformerContext, type TabSnapshot, type TextSnapshot, type TreeItemSnapshot, comboboxTransformer, defineElementSnapshotMatchers, excludeRole, filter, filterByRole, getTextContent, includeRole, markdownTableSnapshot, rawSnapshot, semanticSnapshot };
package/dist/index.mjs CHANGED
@@ -1,6 +1,7 @@
1
1
  import { expect } from "@playwright/test";
2
2
  import { defineJsonFileMatcher, defineTextFileMatcher } from "@cronn/playwright-file-snapshots";
3
3
  import { MarkdownTableSerializer } from "@cronn/lib-file-snapshots";
4
+ import fs from "node:fs/promises";
4
5
  import path from "node:path";
5
6
  import { packageDirectory } from "package-directory";
6
7
  //#region src/utils/guards.ts
@@ -65,7 +66,8 @@ function hasChildren(snapshot) {
65
66
  }
66
67
  //#endregion
67
68
  //#region src/playwright/proxy.ts
68
- var ElementSnapshotProxy = class {
69
+ var ElementSnapshotProxy = class ElementSnapshotProxy {
70
+ static browserLibSource = void 0;
69
71
  page;
70
72
  constructor(page) {
71
73
  this.page = page;
@@ -80,30 +82,120 @@ var ElementSnapshotProxy = class {
80
82
  return await this.page.evaluate(() => typeof window.snapshotElement === "function");
81
83
  }
82
84
  async loadLibrary() {
83
- const packageRootDir = await this.resolvePackageRootDir();
84
- const libPath = path.resolve(packageRootDir, "dist", "browser-lib.iife.js");
85
- await this.page.addScriptTag({ path: libPath });
85
+ ElementSnapshotProxy.browserLibSource ??= await this.readBrowserLibSource();
86
+ await this.page.evaluate(ElementSnapshotProxy.browserLibSource);
86
87
  }
87
- async resolvePackageRootDir() {
88
- const packageDir = await packageDirectory({ cwd: import.meta.dirname });
89
- if (packageDir === void 0) throw new Error("Unable to resolve root directory of package");
90
- return packageDir;
88
+ async readBrowserLibSource() {
89
+ const libPath = await this.resolveFromPackageRoot("dist", "browser-lib.iife.js");
90
+ return await fs.readFile(libPath, "utf8");
91
+ }
92
+ async resolveFromPackageRoot(...paths) {
93
+ const packageRoot = await packageDirectory({ cwd: import.meta.dirname });
94
+ if (packageRoot === void 0) throw new Error("Unable to resolve root directory of package");
95
+ return path.resolve(packageRoot, ...paths);
91
96
  }
92
97
  };
93
98
  //#endregion
94
- //#region src/playwright/semantic-snapshot-transformer.ts
95
- var SemanticSnapshotTransformer = class {
99
+ //#region src/transformers/semantic-snapshot-transformer.ts
100
+ function semanticSnapshotTransformer(snapshot, context) {
101
+ if (snapshot.role === "text") return snapshot.name;
102
+ return transformElementSnapshot(snapshot, context.transform);
103
+ }
104
+ function transformElementSnapshot(snapshot, transformRecursive) {
105
+ const normalizedSnapshot = normalizeElementSnapshot(snapshot, transformRecursive);
106
+ if (hasOnlyRole(normalizedSnapshot)) {
107
+ const { role } = normalizedSnapshot;
108
+ return transformedSnapshot(role, "");
109
+ }
110
+ if (hasOnlyName(normalizedSnapshot)) {
111
+ const { role, name } = normalizedSnapshot;
112
+ return transformedSnapshot(role, name);
113
+ }
114
+ if (hasOnlyChildren(normalizedSnapshot)) {
115
+ const { role, children } = normalizedSnapshot;
116
+ return transformedSnapshot(role, children.length === 1 ? children.at(0) : children);
117
+ }
118
+ const { role, name, attributes, children } = normalizedSnapshot;
119
+ return transformedSnapshot(role, name, {
120
+ ...attributes,
121
+ children: children.length === 0 ? void 0 : children
122
+ });
123
+ }
124
+ function normalizeElementSnapshot(snapshot, transformRecursive) {
125
+ const normalizedName = snapshot.name ?? "";
126
+ const transformedAttributes = transformAttributes(snapshot.attributes ?? {});
127
+ const transformedChildren = (snapshot.children ?? []).map(transformRecursive);
128
+ const nameEqualsChildren = transformedChildren.length === 1 && normalizedName === transformedChildren.at(0);
129
+ return {
130
+ role: snapshot.role,
131
+ name: normalizedName,
132
+ attributes: transformedAttributes,
133
+ children: nameEqualsChildren ? [] : transformedChildren
134
+ };
135
+ }
136
+ function hasOnlyRole(snapshot) {
137
+ return isEmpty(snapshot.name) && isEmpty(snapshot.attributes) && isEmpty(snapshot.children);
138
+ }
139
+ function hasOnlyName(snapshot) {
140
+ return !isEmpty(snapshot.name) && isEmpty(snapshot.attributes) && isEmpty(snapshot.children);
141
+ }
142
+ function hasOnlyChildren(snapshot) {
143
+ return isEmpty(snapshot.name) && isEmpty(snapshot.attributes) && !isEmpty(snapshot.children);
144
+ }
145
+ function transformAttributes(attributes) {
146
+ const filteredAttributes = {};
147
+ Object.entries(attributes).forEach(([attributeName, attributeValue]) => {
148
+ if (attributeValue === void 0) return;
149
+ filteredAttributes[attributeName] = attributeValue;
150
+ });
151
+ return filteredAttributes;
152
+ }
153
+ function transformedSnapshot(role, nameOrContent, additionalProperties) {
154
+ return {
155
+ [role]: nameOrContent,
156
+ ...additionalProperties
157
+ };
158
+ }
159
+ //#endregion
160
+ //#region src/transformers/combobox-transformer.ts
161
+ /**
162
+ * Returns a transformer controlling whether the options of a combobox are
163
+ * included in the snapshot
164
+ *
165
+ * Empty option lists are always omitted.
166
+ */
167
+ function comboboxTransformer(options = {}) {
168
+ const { includeOptions = false } = options;
169
+ return (snapshot, { transform }) => {
170
+ const { options: optionSnapshots, ...attributes } = snapshot.attributes;
171
+ const transformedOptions = optionSnapshots.map(transform);
172
+ return transformElementSnapshot({
173
+ ...snapshot,
174
+ attributes: includeOptions && transformedOptions.length > 0 ? {
175
+ ...attributes,
176
+ options: transformedOptions
177
+ } : attributes
178
+ }, transform);
179
+ };
180
+ }
181
+ //#endregion
182
+ //#region src/playwright/snapshot-serializer.ts
183
+ var SnapshotSerializer = class {
96
184
  filter;
97
185
  recurseFilter;
98
- includeComboboxOptions;
186
+ transformers;
187
+ defaultTransformer;
99
188
  constructor(options = {}) {
100
189
  this.filter = options.filter;
101
190
  this.recurseFilter = options.recurseFilter ?? false;
102
- this.includeComboboxOptions = options.includeComboboxOptions ?? false;
191
+ this.transformers = {
192
+ combobox: comboboxTransformer(),
193
+ ...options.transformers
194
+ };
195
+ this.defaultTransformer = options.defaultTransformer ?? semanticSnapshotTransformer;
103
196
  }
104
197
  transform(snapshots) {
105
- const filteredSnapshots = this.filterSnapshots(snapshots);
106
- const transformedSnapshots = this.transformSnapshots(filteredSnapshots);
198
+ const transformedSnapshots = this.filterSnapshots(snapshots).map(this.transformSnapshotRecursive);
107
199
  if (transformedSnapshots.length === 1) return transformedSnapshots.at(0);
108
200
  return transformedSnapshots;
109
201
  }
@@ -115,81 +207,12 @@ var SemanticSnapshotTransformer = class {
115
207
  recurse: this.recurseFilter
116
208
  });
117
209
  }
118
- transformSnapshots(snapshots) {
119
- return snapshots.map((snapshot) => this.transformSnapshotRecursive(snapshot));
120
- }
121
- transformSnapshotRecursive(snapshot) {
122
- if (isTextSnapshot(snapshot)) return this.simplifyTextSnapshot(snapshot);
123
- return this.simplifyElementSnapshot(snapshot);
124
- }
125
- simplifyTextSnapshot(snapshot) {
126
- return snapshot.name;
127
- }
128
- simplifyElementSnapshot(snapshot) {
129
- const normalizedSnapshot = this.normalizeElementSnapshot(snapshot);
130
- if (this.isEmpty(normalizedSnapshot)) {
131
- const { role } = normalizedSnapshot;
132
- return this.transformedSnapshot(role, "");
133
- }
134
- if (this.hasOnlyName(normalizedSnapshot)) {
135
- const { role, name } = normalizedSnapshot;
136
- return this.transformedSnapshot(role, name);
137
- }
138
- if (this.hasOnlyChildren(normalizedSnapshot)) {
139
- const { role, children } = normalizedSnapshot;
140
- return this.transformedSnapshot(role, children.length === 1 ? children.at(0) : children);
141
- }
142
- const { role, name, attributes, children } = normalizedSnapshot;
143
- return this.transformedSnapshot(role, name, {
144
- ...attributes,
145
- children: children.length === 0 ? void 0 : children
146
- });
147
- }
148
- normalizeElementSnapshot(snapshot) {
149
- const normalizedName = snapshot.name ?? "";
150
- const transformedAttributes = this.transformAttributes(snapshot);
151
- const transformedChildren = this.transformSnapshots(snapshot.children);
152
- const nameEqualsChildren = transformedChildren.length === 1 && normalizedName === transformedChildren.at(0);
153
- return {
154
- role: snapshot.role,
155
- name: normalizedName,
156
- attributes: transformedAttributes,
157
- children: nameEqualsChildren ? [] : transformedChildren
158
- };
159
- }
160
- isEmpty(snapshot) {
161
- return isEmpty(snapshot.name) && isEmpty(snapshot.attributes) && isEmpty(snapshot.children);
162
- }
163
- hasOnlyName(snapshot) {
164
- return !isEmpty(snapshot.name) && isEmpty(snapshot.attributes) && isEmpty(snapshot.children);
165
- }
166
- hasOnlyChildren(snapshot) {
167
- return isEmpty(snapshot.name) && isEmpty(snapshot.attributes) && !isEmpty(snapshot.children);
168
- }
169
- transformAttributes(snapshot) {
170
- const { attributes } = snapshot;
171
- const filteredAttributes = {};
172
- Object.keys(attributes).forEach((attributeName) => {
173
- const transformedAttribute = this.transformAttribute(attributeName, snapshot);
174
- if (transformedAttribute === void 0) return;
175
- filteredAttributes[attributeName] = transformedAttribute;
176
- });
177
- return filteredAttributes;
178
- }
179
- transformAttribute(name, snapshot) {
180
- const { role, attributes } = snapshot;
181
- if (role === "combobox" && name === "options") {
182
- const transformedOptions = attributes.options.map((optionSnapshot) => this.simplifyElementSnapshot(optionSnapshot));
183
- return this.includeComboboxOptions && transformedOptions.length > 0 ? transformedOptions : void 0;
184
- }
185
- return attributes[name];
186
- }
187
- transformedSnapshot(role, nameOrContent, additionalProperties) {
188
- return {
189
- [role]: nameOrContent,
190
- ...additionalProperties
191
- };
192
- }
210
+ boundDefaultTransformer = (snapshot) => this.defaultTransformer(snapshot, { transform: this.transformSnapshotRecursive });
211
+ transformSnapshotRecursive = (snapshot) => {
212
+ const transformer = this.transformers[snapshot.role];
213
+ if (transformer !== void 0) return transformer(snapshot, { transform: this.boundDefaultTransformer });
214
+ return this.defaultTransformer(snapshot, { transform: this.transformSnapshotRecursive });
215
+ };
193
216
  };
194
217
  //#endregion
195
218
  //#region src/playwright/snapshot.ts
@@ -205,7 +228,7 @@ var SemanticSnapshotTransformer = class {
205
228
  */
206
229
  async function semanticSnapshot(target, options) {
207
230
  const snapshot = await rawSnapshot(target);
208
- return new SemanticSnapshotTransformer(options).transform(snapshot);
231
+ return new SnapshotSerializer(options).transform(snapshot);
209
232
  }
210
233
  /**
211
234
  * Captures a raw snapshot of the element matched by the provided locator.
@@ -323,4 +346,4 @@ function mergeTexts(texts) {
323
346
  return normalizeText(texts.map((textNode) => textNode.name).join(" "));
324
347
  }
325
348
  //#endregion
326
- export { defineElementSnapshotMatchers, excludeRole, filter, filterByRole, getTextContent, includeRole, markdownTableSnapshot, rawSnapshot, semanticSnapshot };
349
+ export { comboboxTransformer, defineElementSnapshotMatchers, excludeRole, filter, filterByRole, getTextContent, includeRole, markdownTableSnapshot, rawSnapshot, semanticSnapshot };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cronn/element-snapshot",
3
- "version": "0.28.0",
3
+ "version": "0.30.0",
4
4
  "description": "Element snapshots for Playwright",
5
5
  "homepage": "https://cronn.github.io/file-snapshots/playwright/element-snapshots/",
6
6
  "bugs": {
@@ -25,30 +25,30 @@
25
25
  }
26
26
  },
27
27
  "dependencies": {
28
- "package-directory": "8.2.0",
29
- "@cronn/lib-file-snapshots": "1.2.1",
30
- "@cronn/playwright-file-snapshots": "2.2.1"
28
+ "@cronn/lib-file-snapshots": "1.2.2",
29
+ "@cronn/playwright-file-snapshots": "2.2.2",
30
+ "package-directory": "^8.2.0"
31
31
  },
32
32
  "devDependencies": {
33
33
  "@arethetypeswrong/core": "0.18.5",
34
+ "@cronn/shared-configs": "0.0.0",
35
+ "@cronn/test-utils": "0.0.0",
36
+ "@cronn/vitest-file-snapshots": "2.1.3",
34
37
  "@playwright/test": "1.62.1",
35
38
  "@types/node": "24.13.3",
36
39
  "@typescript/native": "npm:typescript@^7.0.2",
37
- "@vitest/coverage-v8": "4.1.10",
38
- "eslint": "10.8.1",
40
+ "@vitest/coverage-v8": "4.1.11",
41
+ "eslint": "10.9.1",
39
42
  "eslint-config-prettier": "10.1.8",
40
43
  "eslint-plugin-check-file": "3.3.2",
41
44
  "eslint-plugin-unused-imports": "4.4.1",
42
- "publint": "0.3.23",
45
+ "publint": "0.3.24",
43
46
  "rimraf": "6.1.3",
44
47
  "tsdown": "0.22.14",
45
48
  "typescript": "npm:@typescript/typescript6@^6.0.2",
46
- "typescript-eslint": "8.67.0",
47
- "vitest": "4.1.10",
48
- "node": "runtime:24.19.0",
49
- "@cronn/shared-configs": "0.0.0",
50
- "@cronn/test-utils": "0.0.0",
51
- "@cronn/vitest-file-snapshots": "2.1.2"
49
+ "typescript-eslint": "8.68.0",
50
+ "vitest": "4.1.11",
51
+ "node": "runtime:24.20.0"
52
52
  },
53
53
  "peerDependencies": {
54
54
  "@playwright/test": "^1.55"
@@ -56,12 +56,12 @@
56
56
  "devEngines": {
57
57
  "packageManager": {
58
58
  "name": "pnpm",
59
- "version": "11.22.0",
59
+ "version": "11.25.0",
60
60
  "onFail": "download"
61
61
  },
62
62
  "runtime": {
63
63
  "name": "node",
64
- "version": "24.19.0",
64
+ "version": "24.20.0",
65
65
  "onFail": "download"
66
66
  }
67
67
  },