/
index.js
99 lines (77 loc) · 2.39 KB
/
index.js
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
91
92
93
94
95
96
97
98
99
/**
* @typedef {import('mdx/types.js').MDXModule} MDXModule
*/
import assert from 'node:assert/strict'
import fs from 'node:fs/promises'
import {test} from 'node:test'
import React from 'react'
import {renderToStaticMarkup} from 'react-dom/server'
test('@mdx-js/node-loader', async function (t) {
await t.test('should expose the public api', async function () {
assert.deepEqual(Object.keys(await import('@mdx-js/node-loader')).sort(), [
'createLoader',
'load'
])
})
await t.test('should work', async function () {
const mdxUrl = new URL('node-loader.mdx', import.meta.url)
await fs.writeFile(
mdxUrl,
'export function Message() { return <>World!</> }\n\n# Hello, <Message />'
)
/** @type {MDXModule} */
let result
try {
result = await import(mdxUrl.href)
} catch (error) {
const exception = /** @type {NodeJS.ErrnoException} */ (error)
if (exception.code === 'ERR_UNKNOWN_FILE_EXTENSION') {
await fs.rm(mdxUrl)
throw new Error(
'Please run Node with `--loader=@mdx-js/node-loader` to test the ESM loader'
)
}
throw error
}
const Content = result.default
assert.equal(
renderToStaticMarkup(React.createElement(Content)),
'<h1>Hello, World!</h1>'
)
await fs.rm(mdxUrl)
})
await t.test('supports source maps work', async function () {
const mdxUrl = new URL('crash.mdx', import.meta.url)
await fs.writeFile(
mdxUrl,
'<Throw />\nexport function Throw() { throw new Error("Boom") }\n'
)
/** @type {MDXModule} */
let result
try {
result = await import(mdxUrl.href)
} catch (error) {
const exception = /** @type {NodeJS.ErrnoException} */ (error)
if (exception.code === 'ERR_UNKNOWN_FILE_EXTENSION') {
await fs.rm(mdxUrl)
throw new Error(
'Please run Node with `--loader=@mdx-js/node-loader` to test the ESM loader'
)
}
throw error
}
const Content = result.default
assert.throws(
() => renderToStaticMarkup(React.createElement(Content)),
(error) => {
assert(error instanceof Error)
assert.equal(error.message, 'Boom')
// Source maps are off.
// The column should be 26, not 8.
assert(error.stack?.includes('crash.mdx:2:8)'))
return true
}
)
await fs.rm(mdxUrl)
})
})