forked from vercel/next.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnext-test-utils.js
352 lines (298 loc) · 8.7 KB
/
next-test-utils.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
import fetch from 'node-fetch'
import qs from 'querystring'
import http from 'http'
import express from 'express'
import path from 'path'
import getPort from 'get-port'
import spawn from 'cross-spawn'
import { readFileSync, writeFileSync, existsSync, unlinkSync } from 'fs'
import treeKill from 'tree-kill'
// `next` here is the symlink in `test/node_modules/next` which points to the root directory.
// This is done so that requiring from `next` works.
// The reason we don't import the relative path `../../dist/<etc>` is that it would lead to inconsistent module singletons
import server from 'next/dist/server/next'
import _pkg from 'next/package.json'
export const nextServer = server
export const pkg = _pkg
export function initNextServerScript (
scriptPath,
successRegexp,
env,
failRegexp
) {
return new Promise((resolve, reject) => {
const instance = spawn('node', [scriptPath], { env })
function handleStdout (data) {
const message = data.toString()
if (successRegexp.test(message)) {
resolve(instance)
}
process.stdout.write(message)
}
function handleStderr (data) {
const message = data.toString()
if (failRegexp && failRegexp.test(message)) {
instance.kill()
return reject(new Error('received failRegexp'))
}
process.stderr.write(message)
}
instance.stdout.on('data', handleStdout)
instance.stderr.on('data', handleStderr)
instance.on('close', () => {
instance.stdout.removeListener('data', handleStdout)
instance.stderr.removeListener('data', handleStderr)
})
instance.on('error', err => {
reject(err)
})
})
}
export function renderViaAPI (app, pathname, query) {
const url = `${pathname}${query ? `?${qs.stringify(query)}` : ''}`
return app.renderToHTML({ url }, {}, pathname, query)
}
export function renderViaHTTP (appPort, pathname, query) {
return fetchViaHTTP(appPort, pathname, query).then(res => res.text())
}
export function fetchViaHTTP (appPort, pathname, query, opts) {
const url = `http://localhost:${appPort}${pathname}${
query ? `?${qs.stringify(query)}` : ''
}`
return fetch(url, opts)
}
export function findPort () {
return getPort()
}
export function runNextCommand (argv, options = {}) {
const nextDir = path.dirname(require.resolve('next/package'))
const nextBin = path.join(nextDir, 'dist/bin/next')
const cwd = options.cwd || nextDir
// Let Next.js decide the environment
const env = { ...process.env, ...options.env, NODE_ENV: '' }
return new Promise((resolve, reject) => {
console.log(`Running command "next ${argv.join(' ')}"`)
const instance = spawn('node', [nextBin, ...argv], {
...options.spawnOptions,
cwd,
env,
stdio: ['ignore', 'pipe', 'pipe']
})
if (typeof options.instance === 'function') {
options.instance(instance)
}
let stderrOutput = ''
if (options.stderr) {
instance.stderr.on('data', function (chunk) {
stderrOutput += chunk
})
}
let stdoutOutput = ''
if (options.stdout) {
instance.stdout.on('data', function (chunk) {
stdoutOutput += chunk
})
}
instance.on('close', () => {
resolve({
stdout: stdoutOutput,
stderr: stderrOutput
})
})
instance.on('error', err => {
err.stdout = stdoutOutput
err.stderr = stderrOutput
reject(err)
})
})
}
export function runNextCommandDev (argv, stdOut, opts = {}) {
const cwd = path.dirname(require.resolve('next/package'))
const env = { ...process.env, NODE_ENV: undefined, ...opts.env }
return new Promise((resolve, reject) => {
const instance = spawn('node', ['dist/bin/next', ...argv], { cwd, env })
function handleStdout (data) {
const message = data.toString()
if (/ready on/i.test(message)) {
resolve(stdOut ? message : instance)
}
process.stdout.write(message)
}
function handleStderr (data) {
process.stderr.write(data.toString())
}
instance.stdout.on('data', handleStdout)
instance.stderr.on('data', handleStderr)
instance.on('close', () => {
instance.stdout.removeListener('data', handleStdout)
instance.stderr.removeListener('data', handleStderr)
})
instance.on('error', err => {
reject(err)
})
})
}
// Launch the app in dev mode.
export function launchApp (dir, port) {
return runNextCommandDev([dir, '-p', port])
}
export function nextBuild (dir, args = [], opts = {}) {
return runNextCommand(['build', dir, ...args], opts)
}
export function nextExport (dir, { outdir }) {
return runNextCommand(['export', dir, '--outdir', outdir])
}
export function nextStart (dir, port, opts = {}) {
return runNextCommandDev(['start', '-p', port, dir], undefined, opts)
}
export function buildTS (args = [], cwd, env = {}) {
cwd = cwd || path.dirname(require.resolve('next/package'))
env = { ...process.env, NODE_ENV: undefined, ...env }
return new Promise((resolve, reject) => {
const instance = spawn(
'node',
[require.resolve('typescript/lib/tsc'), ...args],
{ cwd, env }
)
let output = ''
const handleData = chunk => {
output += chunk.toString()
}
instance.stdout.on('data', handleData)
instance.stderr.on('data', handleData)
instance.on('exit', code => {
if (code) {
return reject(new Error('exited with code: ' + code + '\n' + output))
}
resolve()
})
})
}
// Kill a launched app
export async function killApp (instance) {
await new Promise((resolve, reject) => {
treeKill(instance.pid, err => {
if (err) return reject(err)
resolve()
})
})
}
export async function startApp (app) {
await app.prepare()
const handler = app.getRequestHandler()
const server = http.createServer(handler)
server.__app = app
await promiseCall(server, 'listen')
return server
}
export async function stopApp (server) {
if (server.__app) {
await server.__app.close()
}
await promiseCall(server, 'close')
}
function promiseCall (obj, method, ...args) {
return new Promise((resolve, reject) => {
const newArgs = [
...args,
function (err, res) {
if (err) return reject(err)
resolve(res)
}
]
obj[method](...newArgs)
})
}
export function waitFor (millis) {
return new Promise(resolve => setTimeout(resolve, millis))
}
export async function startStaticServer (dir) {
const app = express()
const server = http.createServer(app)
app.use(express.static(dir))
await promiseCall(server, 'listen')
return server
}
export async function check (contentFn, regex) {
let found = false
const timeout = setTimeout(async () => {
if (found) {
return
}
let content
try {
content = await contentFn()
} catch (err) {
console.error('Error while getting content', { regex })
}
console.error('TIMED OUT CHECK: ', { regex, content })
throw new Error('TIMED OUT: ' + regex + '\n\n' + content)
}, 1000 * 30)
while (!found) {
try {
const newContent = await contentFn()
if (regex.test(newContent)) {
found = true
clearTimeout(timeout)
break
}
await waitFor(1000)
} catch (ex) {}
}
}
export class File {
constructor (path) {
this.path = path
this.originalContent = existsSync(this.path)
? readFileSync(this.path, 'utf8')
: null
}
write (content) {
if (!this.originalContent) {
this.originalContent = content
}
writeFileSync(this.path, content, 'utf8')
}
replace (pattern, newValue) {
const newContent = this.originalContent.replace(pattern, newValue)
this.write(newContent)
}
delete () {
unlinkSync(this.path)
}
restore () {
this.write(this.originalContent)
}
}
// react-error-overlay uses an iframe so we have to read the contents from the frame
export async function getReactErrorOverlayContent (browser) {
let found = false
setTimeout(() => {
if (found) {
return
}
console.error('TIMED OUT CHECK FOR IFRAME')
throw new Error('TIMED OUT CHECK FOR IFRAME')
}, 1000 * 30)
while (!found) {
try {
await browser.waitForElementByCss('iframe', 10000)
const hasIframe = await browser.hasElementByCssSelector('iframe')
if (!hasIframe) {
throw new Error('Waiting for iframe')
}
found = true
return browser.eval(
`document.querySelector('iframe').contentWindow.document.body.innerHTML`
)
} catch (ex) {
await waitFor(1000)
}
}
return browser.eval(
`document.querySelector('iframe').contentWindow.document.body.innerHTML`
)
}
export function getBrowserBodyText (browser) {
return browser.eval('document.getElementsByTagName("body")[0].innerText')
}