@cronn/playwright-semantic-snapshots 0.0.0-stage → 0.35.3
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/README.md +154 -2
- package/dist/browser-lib.iife.js +919 -0
- package/dist/index.d.mts +269 -0
- package/dist/index.d.mts.map +1 -0
- package/dist/index.mjs +333 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +66 -4
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.mjs","names":["baseExpect"],"sources":["../src/utils/guards.ts","../src/utils/predicates.ts","../src/utils/filter.ts","../src/playwright/proxy.ts","../src/utils/array.ts","../src/transformers/semantic-snapshot-transformer.ts","../src/playwright/snapshot-serializer.ts","../src/playwright/snapshot.ts","../src/playwright/markdown-table.ts","../src/playwright/matchers.ts","../src/utils/text.ts"],"sourcesContent":["import type { TextSnapshot } from \"../types/elements/text\";\nimport type { NodeRole, NodeSnapshot, SnapshotByRole } from \"../types/snapshot\";\n\nexport function isTextSnapshot(\n snapshot: NodeSnapshot,\n): snapshot is TextSnapshot {\n return hasRole(\"text\", snapshot);\n}\n\nexport function hasRole<TRole extends NodeRole>(\n role: TRole | Array<TRole>,\n snapshot: NodeSnapshot,\n): snapshot is SnapshotByRole<TRole> {\n if (typeof role === \"string\") {\n return role === snapshot.role;\n }\n\n return role.includes(snapshot.role as TRole);\n}\n\nexport function isEmpty(\n value: string | Array<unknown> | Record<string, unknown>,\n): boolean {\n if (typeof value === \"string\" || Array.isArray(value)) {\n return value.length === 0;\n }\n\n return Object.keys(value).length === 0;\n}\n","import type { NodeRole, NodeSnapshot, SnapshotByRole } from \"../types/snapshot\";\n\nimport type { GuardedFilterPredicate } from \"./filter\";\nimport { hasRole } from \"./guards\";\n\n/**\n * Include only elements with the specified role\n */\nexport function includeRole<TRole extends NodeRole>(\n role: TRole | Array<TRole>,\n): GuardedFilterPredicate<SnapshotByRole<TRole>> {\n return (snapshot: NodeSnapshot) => hasRole(role, snapshot);\n}\n\n/**\n * Exclude elements with the specified role\n */\nexport function excludeRole<TRole extends NodeRole>(\n role: TRole | Array<TRole>,\n): GuardedFilterPredicate<SnapshotByRole<TRole>> {\n return (snapshot: NodeSnapshot): snapshot is SnapshotByRole<TRole> =>\n !hasRole(role, snapshot);\n}\n","import type { NodeRole, NodeSnapshot, SnapshotByRole } from \"../types/snapshot\";\n\nimport { isTextSnapshot } from \"./guards\";\nimport { includeRole } from \"./predicates\";\n\nexport type FilterPredicate = (snapshot: NodeSnapshot) => boolean;\n\nexport type GuardedFilterPredicate<TSnapshot extends NodeSnapshot> = (\n snapshot: NodeSnapshot,\n) => snapshot is TSnapshot;\n\ninterface FilterOptions extends FilterBaseOptions {\n /**\n * Include only elements in the snapshot for which the specified predicate returns `true`\n */\n predicate: FilterPredicate;\n}\n\ninterface GuardedFilterOptions<\n TSnapshot extends NodeSnapshot,\n> extends FilterBaseOptions {\n /**\n * Include only elements in the snapshot for which the specified predicate returns `true`\n */\n predicate: GuardedFilterPredicate<TSnapshot>;\n}\n\ninterface FilterBaseOptions {\n /**\n * Array of snapshots to filter.\n */\n snapshots: Array<NodeSnapshot>;\n\n /**\n * Recursively apply specified filter to children of filtered elements\n *\n * By default, recursion ends when the filter returns `true` for an element.\n * Should be `true` for filters intended to remove specific elements recursively.\n *\n * @default false\n */\n recurse?: boolean;\n}\n\n/**\n * Filters node snapshots based on the provided predicate function\n */\nexport function filter<TSnapshot extends NodeSnapshot = NodeSnapshot>(\n options: GuardedFilterOptions<TSnapshot>,\n): Array<TSnapshot>;\nexport function filter(options: FilterOptions): Array<NodeSnapshot>;\nexport function filter<TSnapshot extends NodeSnapshot>(\n options: FilterOptions | GuardedFilterOptions<TSnapshot>,\n): Array<TSnapshot> {\n const { predicate, snapshots, recurse = false } = options;\n\n return snapshots.flatMap((snapshot) => {\n const isFilteredIn = predicate(snapshot);\n\n // end recursion on included snapshot\n if (isFilteredIn && !recurse) {\n return snapshot as TSnapshot;\n }\n\n // end recursion on excluded snapshot\n if (!isFilteredIn && recurse) {\n return [];\n }\n\n const filteredChildren = hasChildren(snapshot)\n ? filter<TSnapshot>({\n predicate: predicate as GuardedFilterPredicate<TSnapshot>,\n snapshots: snapshot.children ?? [],\n recurse,\n })\n : [];\n\n // recurse excluded snapshot\n if (!isFilteredIn) {\n return filteredChildren;\n }\n\n // recurse included snapshot\n if (isTextSnapshot(snapshot)) {\n return snapshot as TSnapshot;\n }\n\n return {\n ...snapshot,\n children: filteredChildren as Array<NodeSnapshot>,\n } as TSnapshot;\n });\n}\n\n/**\n * Filters node snapshots by a specific role\n */\nexport function filterByRole<TRole extends NodeRole>(\n role: TRole,\n snapshots: Array<NodeSnapshot>,\n): Array<SnapshotByRole<TRole>> {\n return filter({\n predicate: includeRole(role),\n recurse: false,\n snapshots,\n });\n}\n\nfunction hasChildren(\n snapshot: NodeSnapshot,\n): snapshot is NodeSnapshot & { children: Array<NodeSnapshot> } {\n return \"children\" in snapshot && snapshot.children.length > 0;\n}\n","import type { Frame, Locator } from \"@playwright/test\";\nimport fs from \"node:fs/promises\";\nimport path from \"node:path\";\nimport { packageDirectory } from \"package-directory\";\n\nimport type { NodeSnapshot } from \"../types/snapshot\";\n\nexport class ElementSnapshotProxy {\n private static browserLibSource: string | undefined = undefined;\n\n public async snapshotElement(locator: Locator): Promise<Array<NodeSnapshot>> {\n // the library must be loaded into the element's own frame, which differs\n // from the page's main frame for elements inside iframes\n if (!(await this.isLibraryLoaded(locator))) {\n await this.loadLibrary(await this.resolveOwnerFrame(locator));\n }\n\n return await locator.evaluate((element) => {\n return window.snapshotElement(element);\n });\n }\n\n private async isLibraryLoaded(locator: Locator): Promise<boolean> {\n return await locator.evaluate(\n () => typeof window.snapshotElement === \"function\",\n );\n }\n\n private async resolveOwnerFrame(locator: Locator): Promise<Frame> {\n const elementHandle = await locator.elementHandle();\n try {\n const frame = await elementHandle.ownerFrame();\n if (frame === null) {\n throw new Error(\"Unable to resolve frame of element\");\n }\n return frame;\n } finally {\n await elementHandle.dispose();\n }\n }\n\n private async loadLibrary(frame: Frame): Promise<void> {\n // cache browser lib source to avoid reading it multiple times\n ElementSnapshotProxy.browserLibSource ??= await this.readBrowserLibSource();\n\n // evaluate source via Playwright instead of in-page `eval` to comply with\n // strict content security policies\n await frame.evaluate(ElementSnapshotProxy.browserLibSource);\n }\n\n private async readBrowserLibSource(): Promise<string> {\n const libPath = await this.resolveFromPackageRoot(\n \"dist\",\n \"browser-lib.iife.js\",\n );\n return await fs.readFile(libPath, \"utf8\");\n }\n\n private async resolveFromPackageRoot(\n ...paths: Array<string>\n ): Promise<string> {\n const packageRoot = await packageDirectory({\n cwd: import.meta.dirname,\n });\n\n if (packageRoot === undefined) {\n throw new Error(\"Unable to resolve root directory of package\");\n }\n\n return path.resolve(packageRoot, ...paths);\n }\n}\n","export function unwrapSingleItem<T>(items: Array<T>): T | Array<T> {\n return items.length === 1 ? (items.at(0) as T) : items;\n}\n","import type { ElementRole } from \"../types/role\";\nimport type { GenericElementSnapshot, NodeSnapshot } from \"../types/snapshot\";\nimport type {\n SnapshotTransformer,\n SnapshotTransformerContext,\n} from \"../types/transformer\";\nimport { unwrapSingleItem } from \"../utils/array\";\nimport { isEmpty } from \"../utils/guards\";\n\ninterface NormalizedElementSnapshot {\n role: ElementRole;\n name: string;\n attributes: Record<string, unknown>;\n children: Array<unknown>;\n}\n\nexport function semanticSnapshotTransformer(\n snapshot: NodeSnapshot,\n context: SnapshotTransformerContext,\n): unknown {\n if (snapshot.role === \"text\") {\n return snapshot.name;\n }\n\n return transformElementSnapshot(snapshot, context.transform);\n}\n\nfunction transformElementSnapshot(\n snapshot: GenericElementSnapshot,\n transformRecursive: SnapshotTransformer,\n): unknown {\n const normalizedSnapshot = normalizeElementSnapshot(\n snapshot,\n transformRecursive,\n );\n\n if (hasOnlyRole(normalizedSnapshot)) {\n const { role } = normalizedSnapshot;\n return transformedSnapshot(role, \"\");\n }\n\n if (hasOnlyName(normalizedSnapshot)) {\n const { role, name } = normalizedSnapshot;\n return transformedSnapshot(role, name);\n }\n\n if (hasOnlyChildren(normalizedSnapshot)) {\n const { role, children } = normalizedSnapshot;\n return transformedSnapshot(role, unwrapSingleItem(children));\n }\n\n const { role, name, attributes, children } = normalizedSnapshot;\n return transformedSnapshot(role, name, {\n ...attributes,\n children: isEmpty(children) ? undefined : unwrapSingleItem(children),\n });\n}\n\nfunction normalizeElementSnapshot(\n snapshot: GenericElementSnapshot,\n transformRecursive: SnapshotTransformer,\n): NormalizedElementSnapshot {\n const normalizedName = snapshot.name ?? \"\";\n const transformedAttributes = transformAttributes(snapshot.attributes ?? {});\n const transformedChildren = (snapshot.children ?? []).map(transformRecursive);\n const singleChild =\n transformedChildren.length === 1 ? transformedChildren.at(0) : undefined;\n const nameEqualsChildren = normalizedName === singleChild;\n const valueEqualsChildren =\n typeof transformedAttributes.value === \"string\" &&\n transformedAttributes.value === singleChild;\n\n return {\n role: snapshot.role,\n name: normalizedName,\n attributes: transformedAttributes,\n children:\n nameEqualsChildren || valueEqualsChildren ? [] : transformedChildren,\n };\n}\n\nfunction hasOnlyRole(snapshot: NormalizedElementSnapshot): boolean {\n return (\n isEmpty(snapshot.name) &&\n isEmpty(snapshot.attributes) &&\n isEmpty(snapshot.children)\n );\n}\n\nfunction hasOnlyName(snapshot: NormalizedElementSnapshot): boolean {\n return (\n !isEmpty(snapshot.name) &&\n isEmpty(snapshot.attributes) &&\n isEmpty(snapshot.children)\n );\n}\n\nfunction hasOnlyChildren(snapshot: NormalizedElementSnapshot): boolean {\n return (\n isEmpty(snapshot.name) &&\n isEmpty(snapshot.attributes) &&\n !isEmpty(snapshot.children)\n );\n}\n\nfunction transformAttributes(attributes: object): Record<string, unknown> {\n const filteredAttributes: Record<string, unknown> = {};\n\n Object.entries(attributes).forEach(([attributeName, attributeValue]) => {\n if (attributeValue === undefined) {\n return;\n }\n\n filteredAttributes[attributeName] = attributeValue;\n });\n\n return filteredAttributes;\n}\n\nfunction transformedSnapshot(\n role: ElementRole,\n nameOrContent: unknown,\n additionalProperties?: Record<string, unknown>,\n): unknown {\n return { [role]: nameOrContent, ...additionalProperties };\n}\n","import { semanticSnapshotTransformer } from \"../transformers/semantic-snapshot-transformer\";\nimport type { NodeSnapshot } from \"../types/snapshot\";\nimport type {\n DefaultSnapshotTransformer,\n RoleBasedSnapshotTransformer,\n RoleBasedSnapshotTransformers,\n} from \"../types/transformer\";\nimport { unwrapSingleItem } from \"../utils/array\";\nimport type { FilterPredicate } from \"../utils/filter\";\nimport { filter } from \"../utils/filter\";\n\ninterface SnapshotSerializerOptions {\n filter?: FilterPredicate;\n recurseFilter?: boolean;\n transformers?: RoleBasedSnapshotTransformers;\n defaultTransformer?: DefaultSnapshotTransformer;\n}\n\nexport class SnapshotSerializer {\n private readonly filter?: FilterPredicate;\n private readonly recurseFilter: boolean;\n private readonly transformers: RoleBasedSnapshotTransformers;\n private readonly defaultTransformer: DefaultSnapshotTransformer;\n\n public constructor(options: SnapshotSerializerOptions = {}) {\n this.filter = options.filter;\n this.recurseFilter = options.recurseFilter ?? false;\n this.transformers = { ...options.transformers };\n this.defaultTransformer =\n options.defaultTransformer ?? semanticSnapshotTransformer;\n }\n\n public transform(snapshots: Array<NodeSnapshot>): unknown {\n const filteredSnapshots = this.filterSnapshots(snapshots);\n const transformedSnapshots = filteredSnapshots.map(\n this.transformSnapshotRecursive,\n );\n\n return unwrapSingleItem(transformedSnapshots);\n }\n\n private filterSnapshots(snapshots: Array<NodeSnapshot>): Array<NodeSnapshot> {\n if (this.filter === undefined) {\n return snapshots;\n }\n\n return filter({\n predicate: this.filter,\n snapshots,\n recurse: this.recurseFilter,\n });\n }\n\n private readonly boundDefaultTransformer = (\n snapshot: NodeSnapshot,\n ): unknown =>\n this.defaultTransformer(snapshot, {\n transform: this.transformSnapshotRecursive,\n });\n\n private readonly transformSnapshotRecursive = (\n snapshot: NodeSnapshot,\n ): unknown => {\n const transformer = this.transformers[snapshot.role] as\n | RoleBasedSnapshotTransformer\n | undefined;\n\n if (transformer !== undefined) {\n return transformer(snapshot, { transform: this.boundDefaultTransformer });\n }\n\n return this.defaultTransformer(snapshot, {\n transform: this.transformSnapshotRecursive,\n });\n };\n}\n","import type { Page } from \"@playwright/test\";\n\nimport type { NodeSnapshot } from \"../types/snapshot\";\nimport type {\n DefaultSnapshotTransformer,\n RoleBasedSnapshotTransformers,\n} from \"../types/transformer\";\nimport type { PlaywrightTarget } from \"../types/utils\";\nimport type { FilterPredicate } from \"../utils/filter\";\n\nimport { ElementSnapshotProxy } from \"./proxy\";\nimport { SnapshotSerializer } from \"./snapshot-serializer\";\n\nexport interface SemanticSnapshotOptions {\n /**\n * Include only elements in the snapshot for which the specified filter returns `true`\n *\n * @default () => true\n */\n filter?: FilterPredicate;\n\n /**\n * Recursively apply specified filter to children of filtered elements\n *\n * By default, recursion ends when the filter returns `true` for an element.\n * Should be `true` for filters intended to remove specific elements recursively.\n *\n * @default false\n */\n recurseFilter?: boolean;\n\n /**\n * Replace the default transformation for specific roles\n *\n * Transformers are keyed by role and applied after the filter, but before\n * the default serialization. A transformer may return any JSON-serializable\n * value, which is written to the snapshot verbatim. The `transform` function\n * of its context applies the default transformation to untransformed\n * children.\n */\n transformers?: RoleBasedSnapshotTransformers;\n\n /**\n * Replace the default transformation for all roles\n *\n * Applied to every snapshot passed to the default transformation, including\n * the ones a transformer delegates via the `transform` function of its\n * context. Transformers registered for a role take precedence.\n *\n * @default semanticSnapshotTransformer\n */\n defaultTransformer?: DefaultSnapshotTransformer;\n}\n\n/**\n * Captures a semantic snapshot of the specified locator's DOM structure.\n *\n * This function creates a raw snapshot of the DOM element identified by the locator\n * and transforms it into a semantic representation using the provided options.\n *\n * @param target - The Page or Locator instance that identifies the DOM element to snapshot\n * @param options - Optional configuration settings for controlling the snapshot behavior\n * @return A promise that resolves to the semantic snapshot\n */\nexport async function semanticSnapshot(\n target: PlaywrightTarget,\n options?: SemanticSnapshotOptions,\n): Promise<unknown> {\n const snapshot = await rawSnapshot(target);\n return new SnapshotSerializer(options).transform(snapshot);\n}\n\n/**\n * Captures a raw snapshot of the element matched by the provided locator.\n *\n * Returns an array of node snapshots representing the current state of the matched elements\n * in the DOM, including their properties, attributes, and structure.\n *\n * @param target - The Page or Locator instance used to identify the target element to snapshot\n * @return A promise that resolves to an array of NodeSnapshot objects representing the captured element\n */\nexport async function rawSnapshot(\n target: PlaywrightTarget,\n): Promise<Array<NodeSnapshot>> {\n const locator = isPage(target) ? target.locator(\"body\") : target;\n return await new ElementSnapshotProxy().snapshotElement(locator);\n}\n\nfunction isPage(target: PlaywrightTarget): target is Page {\n return \"goto\" in target && typeof target.goto === \"function\";\n}\n","import type { Locator } from \"@playwright/test\";\n\nimport {\n type MarkdownTableNormalizer,\n MarkdownTableSerializer,\n type TableData,\n type TableRow,\n} from \"@cronn/lib-file-snapshots\";\n\nimport type { ColumnHeaderSnapshot, SortType } from \"../types/elements/table\";\nimport type {\n ElementSnapshot,\n NodeRole,\n NodeSnapshot,\n SnapshotByRole,\n} from \"../types/snapshot\";\nimport { filter, filterByRole } from \"../utils/filter\";\nimport { includeRole } from \"../utils/predicates\";\n\nimport { rawSnapshot } from \"./snapshot\";\n\nconst tableRoles = [\"table\", \"grid\"] satisfies Array<NodeRole>;\n\nconst cellRoles = [\"rowheader\", \"cell\", \"gridcell\"] satisfies Array<NodeRole>;\n\nconst sortIndicators: Record<SortType, string> = {\n ascending: \"⯅\",\n descending: \"⯆\",\n other: \"⯁\",\n};\n\nexport interface MarkdownTableSnapshotOptions {\n /**\n * Show sort indicator for column headers\n *\n * @default true\n */\n showSortIndicator?: boolean;\n\n /**\n * Custom normalizers to apply before serialization\n */\n normalizers?: Array<MarkdownTableNormalizer>;\n}\n\n/**\n * Creates a Markdown table snapshot from a table or grid element\n */\nexport async function markdownTableSnapshot(\n locator: Locator,\n options: MarkdownTableSnapshotOptions = {},\n): Promise<string> {\n const { showSortIndicator, normalizers } = options;\n\n const elementSnapshot = await rawSnapshot(locator);\n const parsedTable = parseTable(elementSnapshot, { showSortIndicator });\n\n return new MarkdownTableSerializer({ normalizers }).serialize(parsedTable);\n}\n\nfunction parseTable(\n snapshot: Array<NodeSnapshot>,\n options: MarkdownTableSnapshotOptions,\n): TableData {\n const { showSortIndicator = true } = options;\n\n const tableOrGridResult = filter({\n predicate: includeRole(tableRoles),\n snapshots: snapshot,\n });\n if (tableOrGridResult.length > 1) {\n throw new Error(\"Multiple tables or grids found\");\n }\n\n const [tableOrGrid] = tableOrGridResult;\n if (tableOrGrid === undefined) {\n throw new Error(\"No table or grid found\");\n }\n\n const [headerRow, ...bodyRows] = filterByRole(\"row\", tableOrGrid.children);\n if (headerRow === undefined) {\n throw new Error(\"No header row found\");\n }\n\n const columnHeaders = filterByRole(\"columnheader\", headerRow.children);\n if (columnHeaders.length === 0) {\n throw new Error(\"No column headers found\");\n }\n\n const columnHeaderTexts = columnHeaders.map((columnHeader) =>\n getColumnHeaderText(columnHeader, showSortIndicator),\n );\n const cellsTextsByRow = bodyRows.map(getCellTexts);\n\n return { columns: columnHeaderTexts, rows: cellsTextsByRow };\n}\n\nfunction getColumnHeaderText(\n columnHeader: ColumnHeaderSnapshot,\n showSortIndicator: boolean,\n): string {\n const headerText = getNameOrDefault(columnHeader);\n const sortType = columnHeader.attributes.sort;\n\n if (!showSortIndicator || sortType === undefined) {\n return headerText;\n }\n\n const sortIndicator = sortIndicators[sortType];\n return `${headerText} ${sortIndicator}`;\n}\n\nfunction getCellTexts(row: SnapshotByRole<\"row\">): TableRow {\n return filter({\n predicate: includeRole(cellRoles),\n snapshots: row.children,\n }).map(getNameOrDefault);\n}\n\nfunction getNameOrDefault(snapshot: ElementSnapshot): string {\n return snapshot.name ?? \"\";\n}\n","import type {\n Expect,\n ExpectMatcherState,\n Locator,\n MatcherReturnType,\n} from \"@playwright/test\";\nimport { expect as baseExpect } from \"@playwright/test\";\n\nimport type { PlaywrightValidationFileMatcherConfig } from \"@cronn/playwright-file-snapshots\";\nimport {\n defineJsonFileMatcher,\n defineTextFileMatcher,\n} from \"@cronn/playwright-file-snapshots\";\n\nimport type {\n SemanticSnapshotMatchers,\n MatchMarkdownTableSnapshotFileOptions,\n MatchSemanticSnapshotFileOptions,\n} from \"../types/matchers\";\n\nimport { markdownTableSnapshot } from \"./markdown-table\";\nimport { semanticSnapshot } from \"./snapshot\";\n\n/**\n * Defines a custom Playwright expect instance with semantic snapshot matchers for validating DOM elements against stored snapshots.\n *\n * @param {PlaywrightValidationFileMatcherConfig} config - Configuration options for file matching behavior\n * @return {Expect<SemanticSnapshotMatchers>} An extended expect instance that includes matcher methods for semantic snapshot validation.\n *\n * @example\n * export const expect = defineSemanticSnapshotMatchers();\n *\n * test(\"matches semantic snapshot\", async ({ page }) => {\n * await expect(page.getByRole(\"main\")).toMatchSemanticSnapshotFile();\n * });\n */\nexport function defineSemanticSnapshotMatchers(\n config: PlaywrightValidationFileMatcherConfig = {},\n): Expect<SemanticSnapshotMatchers> {\n async function toMatchSemanticSnapshotFile(\n this: ExpectMatcherState,\n actual: Locator,\n options: MatchSemanticSnapshotFileOptions = {},\n ): Promise<MatcherReturnType> {\n const toMatchJsonFile = defineJsonFileMatcher(\n \"toMatchSemanticSnapshotFile\",\n config,\n ).bind(this);\n return await toMatchJsonFile(\n () => semanticSnapshot(actual, options),\n options,\n );\n }\n\n async function toMatchMarkdownTableSnapshotFile(\n this: ExpectMatcherState,\n actual: Locator,\n options: MatchMarkdownTableSnapshotFileOptions = {},\n ): Promise<MatcherReturnType> {\n const toMatchTextFile = defineTextFileMatcher(\n \"toMatchMarkdownTableSnapshotFile\",\n config,\n ).bind(this);\n const { showSortIndicator, normalizers, ...matchFileOptions } = options;\n return await toMatchTextFile(\n () =>\n markdownTableSnapshot(actual, {\n showSortIndicator,\n normalizers,\n }),\n {\n ...matchFileOptions,\n fileExtension: \"md\",\n },\n );\n }\n\n return baseExpect.extend({\n toMatchSemanticSnapshotFile,\n toMatchMarkdownTableSnapshotFile,\n });\n}\n","import type { TextSnapshot } from \"../types/elements/text\";\nimport type { NodeSnapshot } from \"../types/snapshot\";\n\nimport { filter } from \"./filter\";\nimport { isTextSnapshot } from \"./guards\";\n\n/**\n * Aggregates and normalizes the text content of all provided snapshots\n */\nexport function getTextContent(snapshots: Array<NodeSnapshot>): string {\n const texts = filter({\n snapshots,\n predicate: isTextSnapshot,\n });\n\n return mergeTexts(texts);\n}\n\nexport function normalizeText(text: string): string {\n return text.replaceAll(/\\s+/g, \" \").trim();\n}\n\nexport function mergeTexts(texts: Array<TextSnapshot>): string {\n const mergedText = texts.map((textNode) => textNode.name).join(\" \");\n return normalizeText(mergedText);\n}\n"],"mappings":";;;;;;;AAGA,SAAgB,eACd,UAC0B;CAC1B,OAAO,QAAQ,QAAQ,QAAQ;AACjC;AAEA,SAAgB,QACd,MACA,UACmC;CACnC,IAAI,OAAO,SAAS,UAClB,OAAO,SAAS,SAAS;CAG3B,OAAO,KAAK,SAAS,SAAS,IAAa;AAC7C;AAEA,SAAgB,QACd,OACS;CACT,IAAI,OAAO,UAAU,YAAY,MAAM,QAAQ,KAAK,GAClD,OAAO,MAAM,WAAW;CAG1B,OAAO,OAAO,KAAK,KAAK,CAAC,CAAC,WAAW;AACvC;;;;;ACpBA,SAAgB,YACd,MAC+C;CAC/C,QAAQ,aAA2B,QAAQ,MAAM,QAAQ;AAC3D;;;;AAKA,SAAgB,YACd,MAC+C;CAC/C,QAAQ,aACN,CAAC,QAAQ,MAAM,QAAQ;AAC3B;;AC6BA,SAAgB,OACd,SACkB;CAClB,MAAM,EAAE,WAAW,WAAW,UAAU,UAAU;CAElD,OAAO,UAAU,SAAS,aAAa;EACrC,MAAM,eAAe,UAAU,QAAQ;EAGvC,IAAI,gBAAgB,CAAC,SACnB,OAAO;EAIT,IAAI,CAAC,gBAAgB,SACnB,OAAO,CAAC;EAGV,MAAM,mBAAmB,YAAY,QAAQ,IACzC,OAAkB;GACL;GACX,WAAW,SAAS,YAAY,CAAC;GACjC;EACF,CAAC,IACD,CAAC;EAGL,IAAI,C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B,CACF;CACF;CAEA,OAAOA,OAAW,OAAO;EACvB;EACA;CACF,CAAC;AACH;;;;;ACxEA,SAAgB,eAAe,WAAwC;CAMrE,OAAO,WALO,OAAO;EACnB;EACA,WAAW;CACb,CAEsB,CAAC;AACzB;AAEA,SAAgB,cAAc,MAAsB;CAClD,OAAO,KAAK,WAAW,QAAQ,GAAG,CAAC,CAAC,KAAK;AAC3C;AAEA,SAAgB,WAAW,OAAoC;CAE7D,OAAO,cADY,MAAM,KAAK,aAAa,SAAS,IAAI,CAAC,CAAC,KAAK,GACjC,CAAC;AACjC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,68 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cronn/playwright-semantic-snapshots",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.35.3",
|
|
4
|
+
"description": "Semantic snapshots for Playwright",
|
|
5
|
+
"homepage": "https://cronn.github.io/file-snapshots/playwright/semantic-snapshots/",
|
|
6
|
+
"bugs": {
|
|
7
|
+
"url": "https://github.com/cronn/file-snapshots/issues"
|
|
8
|
+
},
|
|
9
|
+
"license": "Apache-2.0",
|
|
10
|
+
"author": "cronn",
|
|
11
|
+
"repository": {
|
|
12
|
+
"type": "git",
|
|
13
|
+
"url": "git+https://github.com/cronn/file-snapshots.git",
|
|
14
|
+
"directory": "packages/playwright-semantic-snapshots"
|
|
15
|
+
},
|
|
16
|
+
"files": [
|
|
17
|
+
"dist"
|
|
18
|
+
],
|
|
19
|
+
"type": "module",
|
|
20
|
+
"exports": {
|
|
21
|
+
"./package.json": "./package.json",
|
|
22
|
+
".": {
|
|
23
|
+
"types": "./dist/index.d.mts",
|
|
24
|
+
"default": "./dist/index.mjs"
|
|
25
|
+
}
|
|
26
|
+
},
|
|
27
|
+
"scripts": {
|
|
28
|
+
"build": "tsdown",
|
|
29
|
+
"clean": "rimraf .turbo dist playwright-report test-results coverage",
|
|
30
|
+
"compile": "tsc",
|
|
31
|
+
"lint:check": "oxlint src/ tests/",
|
|
32
|
+
"lint:fix": "oxlint src/ tests/ --fix",
|
|
33
|
+
"playwright:install": "playwright install chromium",
|
|
34
|
+
"snapshots:check": "file-snapshots check --dir data/unit-test --dir data/integration-test",
|
|
35
|
+
"test:integration": "playwright test",
|
|
36
|
+
"test:unit": "vitest run",
|
|
37
|
+
"test:unit:coverage": "vitest run --coverage"
|
|
38
|
+
},
|
|
39
|
+
"dependencies": {
|
|
40
|
+
"@cronn/lib-file-snapshots": "workspace:*",
|
|
41
|
+
"@cronn/playwright-file-snapshots": "workspace:*",
|
|
42
|
+
"package-directory": "catalog:prod"
|
|
43
|
+
},
|
|
44
|
+
"devDependencies": {
|
|
45
|
+
"@arethetypeswrong/core": "catalog:dev",
|
|
46
|
+
"@cronn/file-snapshots-cli": "workspace:*",
|
|
47
|
+
"@cronn/shared-configs": "workspace:*",
|
|
48
|
+
"@cronn/test-utils": "workspace:*",
|
|
49
|
+
"@cronn/vitest-file-snapshots": "workspace:*",
|
|
50
|
+
"@playwright/test": "catalog:dev",
|
|
51
|
+
"@types/node": "catalog:dev",
|
|
52
|
+
"@typescript/native": "catalog:dev",
|
|
53
|
+
"@vitest/coverage-v8": "catalog:dev",
|
|
54
|
+
"oxlint": "catalog:dev",
|
|
55
|
+
"oxlint-tsgolint": "catalog:dev",
|
|
56
|
+
"publint": "catalog:dev",
|
|
57
|
+
"rimraf": "catalog:dev",
|
|
58
|
+
"tsdown": "catalog:dev",
|
|
59
|
+
"typescript": "catalog:dev",
|
|
60
|
+
"vitest": "catalog:dev"
|
|
61
|
+
},
|
|
62
|
+
"peerDependencies": {
|
|
63
|
+
"@playwright/test": "^1.55"
|
|
64
|
+
},
|
|
65
|
+
"engines": {
|
|
66
|
+
"node": "^24.8.0 || >=26"
|
|
67
|
+
}
|
|
68
|
+
}
|