forked from remcohaszing/remark-mdx-frontmatter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.ts
90 lines (81 loc) · 2.54 KB
/
test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import { readdir, readFile, writeFile } from 'node:fs/promises';
import { compile, compileSync } from '@mdx-js/mdx';
import remarkFrontmatter from 'remark-frontmatter';
import { test } from 'uvu';
import { equal, throws } from 'uvu/assert';
import remarkMdxFrontmatter from './index.js';
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 = 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);
}
equal(value, await readFile(expected, 'utf8'));
});
}
test('custom parser', async () => {
const { value } = await compile('---\nfoo: bar\n---\n', {
remarkPlugins: [
remarkFrontmatter,
[remarkMdxFrontmatter, { parsers: { yaml: (content: string) => ({ content }) } }],
],
jsx: true,
});
equal(
value,
`/*@jsxRuntime automatic @jsxImportSource react*/
export const content = "foo: bar";
function _createMdxContent(props) {
return <></>;
}
function MDXContent(props = {}) {
const {wrapper: MDXLayout} = props.components || ({});
return MDXLayout ? <MDXLayout {...props}><_createMdxContent {...props} /></MDXLayout> : _createMdxContent(props);
}
export default MDXContent;
`,
);
});
test('unsupported types', () => {
throws(
() =>
compileSync('---\nunsupported value\n---\n', {
remarkPlugins: [remarkFrontmatter, remarkMdxFrontmatter],
jsx: true,
}),
'Expected frontmatter data to be an object, got:\nunsupported value',
);
});
test('invalid name', () => {
throws(
() =>
compileSync('---\n\n---\n', {
remarkPlugins: [remarkFrontmatter, [remarkMdxFrontmatter, { name: 'Not valid' }]],
jsx: true,
}),
'If name is specified, this should be a valid identifier name, got: "Not valid"',
);
});
test('invalid yaml key', () => {
throws(
() =>
compileSync('---\ninvalid identifier:\n---\n', {
remarkPlugins: [remarkFrontmatter, [remarkMdxFrontmatter]],
jsx: true,
}),
'Frontmatter keys should be valid identifiers, got: "invalid identifier"',
);
});
test.run();