Skip to content

Commit

Permalink
Use snapshot-fixtures for testing
Browse files Browse the repository at this point in the history
This also adds Prettier support and makes empty `options.json` files
redundant.
  • Loading branch information
remcohaszing committed Mar 26, 2024
1 parent 279fc46 commit 6c4e199
Show file tree
Hide file tree
Showing 14 changed files with 176 additions and 78 deletions.
1 change: 0 additions & 1 deletion .prettierignore

This file was deleted.

34 changes: 20 additions & 14 deletions fixtures/default/expected.jsx
Original file line number Diff line number Diff line change
@@ -1,24 +1,30 @@
/*@jsxRuntime automatic @jsxImportSource react*/
export const frontmatter = {
"title": "Hello frontmatter",
"index": 1,
"nested": {
"data": {
"structure": {
"including": {
"numbers": 42,
"booleans": true,
"": null,
"arrays": ["of", "items"]
title: 'Hello frontmatter',
index: 1,
nested: {
data: {
structure: {
including: {
numbers: 42,
booleans: true,
'': null,
arrays: ['of', 'items']
}
}
}
}
};
}
function _createMdxContent(props) {
return <></>;
return <></>
}
export default function MDXContent(props = {}) {
const {wrapper: MDXLayout} = props.components || ({});
return MDXLayout ? <MDXLayout {...props}><_createMdxContent {...props} /></MDXLayout> : _createMdxContent(props);
const { wrapper: MDXLayout } = props.components || {}
return MDXLayout ? (
<MDXLayout {...props}>
<_createMdxContent {...props} />
</MDXLayout>
) : (
_createMdxContent(props)
)
}
1 change: 0 additions & 1 deletion fixtures/default/options.json

This file was deleted.

34 changes: 20 additions & 14 deletions fixtures/named/expected.jsx
Original file line number Diff line number Diff line change
@@ -1,24 +1,30 @@
/*@jsxRuntime automatic @jsxImportSource react*/
export const frontmatter = {
"title": "Hello frontmatter",
"index": 1,
"nested": {
"data": {
"structure": {
"including": {
"numbers": 42,
"booleans": true,
"": null,
"arrays": ["of", "items"]
title: 'Hello frontmatter',
index: 1,
nested: {
data: {
structure: {
including: {
numbers: 42,
booleans: true,
'': null,
arrays: ['of', 'items']
}
}
}
}
};
}
function _createMdxContent(props) {
return <></>;
return <></>
}
export default function MDXContent(props = {}) {
const {wrapper: MDXLayout} = props.components || ({});
return MDXLayout ? <MDXLayout {...props}><_createMdxContent {...props} /></MDXLayout> : _createMdxContent(props);
const { wrapper: MDXLayout } = props.components || {}
return MDXLayout ? (
<MDXLayout {...props}>
<_createMdxContent {...props} />
</MDXLayout>
) : (
_createMdxContent(props)
)
}
14 changes: 10 additions & 4 deletions fixtures/null/expected.jsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
/*@jsxRuntime automatic @jsxImportSource react*/
export const frontmatter = null;
export const frontmatter = null
function _createMdxContent(props) {
return <></>;
return <></>
}
export default function MDXContent(props = {}) {
const {wrapper: MDXLayout} = props.components || ({});
return MDXLayout ? <MDXLayout {...props}><_createMdxContent {...props} /></MDXLayout> : _createMdxContent(props);
const { wrapper: MDXLayout } = props.components || {}
return MDXLayout ? (
<MDXLayout {...props}>
<_createMdxContent {...props} />
</MDXLayout>
) : (
_createMdxContent(props)
)
}
1 change: 0 additions & 1 deletion fixtures/null/options.json

This file was deleted.

28 changes: 20 additions & 8 deletions fixtures/toml-with-content/expected.jsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,29 @@
/*@jsxRuntime automatic @jsxImportSource react*/
export const frontmatter = {
__proto__: null,
"title": "Hello TOML"
};
title: 'Hello TOML'
}
function _createMdxContent(props) {
const _components = {
h1: "h1",
p: "p",
h1: 'h1',
p: 'p',
...props.components
};
return <><_components.h1>{"Hello, World"}</_components.h1>{"\n"}<_components.p>{"Some content"}</_components.p></>;
}
return (
<>
<_components.h1>{'Hello, World'}</_components.h1>
{'\n'}
<_components.p>{'Some content'}</_components.p>
</>
)
}
export default function MDXContent(props = {}) {
const {wrapper: MDXLayout} = props.components || ({});
return MDXLayout ? <MDXLayout {...props}><_createMdxContent {...props} /></MDXLayout> : _createMdxContent(props);
const { wrapper: MDXLayout } = props.components || {}
return MDXLayout ? (
<MDXLayout {...props}>
<_createMdxContent {...props} />
</MDXLayout>
) : (
_createMdxContent(props)
)
}
1 change: 0 additions & 1 deletion fixtures/toml-with-content/options.json

This file was deleted.

16 changes: 11 additions & 5 deletions fixtures/toml/expected.jsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
/*@jsxRuntime automatic @jsxImportSource react*/
export const frontmatter = {
__proto__: null,
"title": "Hello TOML"
};
title: 'Hello TOML'
}
function _createMdxContent(props) {
return <></>;
return <></>
}
export default function MDXContent(props = {}) {
const {wrapper: MDXLayout} = props.components || ({});
return MDXLayout ? <MDXLayout {...props}><_createMdxContent {...props} /></MDXLayout> : _createMdxContent(props);
const { wrapper: MDXLayout } = props.components || {}
return MDXLayout ? (
<MDXLayout {...props}>
<_createMdxContent {...props} />
</MDXLayout>
) : (
_createMdxContent(props)
)
}
1 change: 0 additions & 1 deletion fixtures/toml/options.json

This file was deleted.

14 changes: 10 additions & 4 deletions fixtures/undefined-named/expected.jsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
/*@jsxRuntime automatic @jsxImportSource react*/
export const frontmatter = undefined;
export const frontmatter = undefined
function _createMdxContent(props) {
return <></>;
return <></>
}
export default function MDXContent(props = {}) {
const {wrapper: MDXLayout} = props.components || ({});
return MDXLayout ? <MDXLayout {...props}><_createMdxContent {...props} /></MDXLayout> : _createMdxContent(props);
const { wrapper: MDXLayout } = props.components || {}
return MDXLayout ? (
<MDXLayout {...props}>
<_createMdxContent {...props} />
</MDXLayout>
) : (
_createMdxContent(props)
)
}
65 changes: 65 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 @@ -54,6 +54,7 @@
"remark-cli": "^11.0.0",
"remark-frontmatter": "^5.0.0",
"remark-preset-remcohaszing": "^2.0.0",
"snapshot-fixtures": "^1.0.0",
"typescript": "^5.0.0"
}
}
43 changes: 19 additions & 24 deletions src/test.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,27 @@
import assert from 'node:assert/strict'
import { readdir, readFile, writeFile } from 'node:fs/promises'
import { test } from 'node:test'

import { compile, compileSync } from '@mdx-js/mdx'
import remarkFrontmatter from 'remark-frontmatter'
import remarkMdxFrontmatter from 'remark-mdx-frontmatter'
import { assertEqual, testFixturesDirectory } from 'snapshot-fixtures'

const fixturesDir = new URL('../fixtures/', import.meta.url)
const tests = await readdir(fixturesDir)

for (const name of tests) {
test(name, async () => {
const url = new URL(`${name}/`, fixturesDir)
const input = await readFile(new URL('input.md', url))
const expected = new URL('expected.jsx', url)
const options: unknown = JSON.parse(await readFile(new URL('options.json', url), 'utf8'))
const { value } = await compile(input, {
remarkPlugins: [
[remarkFrontmatter, ['yaml', 'toml']],
[remarkMdxFrontmatter, options]
],
jsx: true
})
if (process.argv.includes('--write')) {
await writeFile(expected, value)
testFixturesDirectory({
directory: new URL('../fixtures', import.meta.url),
prettier: true,
write: true,
tests: {
'expected.jsx'(file, options) {
return compile(file, {
remarkPlugins: [
[remarkFrontmatter, ['yaml', 'toml']],
[remarkMdxFrontmatter, options]
],
jsx: true
})
}
assert.equal(value, await readFile(expected, 'utf8'))
})
}
}
})

test('custom parser', async () => {
const { value } = await compile('---\nfoo: bar\n---\n', {
Expand All @@ -37,8 +31,9 @@ test('custom parser', async () => {
],
jsx: true
})
assert.equal(
value,

assertEqual(
String(value),
`/*@jsxRuntime automatic @jsxImportSource react*/
export const frontmatter = {
"content": "foo: bar"
Expand Down

0 comments on commit 6c4e199

Please sign in to comment.