Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
fuma-nama committed Jan 15, 2025
1 parent 37bbdc1 commit 84a5a1d
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 27 deletions.
5 changes: 4 additions & 1 deletion .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,7 @@ examples/openapi/content/docs/(api)/**/*.mdx

apps/docs/content/docs/ui/typescript.mdx

.content-collections/**/*
.content-collections/**/*

packages/mdx-remote/test/fixtures/**/*.js
packages/mdx-remote/test/fixtures/**/*.json
10 changes: 7 additions & 3 deletions packages/mdx-remote/src/compile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,12 @@ export interface CompileMDXOptions {
mdxOptions?: MDXOptions;
components?: MDXComponents;
scope?: object;

skipRender?: boolean;
}

export interface CompileMDXResult<TFrontmatter = Record<string, unknown>> {
content: React.ReactElement;
content: React.ReactNode;
compiled: string;
frontmatter: TFrontmatter;
toc: TableOfContents;
Expand All @@ -58,7 +60,7 @@ export interface CompileMDXResult<TFrontmatter = Record<string, unknown>> {
export async function compileMDX<
Frontmatter extends object = Record<string, unknown>,
>(options: CompileMDXOptions): Promise<CompileMDXResult<Frontmatter>> {
const { scope = {} } = options;
const { scope = {}, skipRender } = options;
const { frontmatter, content } = parseFrontmatter(options.source);

const file = await compile(
Expand All @@ -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,
Expand Down
58 changes: 35 additions & 23 deletions packages/mdx-remote/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
);
}

0 comments on commit 84a5a1d

Please sign in to comment.