diff --git a/__tests__/lib/run.test.tsx b/__tests__/lib/run.test.tsx new file mode 100644 index 000000000..e30b956fd --- /dev/null +++ b/__tests__/lib/run.test.tsx @@ -0,0 +1,24 @@ +import React from 'react'; +import { render, screen } from '@testing-library/react'; + +import { execute } from '../helpers'; + +describe('run', () => { + it('allows providing imports', async () => { + const mdx = `Hello, world!`; + const Component = await execute(mdx, {}, { imports: { React } }); + + render(); + + expect(screen.getByText('Hello, world!')).toBeInTheDocument(); + }); + + it('merges the imports with the built-ins', async () => { + const mdx = `{user.test}`; + const Component = await execute(mdx, {}, { imports: { React } }); + + render(); + + expect(screen.getByText('TEST')).toBeInTheDocument(); + }); +}); diff --git a/lib/run.tsx b/lib/run.tsx index f44f86b47..e48dd6fe2 100644 --- a/lib/run.tsx +++ b/lib/run.tsx @@ -49,7 +49,7 @@ const makeUseMDXComponents = (more: ReturnType = {}): UseMdxCo const run = async (string: string, _opts: RunOpts = {}) => { const { Fragment } = runtime as any; - const { components = {}, terms, variables, baseUrl, ...opts } = _opts; + const { components = {}, terms, variables, baseUrl, imports = {}, ...opts } = _opts; const executedComponents = Object.entries(components).reduce((memo, [tag, mod]) => { const { default: Content, toc, Toc, ...rest } = mod; memo[tag] = Content; @@ -68,10 +68,10 @@ const run = async (string: string, _opts: RunOpts = {}) => { ...runtime, Fragment, baseUrl: import.meta.url, - imports: { React, user: User(variables) }, + imports: { React, user: User(variables), ...imports }, useMDXComponents, ...opts, - }) as Promise; + } as RunOptions) as Promise; }; const { Toc: _Toc, toc, default: Content, ...exports } = await exec(string);