-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.test.mjs
69 lines (62 loc) · 1.92 KB
/
index.test.mjs
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
import { promises as fs, readdirSync } from 'fs';
import { createRequire } from 'module';
import { join } from 'path';
import { compile, compileSync } from '@mdx-js/mdx';
import remarkFrontmatter from 'remark-frontmatter';
import test from 'tape';
const { remarkMdxNext } = createRequire(import.meta.url)('./src/index.ts');
const tests = readdirSync('__fixtures__');
for (const name of tests) {
test(name, async (t) => {
const path = join('__fixtures__', name);
const input = await fs.readFile(join(path, 'input.md'));
const expected = join(path, 'expected.jsx');
const options = JSON.parse(await fs.readFile(join(path, 'options.json')));
const { value } = await compile(input, {
remarkPlugins: [
[remarkFrontmatter, ['yaml', 'toml']],
[remarkMdxNext, options],
],
jsx: true,
});
if (process.argv.includes('--write')) {
await fs.writeFile(expected, value);
}
const expectedFile = await fs.readFile(expected, 'utf8');
t.equal(value, expectedFile);
t.end();
});
}
test('unsupported types', (t) => {
t.throws(
() =>
compileSync('---\nunsupported value\n---\n', {
remarkPlugins: [remarkFrontmatter, remarkMdxNext],
jsx: true,
}),
'Expected frontmatter data to be an object, got:\n---yaml\nunsupported value\n---',
);
t.end();
});
test('invalid name', (t) => {
t.throws(
() =>
compileSync('---\n\n---\n', {
remarkPlugins: [remarkFrontmatter, [remarkMdxNext, { name: 'Not valid' }]],
jsx: true,
}),
'If name is specified, this should be a valid identifier name, got: "Not valid"',
);
t.end();
});
test('invalid yaml key', (t) => {
t.throws(
() =>
compileSync('---\ninvalid identifier:\n---\n', {
remarkPlugins: [remarkFrontmatter, [remarkMdxNext]],
jsx: true,
}),
'Frontmatter keys should be valid identifiers, got: "invalid identifier"',
);
t.end();
});