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(