Skip to content

Commit

Permalink
Replace file comparison code with jest-file-snapshot
Browse files Browse the repository at this point in the history
jest-file-snapshot produces nicer error messages and prints the
following helpful instructions if the tests fail:

```
Snapshot Summary
 › 1 snapshot failed from 1 test suite. Inspect your code changes or run `npm test -- -u` to update them.
```
  • Loading branch information
lpsinger committed Jul 18, 2024
1 parent f62a663 commit c56f3ee
Show file tree
Hide file tree
Showing 3 changed files with 176 additions and 41 deletions.
160 changes: 160 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
"glob": "^10.3.4",
"husky": "^8.0.3",
"jest": "^29.6.4",
"jest-file-snapshot": "^0.7.0",
"lint-staged": "^13.1.0",
"npm-run-all": "^4.1.5",
"prettier": "3.0.3",
Expand Down
56 changes: 15 additions & 41 deletions test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { readFile, writeFile } from 'fs/promises'
import { readFile } from 'fs/promises'
import { globSync } from 'glob'
import { toMatchFile } from 'jest-file-snapshot'
import type { Nodes } from 'mdast'
import rehypeStringify from 'rehype-stringify'
import remarkParse from 'remark-parse'
Expand All @@ -9,10 +10,6 @@ import { removePosition } from 'unist-util-remove-position'

import { rehypeAstro, remarkAstro } from './src'

async function read(filename: string) {
return await readFile(filename, { encoding: 'utf-8' })
}

function remarkRemovePosition() {
return (tree: Nodes) => {
removePosition(tree)
Expand All @@ -24,51 +21,28 @@ function remarkJson(this: Processor) {
this.Compiler = (root) => JSON.stringify(root, undefined, 2)
}

function splitLines(s: string) {
return s.split(/\r?\n/)
}

function remarkProcessor() {
return unified()
const processors = {
json: unified()
.use(remarkParse)
.use(remarkRemovePosition)
.use(remarkAstro)
.use(remarkJson)
}

function rehypeProcessor() {
return unified()
.use(remarkJson),
html: unified()
.use(remarkParse)
.use(remarkRehype)
.use(rehypeAstro)
.use(rehypeStringify)
.use(rehypeStringify),
}

describe.each([
['remarkAstro', 'json', remarkProcessor],
['rehypeAstro', 'html', rehypeProcessor],
])('%s', (_, ext, processor) => {
test.each(globSync('src/replacements/*/test.md'))(
'%s',
async (inputFilename) => {
const result = (
await processor().process(await readFile(inputFilename))
).value.toString()
expect.extend({ toMatchFile })

describe.each(globSync('src/replacements/*/test.md'))('convert %s', (path) => {
const stem = path.replace(/\.md$/, '')
let input: Buffer

const expectedFilename = inputFilename.replace(/\.md$/, `.${ext}`)
let expected
try {
expected = await read(expectedFilename)
} catch {
const draftFilename = inputFilename.replace(/\.md$/, `.draft.${ext}`)
await writeFile(draftFilename, result)
throw new Error(
`Could not read the expected results file, '${expectedFilename}'. Please review the draft results file at '${draftFilename}'. If the results are correct, then rename it to '${expectedFilename}' and add it to version control.`
)
}
beforeAll(async () => (input = await readFile(path)))

// Compare lines, not the strings themselves, due to newlines on Windows
expect(splitLines(result)).toStrictEqual(splitLines(expected))
}
test.each(Object.entries(processors))(`to ${stem}.%s`, (ext, proc) =>
expect(proc.processSync(input).value).toMatchFile(`${stem}.${ext}`)
)
})

0 comments on commit c56f3ee

Please sign in to comment.