From 84a5a1d83a8d8e2554904a6b154a41beb31e74bb Mon Sep 17 00:00:00 2001 From: Fuma Nama Date: Wed, 15 Jan 2025 12:53:32 +0800 Subject: [PATCH] Fix tests --- .prettierignore | 5 ++- packages/mdx-remote/src/compile.ts | 10 +++-- packages/mdx-remote/test/index.test.ts | 58 ++++++++++++++++---------- 3 files changed, 46 insertions(+), 27 deletions(-) diff --git a/.prettierignore b/.prettierignore index a54729d53..0e9a7672b 100644 --- a/.prettierignore +++ b/.prettierignore @@ -21,4 +21,7 @@ examples/openapi/content/docs/(api)/**/*.mdx apps/docs/content/docs/ui/typescript.mdx -.content-collections/**/* \ No newline at end of file +.content-collections/**/* + +packages/mdx-remote/test/fixtures/**/*.js +packages/mdx-remote/test/fixtures/**/*.json diff --git a/packages/mdx-remote/src/compile.ts b/packages/mdx-remote/src/compile.ts index 8bd757420..88e967dc7 100644 --- a/packages/mdx-remote/src/compile.ts +++ b/packages/mdx-remote/src/compile.ts @@ -44,10 +44,12 @@ export interface CompileMDXOptions { mdxOptions?: MDXOptions; components?: MDXComponents; scope?: object; + + skipRender?: boolean; } export interface CompileMDXResult> { - content: React.ReactElement; + content: React.ReactNode; compiled: string; frontmatter: TFrontmatter; toc: TableOfContents; @@ -58,7 +60,7 @@ export interface CompileMDXResult> { export async function compileMDX< Frontmatter extends object = Record, >(options: CompileMDXOptions): Promise> { - const { scope = {} } = options; + const { scope = {}, skipRender } = options; const { frontmatter, content } = parseFrontmatter(options.source); const file = await compile( @@ -70,7 +72,9 @@ export async function compileMDX< return { vfile: file, compiled, - content: await renderMDX(compiled, scope, options.components), + content: skipRender + ? null + : await renderMDX(compiled, scope, options.components), frontmatter: frontmatter as Frontmatter, toc: file.data.toc as TableOfContents, scope, diff --git a/packages/mdx-remote/test/index.test.ts b/packages/mdx-remote/test/index.test.ts index b8a479ef8..f7fc70691 100644 --- a/packages/mdx-remote/test/index.test.ts +++ b/packages/mdx-remote/test/index.test.ts @@ -11,30 +11,42 @@ const files = await Glob('./fixtures/*.mdx', { }); for (const file of files) { - test(`compile: ${file}`, async () => { - const out = await compileMDX({ - source: (await fs.readFile(path.join(dir, file))).toString(), - }); + const content = (await fs.readFile(path.join(dir, file))).toString(); - await expect(out.compiled).toMatchFileSnapshot(`${file}.js`); - await expect({ - toc: out.toc, - frontmatter: out.frontmatter, - }).toMatchFileSnapshot(`${file}.json`); - }); + test( + `compile: ${file}`, + async () => { + const out = await compileMDX({ + skipRender: true, + source: content, + }); - test(`compile: ${file} (production)`, async () => { - const out = await compileMDX({ - mdxOptions: { - development: false, - }, - source: (await fs.readFile(path.join(dir, file))).toString(), - }); + await expect(out.compiled).toMatchFileSnapshot(`${file}.js`); + await expect({ + toc: out.toc, + frontmatter: out.frontmatter, + }).toMatchFileSnapshot(`${file}.json`); + }, + 1000 * 15, + ); - await expect(out.compiled).toMatchFileSnapshot(`${file}.production.js`); - await expect({ - toc: out.toc, - frontmatter: out.frontmatter, - }).toMatchFileSnapshot(`${file}.json`); - }); + test( + `compile: ${file} (production)`, + async () => { + const out = await compileMDX({ + skipRender: true, + mdxOptions: { + development: false, + }, + source: content, + }); + + await expect(out.compiled).toMatchFileSnapshot(`${file}.production.js`); + await expect({ + toc: out.toc, + frontmatter: out.frontmatter, + }).toMatchFileSnapshot(`${file}.json`); + }, + 1000 * 15, + ); }