Skip to content

Commit

Permalink
Ensure required-server-files test can exit cleanly
Browse files Browse the repository at this point in the history
  • Loading branch information
unstubbable committed Jun 11, 2024
1 parent 0fee50e commit 96d5c9a
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 110 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import fs from 'fs-extra'

export interface PatchServerFileOptions {
readonly minimalMode: boolean
}

export async function patchServerFile(
serverFilename: string,
options: PatchServerFileOptions
): Promise<void> {
const { minimalMode } = options
const content = await fs.readFile(serverFilename, 'utf8')

await fs.writeFile(
serverFilename,
content.replace(
/(startServer\({\s*)(minimalMode: (true|false),\n {2})?/,
`$1minimalMode: ${minimalMode},\n `
)
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
killApp,
} from 'next-test-utils'
import { ChildProcess } from 'child_process'
import { patchServerFile } from './patch-server-file'

describe('required server files app router', () => {
let next: NextInstance
Expand Down Expand Up @@ -70,17 +71,11 @@ describe('required server files app router', () => {
}
}

const testServer = join(next.testDir, 'standalone/server.js')
await fs.writeFile(
testServer,
(await fs.readFile(testServer, 'utf8')).replace(
'port:',
`minimalMode: ${minimalMode},port:`
)
)
const testServerFilename = join(next.testDir, 'standalone/server.js')
await patchServerFile(testServerFilename, { minimalMode })
appPort = await findPort()
server = await initNextServerScript(
testServer,
testServerFilename,
/- Local:/,
{
...process.env,
Expand All @@ -91,10 +86,6 @@ describe('required server files app router', () => {
cwd: next.testDir,
}
)

if (process.platform === 'darwin') {
appPort = `http://127.0.0.1:${appPort}`
}
}

beforeAll(async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
} from 'next-test-utils'
import nodeFetch from 'node-fetch'
import { ChildProcess } from 'child_process'
import { patchServerFile } from './patch-server-file'

describe('required server files i18n', () => {
let next: NextInstance
Expand Down Expand Up @@ -104,17 +105,11 @@ describe('required server files i18n', () => {
}
}

const testServer = join(next.testDir, 'standalone/server.js')
await fs.writeFile(
testServer,
(await fs.readFile(testServer, 'utf8')).replace(
'port:',
'minimalMode: true,port:'
)
)
const testServerFilename = join(next.testDir, 'standalone/server.js')
await patchServerFile(testServerFilename, { minimalMode: true })
appPort = await findPort()
server = await initNextServerScript(
testServer,
testServerFilename,
/- Local:/,
{
...process.env,
Expand All @@ -128,10 +123,6 @@ describe('required server files i18n', () => {
},
}
)

if (process.platform === 'darwin') {
appPort = `http://127.0.0.1:${appPort}`
}
})

afterAll(async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
killApp,
} from 'next-test-utils'
import { ChildProcess } from 'child_process'
import { patchServerFile } from './patch-server-file'

describe('required server files app router', () => {
let next: NextInstance
Expand Down Expand Up @@ -80,17 +81,11 @@ describe('required server files app router', () => {
}
}

const testServer = join(next.testDir, 'standalone/server.js')
await fs.writeFile(
testServer,
(await fs.readFile(testServer, 'utf8')).replace(
'port:',
`minimalMode: ${minimalMode},port:`
)
)
const testServerFilename = join(next.testDir, 'standalone/server.js')
await patchServerFile(testServerFilename, { minimalMode })
appPort = await findPort()
server = await initNextServerScript(
testServer,
testServerFilename,
/- Local:/,
{
...process.env,
Expand All @@ -101,10 +96,6 @@ describe('required server files app router', () => {
cwd: next.testDir,
}
)

if (process.platform === 'darwin') {
appPort = `http://127.0.0.1:${appPort}`
}
}

beforeAll(async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
waitFor,
} from 'next-test-utils'
import { ChildProcess } from 'child_process'
import { patchServerFile } from './patch-server-file'

describe('required server files', () => {
let next: NextInstance
Expand All @@ -24,14 +25,9 @@ describe('required server files', () => {
let errors = []
let stderr = ''
let requiredFilesManifest
let minimalMode = true

const setupNext = async ({
nextEnv,
minimalMode,
}: {
nextEnv?: boolean
minimalMode?: boolean
}) => {
const setupNext = async ({ nextEnv }: { nextEnv?: boolean }) => {
// test build against environment with next support
process.env.NOW_BUILDER = nextEnv ? '1' : ''

Expand Down Expand Up @@ -109,18 +105,23 @@ describe('required server files', () => {
await fs.remove(join(next.testDir, '.next/server', file))
}
}
}

beforeAll(async () => {
await setupNext({ nextEnv: true })
})

beforeEach(async () => {
errors = []
stderr = ''

const testServerFilename = join(next.testDir, 'standalone/server.js')
await patchServerFile(testServerFilename, { minimalMode })

const testServer = join(next.testDir, 'standalone/server.js')
await fs.writeFile(
testServer,
(await fs.readFile(testServer, 'utf8')).replace(
'port:',
`minimalMode: ${minimalMode},port:`
)
)
appPort = await findPort()

server = await initNextServerScript(
testServer,
testServerFilename,
/- Local:/,
{
...process.env,
Expand All @@ -136,24 +137,14 @@ describe('required server files', () => {
},
}
)

if (process.platform === 'darwin') {
appPort = `http://127.0.0.1:${appPort}`
}
}

beforeAll(async () => {
await setupNext({ nextEnv: true, minimalMode: true })
})

beforeEach(() => {
errors = []
stderr = ''
afterEach(async () => {
await killApp(server)
})

afterAll(async () => {
await next.destroy()
if (server) await killApp(server)
})

it('should resolve correctly when a redirect is returned', async () => {
Expand Down Expand Up @@ -1279,51 +1270,29 @@ describe('required server files', () => {
expect(envVariables.envFromHost).toBe('FOOBAR')
})

// FIXME: update to not mutate the global state
it('should run middleware correctly (without minimalMode, with wasm)', async () => {
const standaloneDir = join(next.testDir, 'standalone')

const testServer = join(standaloneDir, 'server.js')
await fs.writeFile(
testServer,
(await fs.readFile(testServer, 'utf8')).replace(
'minimalMode: true',
'minimalMode: false'
)
)
appPort = await findPort()
server = await initNextServerScript(
testServer,
/- Local:/,
{
...process.env,
PORT: `${appPort}`,
},
undefined,
{
cwd: next.testDir,
onStderr(msg) {
errors.push(msg)
stderr += msg
},
}
)
describe('without minimalMode, with wasm', () => {
beforeAll(() => {
minimalMode = false
})

const res = await fetchViaHTTP(appPort, '/')
expect(res.status).toBe(200)
expect(await res.text()).toContain('index page')
it('should run middleware correctly', async () => {
const standaloneDir = join(next.testDir, 'standalone')
const res = await fetchViaHTTP(appPort, '/')
expect(res.status).toBe(200)
expect(await res.text()).toContain('index page')

expect(fs.existsSync(join(standaloneDir, '.next/server/edge-chunks'))).toBe(
true
)
expect(
fs.existsSync(join(standaloneDir, '.next/server/edge-chunks'))
).toBe(true)

const resImageResponse = await fetchViaHTTP(
appPort,
'/a-non-existent-page/to-test-with-middleware'
)
const resImageResponse = await fetchViaHTTP(
appPort,
'/a-non-existent-page/to-test-with-middleware'
)

expect(resImageResponse.status).toBe(200)
expect(resImageResponse.headers.get('content-type')).toBe('image/png')
expect(resImageResponse.status).toBe(200)
expect(resImageResponse.headers.get('content-type')).toBe('image/png')
})
})

it('should correctly handle a mismatch in buildIds when normalizing next data', async () => {
Expand Down
4 changes: 2 additions & 2 deletions test/turbopack-build-tests-manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -15664,11 +15664,11 @@
"required server files should resolve correctly when a redirect is returned",
"required server files should return data correctly with x-matched-path",
"required server files should return data correctly with x-matched-path for optional catch-all route",
"required server files should run middleware correctly (without minimalMode, with wasm)",
"required server files should set correct SWR headers with notFound gsp",
"required server files should set correct SWR headers with notFound gssp",
"required server files should show invariant when an automatic static page is requested",
"required server files should warn when \"next\" is imported directly"
"required server files should warn when \"next\" is imported directly",
"required server files without minimalMode, with wasm should run middleware correctly"
],
"pending": [],
"flakey": [],
Expand Down

0 comments on commit 96d5c9a

Please sign in to comment.