From e30d65c259de2e021e48cb70363f9f832ef9b158 Mon Sep 17 00:00:00 2001 From: Joe Haddad Date: Thu, 6 Aug 2020 00:03:14 -0400 Subject: [PATCH] Delay server start until it's listening --- packages/next/cli/next-dev.ts | 3 +- test/integration/cli/test/index.test.js | 42 ++++++++++++++++++++++++- 2 files changed, 42 insertions(+), 3 deletions(-) diff --git a/packages/next/cli/next-dev.ts b/packages/next/cli/next-dev.ts index 4c9b9ae1962f5..c3c66f1c489dd 100755 --- a/packages/next/cli/next-dev.ts +++ b/packages/next/cli/next-dev.ts @@ -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) => { diff --git a/test/integration/cli/test/index.test.js b/test/integration/cli/test/index.test.js index 311ad5a4d04ee..4dde3d7f2bc0d 100644 --- a/test/integration/cli/test/index.test.js +++ b/test/integration/cli/test/index.test.js @@ -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, '..') @@ -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(