-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
index.js
55 lines (43 loc) · 1.31 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
/**
* @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 mod
try {
mod = 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 = mod.default
assert.equal(
renderToStaticMarkup(React.createElement(Content)),
'<h1>Hello, World!</h1>'
)
await fs.rm(mdxUrl)
})
})