|
1 | 1 | import ReactDOMServer from 'react-dom/server'
|
2 | 2 | import React from 'react'
|
| 3 | +import { VFileCompatible } from 'vfile' |
| 4 | +import fs from 'fs' |
| 5 | +import os from 'os' |
| 6 | +import path from 'path' |
| 7 | +import http from 'http' |
| 8 | +import spawn from 'cross-spawn' |
| 9 | +import { ChildProcess } from 'child_process' |
| 10 | +import treeKill from 'tree-kill' |
| 11 | +import puppeteer, { Browser } from 'puppeteer' |
| 12 | +import { Server } from 'http' |
| 13 | +import handler from 'serve-handler' |
3 | 14 |
|
4 | 15 | import { MDXRemote, MDXRemoteProps } from '../src/index'
|
5 | 16 | import { serialize } from '../src/serialize'
|
6 | 17 | import { SerializeOptions } from '../src/types'
|
7 |
| -import { VFileCompatible } from 'vfile' |
8 | 18 |
|
9 | 19 | export async function renderStatic(
|
10 | 20 | mdx: VFileCompatible,
|
11 | 21 | {
|
12 | 22 | components,
|
13 | 23 | scope,
|
14 | 24 | mdxOptions,
|
15 |
| - minifyOptions, |
16 | 25 | parseFrontmatter,
|
17 | 26 | }: SerializeOptions & Pick<MDXRemoteProps, 'components'> = {}
|
18 | 27 | ): Promise<string> {
|
19 | 28 | const mdxSource = await serialize(mdx, {
|
20 | 29 | mdxOptions,
|
21 |
| - minifyOptions, |
22 | 30 | parseFrontmatter,
|
23 | 31 | })
|
24 | 32 |
|
25 | 33 | return ReactDOMServer.renderToStaticMarkup(
|
26 | 34 | <MDXRemote {...mdxSource} components={components} scope={scope} />
|
27 | 35 | )
|
28 | 36 | }
|
| 37 | + |
| 38 | +export async function getPathToPackedPackage() { |
| 39 | + const packageJson = JSON.parse( |
| 40 | + await fs.promises.readFile( |
| 41 | + path.join(__dirname, '..', 'package.json'), |
| 42 | + 'utf-8' |
| 43 | + ) |
| 44 | + ) |
| 45 | + |
| 46 | + const filename = `${packageJson.name}-${packageJson.version}.tgz` |
| 47 | + |
| 48 | + return path.join(__dirname, '..', 'dist', filename) |
| 49 | +} |
| 50 | + |
| 51 | +// Create a temporary directory from one of our fixtures to run isolated tests in |
| 52 | +// Handles installing the locally-packed next-mdx-remote |
| 53 | +export async function createTmpTestDir(fixture) { |
| 54 | + const tmpDir = await fs.promises.mkdtemp( |
| 55 | + path.join(os.tmpdir(), `next-mdx-remote-${fixture}-`) |
| 56 | + ) |
| 57 | + |
| 58 | + // copy over the fixture |
| 59 | + const pathToFixture = path.join( |
| 60 | + process.cwd(), |
| 61 | + '__tests__', |
| 62 | + 'fixtures', |
| 63 | + fixture |
| 64 | + ) |
| 65 | + |
| 66 | + await fs.promises.cp(pathToFixture, tmpDir, { recursive: true }) |
| 67 | + |
| 68 | + // install locally packed package |
| 69 | + const pathToPackedPackage = await getPathToPackedPackage() |
| 70 | + |
| 71 | + console.log('installing dependencies in test directory') |
| 72 | + |
| 73 | + spawn.sync('npm', ['install', pathToPackedPackage], { |
| 74 | + cwd: tmpDir, |
| 75 | + }) |
| 76 | + |
| 77 | + return tmpDir |
| 78 | +} |
| 79 | + |
| 80 | +async function cleanupTmpTestDir(tmpDir: string) { |
| 81 | + await fs.promises.rm(tmpDir, { recursive: true, force: true }) |
| 82 | +} |
| 83 | + |
| 84 | +// Handles creating an isolated test dir from one of the fixtures in __tests__/fixtures/ |
| 85 | +export function createDescribe( |
| 86 | + name: string, |
| 87 | + options: { fixture: string }, |
| 88 | + fn: ({ dir }: { dir: () => string; browser: () => Browser }) => void |
| 89 | +): void { |
| 90 | + describe(name, () => { |
| 91 | + let tmpDir |
| 92 | + let browser |
| 93 | + |
| 94 | + beforeAll(async () => { |
| 95 | + tmpDir = await createTmpTestDir(options.fixture) |
| 96 | + browser = await puppeteer.launch() |
| 97 | + }) |
| 98 | + |
| 99 | + fn({ |
| 100 | + dir() { |
| 101 | + return tmpDir |
| 102 | + }, |
| 103 | + browser() { |
| 104 | + return browser |
| 105 | + }, |
| 106 | + }) |
| 107 | + |
| 108 | + afterAll(async () => { |
| 109 | + await browser.close() |
| 110 | + await cleanupTmpTestDir(tmpDir) |
| 111 | + }) |
| 112 | + }) |
| 113 | +} |
| 114 | + |
| 115 | +// Starts a next dev server from the given directory on port 12333 |
| 116 | +export async function startDevServer(dir: string) { |
| 117 | + const childProcess = spawn('npx', ['next', 'dev', '-p', '12333'], { |
| 118 | + stdio: ['ignore', 'pipe', 'pipe'], |
| 119 | + cwd: dir, |
| 120 | + env: { ...process.env, NODE_ENV: 'development', __NEXT_TEST_MODE: 'true' }, |
| 121 | + }) |
| 122 | + |
| 123 | + childProcess.stderr?.on('data', (chunk) => { |
| 124 | + process.stdout.write(chunk) |
| 125 | + }) |
| 126 | + |
| 127 | + async function waitForStarted() { |
| 128 | + return new Promise<undefined>((resolve) => { |
| 129 | + childProcess.stdout?.on('data', (chunk) => { |
| 130 | + const msg = chunk.toString() |
| 131 | + process.stdout.write(chunk) |
| 132 | + |
| 133 | + if (msg.includes('started server on') && msg.includes('url:')) { |
| 134 | + resolve(undefined) |
| 135 | + } |
| 136 | + }) |
| 137 | + }) |
| 138 | + } |
| 139 | + |
| 140 | + await waitForStarted() |
| 141 | + |
| 142 | + return childProcess |
| 143 | +} |
| 144 | + |
| 145 | +// Stops running dev server using its ChildProcess instance |
| 146 | +export async function stopDevServer(childProcess: ChildProcess) { |
| 147 | + console.log('stopping development server...') |
| 148 | + const promise = new Promise((resolve) => { |
| 149 | + childProcess.on('close', () => { |
| 150 | + console.log('development server stopped') |
| 151 | + resolve(undefined) |
| 152 | + }) |
| 153 | + }) |
| 154 | + |
| 155 | + await new Promise((resolve) => { |
| 156 | + treeKill(childProcess.pid!, 'SIGKILL', () => resolve(undefined)) |
| 157 | + }) |
| 158 | + |
| 159 | + childProcess.kill('SIGKILL') |
| 160 | + |
| 161 | + await promise |
| 162 | +} |
| 163 | + |
| 164 | +// Runs next build and next export in the provided directory |
| 165 | +export function buildFixture(dir: string) { |
| 166 | + spawn.sync('npx', ['next', 'build'], { |
| 167 | + stdio: 'inherit', |
| 168 | + cwd: dir, |
| 169 | + env: { ...process.env, NODE_ENV: 'production', __NEXT_TEST_MODE: 'true' }, |
| 170 | + }) |
| 171 | + spawn.sync('npx', ['next', 'export'], { |
| 172 | + stdio: 'inherit', |
| 173 | + cwd: dir, |
| 174 | + env: { ...process.env, NODE_ENV: 'production', __NEXT_TEST_MODE: 'true' }, |
| 175 | + }) |
| 176 | +} |
| 177 | + |
| 178 | +// Helper to read an html file in the out directory relative to the provided dir |
| 179 | +export function readOutputFile(dir: string, name: string) { |
| 180 | + return fs.readFileSync(path.join(dir, 'out', `${name}.html`), 'utf8') |
| 181 | +} |
| 182 | + |
| 183 | +// Serves the out directory relative to the provided dir on port 1235 |
| 184 | +// TODO: we should just use next start |
| 185 | +export function serveStatic(dir): Promise<Server> { |
| 186 | + return new Promise((resolve) => { |
| 187 | + const server = http.createServer((req, res) => |
| 188 | + handler(req, res, { |
| 189 | + public: path.join(dir, 'out'), |
| 190 | + }) |
| 191 | + ) |
| 192 | + server.listen(1235, () => resolve(server)) |
| 193 | + }) |
| 194 | +} |
0 commit comments