-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Snapshot testing for the articles transformer
- Loading branch information
1 parent
0bfcef7
commit 4da245b
Showing
11 changed files
with
3,047 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
pipeline/test/prismic-snapshots/*.json linguist-generated=true | ||
*.shot linguist-language=Jest-Snapshot | ||
*.shot linguist-generated |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import fs from "node:fs"; | ||
import path from "node:path"; | ||
import { test } from "@jest/globals"; | ||
import type { Global as JestGlobal } from "@jest/types"; | ||
import { ContentType } from "../../src/types"; | ||
import { PrismicDocument } from "@prismicio/types"; | ||
|
||
// Hold snapshots in memory rather than reading from the filesystem for every test | ||
const snapshotCache = new Map<string, any>(); | ||
const contentTypesCache = new Map<ContentType, string[]>(); | ||
|
||
const dataDir = path.resolve(__dirname, "../prismic-snapshots"); | ||
|
||
const getSnapshot = <T>(name: string): T => { | ||
if (snapshotCache.has(name)) { | ||
return snapshotCache.get(name); | ||
} | ||
return JSON.parse( | ||
fs.readFileSync(path.resolve(dataDir, name), { encoding: "utf-8" }) | ||
); | ||
}; | ||
|
||
const snapshotNamesForContentType = (contentType: ContentType): string[] => | ||
contentTypesCache.get(contentType) ?? | ||
fs.readdirSync(dataDir).filter((f) => f.endsWith(`${contentType}.json`)); | ||
|
||
const forEachPrismicSnapshot = <T extends PrismicDocument>( | ||
...contentTypes: ContentType[] | ||
) => { | ||
const snapshots = contentTypes | ||
.flatMap(snapshotNamesForContentType) | ||
.map(getSnapshot<T>); | ||
|
||
return <EachFn extends JestGlobal.TestFn | JestGlobal.BlockFn>( | ||
description: string, | ||
testCase: (snapshot: T) => ReturnType<EachFn> | ||
) => test.each(snapshots)(`${description} (document: $type/$id)`, testCase); | ||
}; | ||
|
||
export default forEachPrismicSnapshot; |
Oops, something went wrong.