Skip to content

Commit 92322fc

Browse files
author
Bryce Kalow
committed
chore: add integration test for development server
1 parent e912ff5 commit 92322fc

File tree

5 files changed

+661
-666
lines changed

5 files changed

+661
-666
lines changed

__tests__/fixtures/basic/mdx/test.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ name: jeff
55

66
import Bar from 'bar'
77

8-
# Headline
8+
## Headline
99

1010
<Test name={frontmatter.name} />
1111

__tests__/integration.test.tsx

Lines changed: 87 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,27 @@ import handler from 'serve-handler'
99
import http from 'http'
1010
import rmfr from 'rmfr'
1111
import * as cheerio from 'cheerio'
12+
import { ChildProcess } from 'child_process'
1213

1314
jest.setTimeout(30000)
1415

15-
describe('hydration', () => {
16+
describe('hydration - production', () => {
1617
beforeAll(() => {
1718
buildFixture('basic')
1819
})
1920

21+
afterAll(async () => {
22+
await cleanupNextDirectory('basic')
23+
})
24+
2025
test('server rendered output', () => {
2126
const result = readOutputFile('basic', 'index')
2227
const $ = cheerio.load(result)
2328

2429
const htmlOutput = $('#__next').html()
2530

2631
// server renders correctly
27-
expect(htmlOutput).toContain(`<h1>foo</h1><h1>Headline</h1>
32+
expect(htmlOutput).toContain(`<h1>foo</h1><h2>Headline</h2>
2833
<!-- --><p>hello <!-- -->jeff<!-- --></p><button>Count: <!-- -->0<!-- --></button>
2934
<!-- --><p class=\"context\">Context value: \"<!-- -->foo<!-- -->\"<!-- --></p>
3035
<!-- --><p>Some <!-- --><strong class=\"custom-strong\">markdown</strong> content<!-- --></p>
@@ -73,9 +78,36 @@ describe('hydration', () => {
7378
})
7479
})
7580

76-
afterAll(async () => {
77-
await rmfr(path.join(__dirname, 'fixtures/basic/out'))
78-
await rmfr(path.join(__dirname, 'fixtures/basic/.next'))
81+
describe('hydration - dev server', () => {
82+
let childProcess
83+
let browser: Browser
84+
85+
beforeAll(async () => {
86+
childProcess = await startDevServer('basic')
87+
browser = await puppeteer.launch()
88+
})
89+
90+
afterAll(async () => {
91+
// close the browser and dev server
92+
await stopDevServer(childProcess)
93+
await browser.close()
94+
await cleanupNextDirectory('basic')
95+
})
96+
97+
test('loads in development', async () => {
98+
const page = await browser.newPage()
99+
page.on('console', (msg) => console.log(msg.text()))
100+
101+
await page.goto('http://localhost:12333')
102+
103+
// @ts-expect-error
104+
await page.waitForFunction(() => Boolean(window.__NEXT_HYDRATED))
105+
106+
// @ts-expect-error -- el is typed as Element, but reasonable to assume it is an HTMLElement at this point
107+
const headingText = await page.$eval('h1', (el) => el.innerText)
108+
109+
expect(headingText).toEqual('foo')
110+
})
79111
})
80112

81113
//
@@ -112,3 +144,53 @@ function serveStatic(fixture: string): Promise<Server> {
112144
server.listen(1235, () => resolve(server))
113145
})
114146
}
147+
148+
async function cleanupNextDirectory(fixture: string) {
149+
await rmfr(path.join(__dirname, `fixtures/${fixture}/out`))
150+
await rmfr(path.join(__dirname, `fixtures/${fixture}/.next`))
151+
}
152+
153+
async function startDevServer(fixture: string) {
154+
const dir = path.join(__dirname, 'fixtures', fixture)
155+
156+
const childProcess = spawn('next', ['dev', '-p', '12333'], {
157+
stdio: ['ignore', 'pipe', 'pipe'],
158+
cwd: dir,
159+
env: { ...process.env, NODE_ENV: 'development', __NEXT_TEST_MODE: 'true' },
160+
})
161+
162+
childProcess.stderr?.on('data', (chunk) => {
163+
process.stdout.write(chunk)
164+
})
165+
166+
async function waitForStarted() {
167+
return new Promise<undefined>((resolve) => {
168+
childProcess.stdout?.on('data', (chunk) => {
169+
const msg = chunk.toString()
170+
process.stdout.write(chunk)
171+
172+
if (msg.includes('started server on') && msg.includes('url:')) {
173+
resolve(undefined)
174+
}
175+
})
176+
})
177+
}
178+
179+
await waitForStarted()
180+
181+
return childProcess
182+
}
183+
184+
async function stopDevServer(childProcess: ChildProcess) {
185+
console.log('stopping development server...')
186+
const promise = new Promise((resolve) => {
187+
childProcess.on('close', () => {
188+
console.log('development server stopped')
189+
resolve(undefined)
190+
})
191+
})
192+
193+
childProcess.kill('SIGINT')
194+
195+
await promise
196+
}

__tests__/serialize.test.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,10 +155,10 @@ hello: world
155155
} catch (error) {
156156
expect(error).toMatchInlineSnapshot(`
157157
[Error: [next-mdx-remote] error compiling MDX:
158-
Cannot close \`paragraph\` (1:1-1:31): a different token (\`mdxJsxTextTag\`, 1:18-1:31) is open
158+
Expected a closing tag for \`<GITHUB_USER>\` (1:18-1:31) before the end of \`paragraph\`
159159
160160
> 1 | This is very bad <GITHUB_USER>
161-
| ^
161+
| ^
162162
163163
More information: https://mdxjs.com/docs/troubleshooting-mdx]
164164
`)

0 commit comments

Comments
 (0)