-
Notifications
You must be signed in to change notification settings - Fork 27k
/
base.ts
605 lines (545 loc) · 18.1 KB
/
base.ts
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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
import os from 'os'
import path from 'path'
import { existsSync, promises as fs, rmSync } from 'fs'
import treeKill from 'tree-kill'
import type { NextConfig } from 'next'
import { FileRef, isNextDeploy } from '../e2e-utils'
import { ChildProcess } from 'child_process'
import { createNextInstall } from '../create-next-install'
import { Span } from 'next/dist/trace'
import webdriver from '../next-webdriver'
import { renderViaHTTP, fetchViaHTTP, findPort } from 'next-test-utils'
import cheerio from 'cheerio'
import { once } from 'events'
import { BrowserInterface } from '../browsers/base'
import escapeStringRegexp from 'escape-string-regexp'
type Event = 'stdout' | 'stderr' | 'error' | 'destroy'
export type InstallCommand =
| string
| ((ctx: { dependencies: { [key: string]: string } }) => string)
export type PackageJson = {
dependencies?: { [key: string]: string }
[key: string]: unknown
}
export interface NextInstanceOpts {
files: FileRef | string | { [filename: string]: string | FileRef }
dependencies?: { [name: string]: string }
resolutions?: { [name: string]: string }
packageJson?: PackageJson
nextConfig?: NextConfig
installCommand?: InstallCommand
buildCommand?: string
startCommand?: string
env?: Record<string, string>
dirSuffix?: string
turbo?: boolean
forcedPort?: string
serverReadyPattern?: RegExp
}
/**
* Omit the first argument of a function
*/
type OmitFirstArgument<F> = F extends (
firstArgument: any,
...args: infer P
) => infer R
? (...args: P) => R
: never
export class NextInstance {
protected files: FileRef | { [filename: string]: string | FileRef }
protected nextConfig?: NextConfig
protected installCommand?: InstallCommand
protected buildCommand?: string
protected startCommand?: string
protected dependencies?: PackageJson['dependencies'] = {}
protected resolutions?: PackageJson['resolutions']
protected events: { [eventName: string]: Set<any> } = {}
public testDir: string
protected isStopping: boolean = false
protected isDestroyed: boolean = false
protected childProcess?: ChildProcess
protected _url: string
protected _parsedUrl: URL
protected packageJson?: PackageJson = {}
protected basePath?: string
public env: Record<string, string>
public forcedPort?: string
public dirSuffix: string = ''
public serverReadyPattern?: RegExp = / ✓ Ready in /
constructor(opts: NextInstanceOpts) {
this.env = {}
Object.assign(this, opts)
require('console').log('packageJson??', this.packageJson)
if (!isNextDeploy) {
this.env = {
...this.env,
// remove node_modules/.bin repo path from env
// to match CI $PATH value and isolate further
PATH: process.env.PATH.split(path.delimiter)
.filter((part) => {
return !part.includes(path.join('node_modules', '.bin'))
})
.join(path.delimiter),
}
}
}
protected async writeInitialFiles() {
// Handle case where files is a directory string
const files =
typeof this.files === 'string' ? new FileRef(this.files) : this.files
if (files instanceof FileRef) {
// if a FileRef is passed directly to `files` we copy the
// entire folder to the test directory
const stats = await fs.stat(files.fsPath)
if (!stats.isDirectory()) {
throw new Error(
`FileRef passed to "files" in "createNext" is not a directory ${files.fsPath}`
)
}
await fs.cp(files.fsPath, this.testDir, {
recursive: true,
filter(source) {
// we don't copy a package.json as it's manually written
// via the createNextInstall process
if (path.relative(files.fsPath, source) === 'package.json') {
return false
}
return true
},
})
} else {
for (const filename of Object.keys(files)) {
const item = files[filename]
const outputFilename = path.join(this.testDir, filename)
if (typeof item === 'string') {
await fs.mkdir(path.dirname(outputFilename), { recursive: true })
await fs.writeFile(outputFilename, item)
} else {
await fs.cp(item.fsPath, outputFilename, { recursive: true })
}
}
}
}
protected async createTestDir({
skipInstall = false,
parentSpan,
}: {
skipInstall?: boolean
parentSpan: Span
}) {
if (this.isDestroyed) {
throw new Error('next instance already destroyed')
}
await parentSpan
.traceChild('createTestDir')
.traceAsyncFn(async (rootSpan) => {
const skipIsolatedNext = !!process.env.NEXT_SKIP_ISOLATE
if (!skipIsolatedNext) {
require('console').log(
`Creating test directory with isolated next... (use NEXT_SKIP_ISOLATE=1 to opt-out)`
)
}
const tmpDir = skipIsolatedNext
? path.join(__dirname, '../../tmp')
: process.env.NEXT_TEST_DIR || (await fs.realpath(os.tmpdir()))
this.testDir = path.join(
tmpDir,
`next-test-${Date.now()}-${(Math.random() * 1000) | 0}${
this.dirSuffix
}`
)
const reactVersion =
process.env.NEXT_TEST_REACT_VERSION || '19.0.0-rc-49496d49-20240814'
const finalDependencies = {
react: reactVersion,
'react-dom': reactVersion,
'@types/react': 'latest',
'@types/react-dom': 'latest',
typescript: 'latest',
'@types/node': 'latest',
...this.dependencies,
...this.packageJson?.dependencies,
}
if (skipInstall || skipIsolatedNext) {
const pkgScripts = (this.packageJson['scripts'] as {}) || {}
await fs.mkdir(this.testDir, { recursive: true })
await fs.writeFile(
path.join(this.testDir, 'package.json'),
JSON.stringify(
{
...this.packageJson,
dependencies: {
...finalDependencies,
next:
process.env.NEXT_TEST_VERSION ||
require('next/package.json').version,
},
...(this.resolutions ? { resolutions: this.resolutions } : {}),
scripts: {
// since we can't get the build id as a build artifact, make it
// available under the static files
'post-build': 'cp .next/BUILD_ID .next/static/__BUILD_ID',
...pkgScripts,
build:
(pkgScripts['build'] || this.buildCommand || 'next build') +
' && pnpm post-build',
},
},
null,
2
)
)
} else {
if (
process.env.NEXT_TEST_STARTER &&
!this.dependencies &&
!this.installCommand &&
!this.packageJson &&
!isNextDeploy
) {
await fs.cp(process.env.NEXT_TEST_STARTER, this.testDir, {
recursive: true,
})
} else {
const { installDir } = await createNextInstall({
parentSpan: rootSpan,
dependencies: finalDependencies,
resolutions: this.resolutions ?? null,
installCommand: this.installCommand,
packageJson: this.packageJson,
dirSuffix: this.dirSuffix,
keepRepoDir: Boolean(process.env.NEXT_TEST_SKIP_CLEANUP),
})
this.testDir = installDir
}
require('console').log('created next.js install, writing test files')
}
await rootSpan
.traceChild('writeInitialFiles')
.traceAsyncFn(async () => {
await this.writeInitialFiles()
})
const testDirFiles = await fs.readdir(this.testDir)
let nextConfigFile = testDirFiles.find((file) =>
file.startsWith('next.config.')
)
if (nextConfigFile && this.nextConfig) {
throw new Error(
`nextConfig provided on "createNext()" and as a file "${nextConfigFile}", use one or the other to continue`
)
}
if (this.nextConfig || (isNextDeploy && !nextConfigFile)) {
const functions = []
const exportDeclare =
this.packageJson?.type === 'module'
? 'export default'
: 'module.exports = '
await fs.writeFile(
path.join(this.testDir, 'next.config.js'),
exportDeclare +
JSON.stringify(
{
...this.nextConfig,
} as NextConfig,
(key, val) => {
if (typeof val === 'function') {
functions.push(
val
.toString()
.replace(
new RegExp(`${val.name}[\\s]{0,}\\(`),
'function('
)
)
return `__func_${functions.length - 1}`
}
return val
},
2
).replace(/"__func_[\d]{1,}"/g, function (str) {
return functions.shift()
})
)
}
if (isNextDeploy) {
const fileName = path.join(
this.testDir,
nextConfigFile || 'next.config.js'
)
const content = await fs.readFile(fileName, 'utf8')
if (content.includes('basePath')) {
this.basePath =
content.match(/['"`]?basePath['"`]?:.*?['"`](.*?)['"`]/)?.[1] ||
''
}
await fs.writeFile(
fileName,
`${content}\n` +
`
// alias __NEXT_TEST_MODE for next-deploy as "_" is not a valid
// env variable during deploy
if (process.env.NEXT_PRIVATE_TEST_MODE) {
process.env.__NEXT_TEST_MODE = process.env.NEXT_PRIVATE_TEST_MODE
}
`
)
if (
testDirFiles.includes('node_modules') &&
!testDirFiles.includes('vercel.json')
) {
// Tests that include a patched node_modules dir won't automatically be uploaded to Vercel.
// We need to ensure node_modules is not excluded from the deploy files, and tweak the
// start + build commands to handle copying the patched node modules into the final.
// To be extra safe, we only do this if the test directory doesn't already have a custom vercel.json
require('console').log(
'Detected node_modules in the test directory, writing `vercel.json` and `.vercelignore` to ensure its included.'
)
await fs.writeFile(
path.join(this.testDir, 'vercel.json'),
JSON.stringify({
installCommand:
'mv node_modules node_modules.bak && npm i && cp -r node_modules.bak/* node_modules',
})
)
await fs.writeFile(
path.join(this.testDir, '.vercelignore'),
'!node_modules'
)
}
}
})
}
protected setServerReadyTimeout(
reject: (reason?: unknown) => void,
ms = 10_000
): NodeJS.Timeout {
return setTimeout(() => {
reject(
new Error(
`Failed to start server after ${ms}ms, waiting for this log pattern: ${this.serverReadyPattern}`
)
)
}, ms)
}
// normalize snapshots or stack traces being tested
// to a consistent test dir value since it's random
public normalizeTestDirContent(content) {
content = content.replace(
new RegExp(escapeStringRegexp(this.testDir), 'g'),
'TEST_DIR'
)
return content
}
public async clean() {
if (this.childProcess) {
throw new Error(`stop() must be called before cleaning`)
}
const keptFiles = [
'node_modules',
'package.json',
'yarn.lock',
'pnpm-lock.yaml',
]
for (const file of await fs.readdir(this.testDir)) {
if (!keptFiles.includes(file)) {
await fs.rm(path.join(this.testDir, file), {
recursive: true,
force: true,
})
}
}
await this.writeInitialFiles()
}
public async build(): Promise<{ exitCode?: number; cliOutput?: string }> {
throw new Error('Not implemented')
}
public async setup(parentSpan: Span): Promise<void> {
if (this.forcedPort === 'random') {
this.forcedPort = (await findPort()) + ''
console.log('Forced random port:', this.forcedPort)
}
}
public async start(useDirArg: boolean = false): Promise<void> {}
public async stop(): Promise<void> {
if (this.childProcess) {
this.isStopping = true
const closePromise = once(this.childProcess, 'close')
await new Promise<void>((resolve) => {
treeKill(this.childProcess.pid, 'SIGKILL', (err) => {
if (err) {
require('console').error('tree-kill', err)
}
resolve()
})
})
this.childProcess.kill('SIGKILL')
await closePromise
this.childProcess = undefined
this.isStopping = false
require('console').log(`Stopped next server`)
}
}
public async destroy(): Promise<void> {
try {
require('console').time('destroyed next instance')
if (this.isDestroyed) {
throw new Error(`next instance already destroyed`)
}
this.isDestroyed = true
this.emit('destroy', [])
await this.stop().catch(console.error)
if (process.env.TRACE_PLAYWRIGHT) {
await fs
.cp(
path.join(this.testDir, '.next/trace'),
path.join(
__dirname,
'../../traces',
`${path
.relative(
path.join(__dirname, '../../'),
process.env.TEST_FILE_PATH
)
.replace(/\//g, '-')}`,
`next-trace`
),
{ recursive: true }
)
.catch((e) => {
require('console').error(e)
})
}
if (!process.env.NEXT_TEST_SKIP_CLEANUP) {
// Faster than `await fs.rm`. Benchmark before change.
rmSync(this.testDir, { recursive: true, force: true })
}
require('console').timeEnd(`destroyed next instance`)
} catch (err) {
require('console').error('Error while destroying', err)
}
}
public get url() {
return this._url
}
public get appPort() {
return this._parsedUrl.port
}
public get buildId(): string {
return ''
}
public get cliOutput(): string {
return ''
}
// TODO: block these in deploy mode
public async hasFile(filename: string) {
return existsSync(path.join(this.testDir, filename))
}
public async readFile(filename: string) {
return fs.readFile(path.join(this.testDir, filename), 'utf8')
}
public async readJSON(filename: string) {
return JSON.parse(
await fs.readFile(path.join(this.testDir, filename), 'utf-8')
)
}
public async patchFile(
filename: string,
content: string | ((content: string) => string),
runWithTempContent?: (context: { newFile: boolean }) => Promise<void>
): Promise<{ newFile: boolean }> {
const outputPath = path.join(this.testDir, filename)
const newFile = !existsSync(outputPath)
await fs.mkdir(path.dirname(outputPath), { recursive: true })
const previousContent = newFile ? undefined : await this.readFile(filename)
await fs.writeFile(
outputPath,
typeof content === 'function' ? content(previousContent) : content
)
if (runWithTempContent) {
try {
await runWithTempContent({ newFile })
} finally {
if (previousContent === undefined) {
await fs.rm(outputPath)
} else {
await fs.writeFile(outputPath, previousContent)
}
}
}
return { newFile }
}
public async patchFileFast(filename: string, content: string) {
const outputPath = path.join(this.testDir, filename)
await fs.writeFile(outputPath, content)
}
public async renameFile(filename: string, newFilename: string) {
await fs.rename(
path.join(this.testDir, filename),
path.join(this.testDir, newFilename)
)
}
public async renameFolder(foldername: string, newFoldername: string) {
await fs.rename(
path.join(this.testDir, foldername),
path.join(this.testDir, newFoldername)
)
}
public async deleteFile(filename: string) {
await fs.rm(path.join(this.testDir, filename), {
recursive: true,
force: true,
})
}
/**
* Create new browser window for the Next.js app.
*/
public async browser(
...args: Parameters<OmitFirstArgument<typeof webdriver>>
): Promise<BrowserInterface> {
return webdriver(this.url, ...args)
}
/**
* Fetch the HTML for the provided page. This is a shortcut for `renderViaHTTP().then(html => cheerio.load(html))`.
*/
public async render$(
...args: Parameters<OmitFirstArgument<typeof renderViaHTTP>>
): Promise<ReturnType<typeof cheerio.load>> {
const html = await renderViaHTTP(this.url, ...args)
return cheerio.load(html)
}
/**
* Fetch the HTML for the provided page. This is a shortcut for `fetchViaHTTP().then(res => res.text())`.
*/
public async render(
...args: Parameters<OmitFirstArgument<typeof renderViaHTTP>>
) {
return renderViaHTTP(this.url, ...args)
}
/**
* Performs a fetch request to the NextInstance with the options provided.
*
* @param pathname the pathname on the NextInstance to fetch
* @param opts the optional options to pass to the underlying fetch
* @returns the fetch response
*/
public async fetch(
pathname: string,
opts?: import('node-fetch').RequestInit
) {
return fetchViaHTTP(this.url, pathname, null, opts)
}
public on(event: Event, cb: (...args: any[]) => any) {
if (!this.events[event]) {
this.events[event] = new Set()
}
this.events[event].add(cb)
}
public off(event: Event, cb: (...args: any[]) => any) {
this.events[event]?.delete(cb)
}
protected emit(event: Event, args: any[]) {
this.events[event]?.forEach((cb) => {
cb(...args)
})
}
}