Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Delay server start message until it's listening #15929

Merged
merged 2 commits into from
Aug 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions packages/next/cli/next-dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,13 @@ const nextDev: cliCommand = (argv) => {
const port = args['--port'] || 3000
const appUrl = `http://${args['--hostname'] || 'localhost'}:${port}`

startedDevelopmentServer(appUrl)

startServer(
{ dir, dev: true, isNextDevCommand: true },
port,
args['--hostname']
)
.then(async (app) => {
startedDevelopmentServer(appUrl)
await app.prepare()
})
.catch((err) => {
Expand Down
42 changes: 41 additions & 1 deletion test/integration/cli/test/index.test.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
/* eslint-env jest */

import { runNextCommand, runNextCommandDev, findPort } from 'next-test-utils'
import {
runNextCommand,
runNextCommandDev,
findPort,
launchApp,
} from 'next-test-utils'
import { join } from 'path'
import pkg from 'next/package'
import http from 'http'

jest.setTimeout(1000 * 60 * 5)

const dir = join(__dirname, '..')
Expand Down Expand Up @@ -117,6 +124,39 @@ describe('CLI Usage', () => {
expect(output).toMatch(new RegExp(`http://localhost:${port}`))
})

test('-p conflict', async () => {
const port = await findPort()

let app = http.createServer((_, res) => {
res.writeHead(200, { 'Content-Type': 'text/plain' })
res.end('OK')
})
await new Promise((resolve, reject) => {
// This code catches EADDRINUSE error if the port is already in use
app.on('error', reject)
app.on('listening', () => resolve())
app.listen(port)
})
let stdout = '',
stderr = ''
await launchApp(dir, port, {
stdout: true,
stderr: true,
onStdout(msg) {
stdout += msg
},
onStderr(msg) {
stderr += msg
},
})
await new Promise((resolve) => app.close(resolve))
expect(stderr).toMatch('already in use')
expect(stdout).not.toMatch('ready')
expect(stdout).not.toMatch('started')
expect(stdout).not.toMatch(`${port}`)
expect(stdout).toBeFalsy()
})

test('--hostname', async () => {
const port = await findPort()
const output = await runNextCommandDev(
Expand Down