MCPcopy
hub / github.com/vercel/next.js / runTests

Function runTests

test/production/graceful-shutdown/index.test.ts:121–234  ·  view source on GitHub ↗
(dev = false)

Source from the content-addressed store, hash-verified

119})
120
121function runTests(dev = false) {
122 if (dev) {
123 it('should shut down child immediately', async () => {
124 const appKilledPromise = once(app, 'exit')
125
126 // let the dev server compile the route before running the test
127 await expect(
128 fetchViaHTTP(appPort, '/api/long-running')
129 ).resolves.toBeDefined()
130
131 const resPromise = fetchViaHTTP(appPort, '/api/long-running')
132
133 // yield event loop to kick off request before killing the app
134 await waitFor(20)
135 process.kill(app.pid, 'SIGTERM')
136 expect(app.exitCode).toBe(null)
137
138 // `next dev` should kill the child immediately
139 let start = Date.now()
140 await expect(resPromise).rejects.toThrow()
141 expect(Date.now() - start).toBeLessThan(LONG_RUNNING_MS)
142
143 // `next dev` parent process is still running cleanup
144 expect(app.exitCode).toBe(null)
145
146 // App finally shuts down
147 expect(await appKilledPromise).toEqual([0, null])
148 expect(app.exitCode).toBe(0)
149 })
150 } else {
151 // TODO: investigate this is constantly failing
152 it.skip('should wait for requests to complete before exiting', async () => {
153 const appKilledPromise = once(app, 'exit')
154
155 let responseResolved = false
156 const resPromise = fetchViaHTTP(appPort, '/api/long-running')
157 .then((res) => {
158 responseResolved = true
159 return res
160 })
161 .catch(() => {})
162
163 // yield event loop to kick off request before killing the app
164 await waitFor(20)
165 process.kill(app.pid, 'SIGTERM')
166 expect(app.exitCode).toBe(null)
167
168 // Long running response should still be running after a bit
169 await waitFor(LONG_RUNNING_MS / 2)
170 expect(app.exitCode).toBe(null)
171 expect(responseResolved).toBe(false)
172
173 // App responds as expected without being interrupted
174 const res = await resPromise
175 assertDefined(res)
176 expect(res.status).toBe(200)
177 expect(await res.json()).toStrictEqual({ hello: 'world' })
178

Callers 1

index.test.tsFile · 0.70

Calls 13

onceFunction · 0.90
fetchViaHTTPFunction · 0.90
waitForFunction · 0.90
assertDefinedFunction · 0.85
describeFunction · 0.85
thenMethod · 0.80
killMethod · 0.65
itFunction · 0.50
expectFunction · 0.50
nowMethod · 0.45
catchMethod · 0.45

Tested by

no test coverage detected