Skip to content

Commit

Permalink
Use snapshot-fixtures for fixture tests
Browse files Browse the repository at this point in the history
  • Loading branch information
remcohaszing committed Mar 29, 2024
1 parent 007ff4a commit 6685003
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 28 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.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
"prettier": "^3.0.0",
"remark-cli": "11.0.0",
"remark-preset-remcohaszing": "^2.0.0",
"to-vfile": "^8.0.0",
"snapshot-fixtures": "^1.0.0",
"typescript": "^5.0.0"
}
}
43 changes: 17 additions & 26 deletions src/test.ts
Original file line number Diff line number Diff line change
@@ -1,40 +1,31 @@
import assert from 'node:assert/strict'
import { readdir, readFile, writeFile } from 'node:fs/promises'
import { test } from 'node:test'
import { fileURLToPath } from 'node:url'

import { compile } from '@mdx-js/mdx'
import { type Root } from 'hast'
import prettier from 'prettier'
import rehypeMdxCodeProps from 'rehype-mdx-code-props'
import { read } from 'to-vfile'
import { testFixturesDirectory } from 'snapshot-fixtures'
import { visitParents } from 'unist-util-visit-parents'

const fixturesDir = new URL('../fixtures/', import.meta.url)
testFixturesDirectory({
directory: new URL('../fixtures', import.meta.url),
prettier: true,
tests: {
'expected-code.jsx'(file) {
return compile(file, {
jsx: true,
rehypePlugins: [[rehypeMdxCodeProps, { tagName: 'code' }]]
})
},

for (const name of await readdir(fixturesDir)) {
const testFixture = async (tagName: 'code' | 'pre'): Promise<void> => {
const path = new URL(`${name}/`, fixturesDir)
const input = await read(new URL('input.md', path))
const expected = new URL(`expected-${tagName}.jsx`, path)
const filepath = fileURLToPath(expected)
const prettierConfig = await prettier.resolveConfig(filepath, { editorconfig: true })

const result = await compile(input, {
jsx: true,
rehypePlugins: [[rehypeMdxCodeProps, { tagName }]]
})
const formatted = await prettier.format(String(result), { ...prettierConfig, filepath })
if (process.argv.includes('update')) {
await writeFile(expected, formatted)
'expected-pre.jsx'(file) {
return compile(file, {
jsx: true,
rehypePlugins: [rehypeMdxCodeProps]
})
}
assert.equal(String(formatted), await readFile(expected, 'utf8'))
}

test(`${name} code`, () => testFixture('code'))

test(`${name} pre`, () => testFixture('pre'))
}
})

test('invalid tagName', () => {
assert.throws(
Expand Down

0 comments on commit 6685003

Please sign in to comment.