|
1 | 1 | /* eslint-env jest */ |
2 | 2 |
|
3 | | -import { runNextCommand, runNextCommandDev, findPort } from 'next-test-utils' |
| 3 | +import { |
| 4 | + runNextCommand, |
| 5 | + runNextCommandDev, |
| 6 | + findPort, |
| 7 | + launchApp, |
| 8 | +} from 'next-test-utils' |
4 | 9 | import { join } from 'path' |
5 | 10 | import pkg from 'next/package' |
| 11 | +import http from 'http' |
| 12 | + |
6 | 13 | jest.setTimeout(1000 * 60 * 5) |
7 | 14 |
|
8 | 15 | const dir = join(__dirname, '..') |
@@ -117,6 +124,39 @@ describe('CLI Usage', () => { |
117 | 124 | expect(output).toMatch(new RegExp(`http://localhost:${port}`)) |
118 | 125 | }) |
119 | 126 |
|
| 127 | + test('-p conflict', async () => { |
| 128 | + const port = await findPort() |
| 129 | + |
| 130 | + let app = http.createServer((_, res) => { |
| 131 | + res.writeHead(200, { 'Content-Type': 'text/plain' }) |
| 132 | + res.end('OK') |
| 133 | + }) |
| 134 | + await new Promise((resolve, reject) => { |
| 135 | + // This code catches EADDRINUSE error if the port is already in use |
| 136 | + app.on('error', reject) |
| 137 | + app.on('listening', () => resolve()) |
| 138 | + app.listen(port) |
| 139 | + }) |
| 140 | + let stdout = '', |
| 141 | + stderr = '' |
| 142 | + await launchApp(dir, port, { |
| 143 | + stdout: true, |
| 144 | + stderr: true, |
| 145 | + onStdout(msg) { |
| 146 | + stdout += msg |
| 147 | + }, |
| 148 | + onStderr(msg) { |
| 149 | + stderr += msg |
| 150 | + }, |
| 151 | + }) |
| 152 | + await new Promise((resolve) => app.close(resolve)) |
| 153 | + expect(stderr).toMatch('already in use') |
| 154 | + expect(stdout).not.toMatch('ready') |
| 155 | + expect(stdout).not.toMatch('started') |
| 156 | + expect(stdout).not.toMatch(`${port}`) |
| 157 | + expect(stdout).toBeFalsy() |
| 158 | + }) |
| 159 | + |
120 | 160 | test('--hostname', async () => { |
121 | 161 | const port = await findPort() |
122 | 162 | const output = await runNextCommandDev( |
|
0 commit comments