Skip to content

Commit

Permalink
Use snapshot-fixtures for testing
Browse files Browse the repository at this point in the history
This saves some boilerplate code.
  • Loading branch information
remcohaszing committed Mar 25, 2024
1 parent ad99b57 commit a4cfb20
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 34 deletions.
53 changes: 52 additions & 1 deletion package-lock.json

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

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@
"eslint-config-remcohaszing": "^10.0.0",
"eslint-plugin-jsx-a11y": "^6.0.0",
"eslint-plugin-react": "^7.0.0",
"prettier": "^3.0.0",
"rehype-raw": "^7.0.0",
"remark-cli": "^11.0.0",
"remark-preset-remcohaszing": "^2.0.0",
"prettier": "^3.0.0",
"to-vfile": "^8.0.0",
"snapshot-fixtures": "^1.0.0",
"typescript": "^5.0.0"
}
}
47 changes: 16 additions & 31 deletions src/rehype-mdx-import-media.test.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,19 @@
import assert from 'node:assert/strict'
import { readdir, readFile, writeFile } from 'node:fs/promises'
import { test } from 'node:test'

import { compile } from '@mdx-js/mdx'
import prettier from 'prettier'
import rehypeMdxImportMedia from 'rehype-mdx-import-media'
import { compileSync } from '@mdx-js/mdx'
import rehypeMdxImportMedia, { type RehypeMdxImportMediaOptions } from 'rehype-mdx-import-media'
import rehypeRaw from 'rehype-raw'
import { read } from 'to-vfile'

const fixturesDir = new URL('../fixtures/', import.meta.url)
const tests = await readdir(fixturesDir)
import { testFixturesDirectory } from 'snapshot-fixtures'

for (const name of tests) {
test(name, async () => {
const path = new URL(`${name}/`, fixturesDir)
const input = await read(new URL('input.md', path))
const expected = new URL('expected.jsx', path)
const options = JSON.parse(await readFile(new URL('options.json', path), 'utf8'))
const result = await compile(input, {
rehypePlugins: [rehypeRaw, [rehypeMdxImportMedia, options]],
jsx: true
})
const prettierConfig = await prettier.resolveConfig(expected.pathname, { editorconfig: true })
const output = await prettier.format(
String(result),
{ ...prettierConfig, filepath: expected.pathname }!
)
if (process.argv.includes('update')) {
await writeFile(expected, output)
testFixturesDirectory<RehypeMdxImportMediaOptions>({
directory: new URL('../fixtures', import.meta.url),
prettier: true,
tests: {
'expected.jsx'(file, options) {
return String(
compileSync(file, {
rehypePlugins: [rehypeRaw, [rehypeMdxImportMedia, options]],
jsx: true
})
)
}
assert.equal(output, await readFile(expected, 'utf8'))
})
}
}
})

0 comments on commit a4cfb20

Please sign in to comment.