Skip to content

Commit

Permalink
refactor: combine graceful-shutdown tests
Browse files Browse the repository at this point in the history
rather than having an integration test with dev and production modes,
and then a separate test for standalone-mode that copies all the same
tests, we can just follow the same pattern we had in integration but
include the standalone-mode tests in the same file.
  • Loading branch information
redbmk committed Dec 30, 2023
1 parent c739e11 commit 4f74aee
Show file tree
Hide file tree
Showing 6 changed files with 117 additions and 199 deletions.
Original file line number Diff line number Diff line change
@@ -1,27 +1,120 @@
/* eslint-env jest */

import { join } from 'path'
import { NextInstance, createNext, FileRef } from 'e2e-utils'
import {
fetchViaHTTP,
findPort,
initNextServerScript,
isAppRunning,
killApp,
launchApp,
nextBuild,
nextStart,
waitFor,
} from 'next-test-utils'
import { LONG_RUNNING_MS } from '../pages/api/long-running'
import fs from 'fs-extra'
import glob from 'glob'
import { LONG_RUNNING_MS } from './pages/api/long-running'
import { once } from 'events'

const appDir = join(__dirname, '../')
let appPort
let app

function assertDefined<T>(value: T | void): asserts value is T {
expect(value).toBeDefined()
}

describe('Graceful Shutdown', () => {
describe('development (next dev)', () => {
beforeEach(async () => {
appPort = await findPort()
app = await launchApp(__dirname, appPort)
})
afterEach(() => killApp(app))

runTests(true)
})
;(process.env.TURBOPACK ? describe.skip : describe)(
'production (next start)',
() => {
beforeAll(async () => {
await nextBuild(__dirname)
})
beforeEach(async () => {
appPort = await findPort()
app = await nextStart(__dirname, appPort)
})
afterEach(() => killApp(app))

runTests()
}
)
;(process.env.TURBOPACK ? describe.skip : describe)(
'production (standalone mode)',
() => {
let next: NextInstance
let serverFile

const projectFiles = {
'next.config.mjs': `export default { output: 'standalone' }`,
}

for (const file of glob.sync('*', { cwd: __dirname, dot: false })) {
projectFiles[file] = new FileRef(join(__dirname, file))
}

beforeAll(async () => {
next = await createNext({
files: projectFiles,
dependencies: {
swr: 'latest',
},
})

await next.stop()

await fs.move(
join(next.testDir, '.next/standalone'),
join(next.testDir, 'standalone')
)

for (const file of await fs.readdir(next.testDir)) {
if (file !== 'standalone') {
await fs.remove(join(next.testDir, file))
}
}
const files = glob.sync('**/*', {
cwd: join(next.testDir, 'standalone/.next/server/pages'),
dot: true,
})

for (const file of files) {
if (file.endsWith('.json') || file.endsWith('.html')) {
await fs.remove(join(next.testDir, '.next/server', file))
}
}

serverFile = join(next.testDir, 'standalone/server.js')
})

beforeEach(async () => {
appPort = await findPort()
app = await initNextServerScript(
serverFile,
/- Local:/,
{ ...process.env, PORT: appPort.toString() },
undefined,
{ cwd: next.testDir }
)
})
afterEach(() => killApp(app))

afterAll(() => next.destroy())

runTests()
}
)
})

function runTests(dev = false) {
if (dev) {
it('should shut down child immediately', async () => {
Expand Down Expand Up @@ -128,41 +221,16 @@ function runTests(dev = false) {
process.kill(app.pid, 'SIGTERM')
expect(isAppRunning(app)).toBe(true)

// yield event loop to allow server to start the shutdown process
await waitFor(20)
await expect(
fetchViaHTTP(appPort, '/api/long-running')
).rejects.toThrow()

// App is still running briefly while server is closing
expect(isAppRunning(app)).toBe(true)

// App finally shuts down
await appKilledPromise
expect(isAppRunning(app)).toBe(false)
})
})
}
}

describe('API routes', () => {
describe('dev support', () => {
beforeEach(async () => {
appPort = await findPort()
app = await launchApp(appDir, appPort)
})
afterEach(() => killApp(app))

runTests(true)
})
;(process.env.TURBOPACK ? describe.skip : describe)('production mode', () => {
beforeAll(async () => {
await nextBuild(appDir)
})
beforeEach(async () => {
appPort = await findPort()
app = await nextStart(appDir, appPort)
})
afterEach(() => killApp(app))

runTests()
})
})
18 changes: 18 additions & 0 deletions test/production/graceful-shutdown/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"compilerOptions": {
"lib": ["dom", "dom.iterable", "esnext"],
"allowJs": true,
"skipLibCheck": true,
"strict": false,
"noEmit": true,
"incremental": true,
"esModuleInterop": true,
"module": "esnext",
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"jsx": "preserve"
},
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx"],
"exclude": ["node_modules"]
}
159 changes: 0 additions & 159 deletions test/production/standalone-mode/graceful-shutdown/index.test.ts

This file was deleted.

This file was deleted.

This file was deleted.

0 comments on commit 4f74aee

Please sign in to comment.