forked from nuxt/nuxt
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.listen.test.js
More file actions
79 lines (60 loc) · 2.25 KB
/
server.listen.test.js
File metadata and controls
79 lines (60 loc) · 2.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import consola from 'consola'
import { loadFixture, getPort, Nuxt } from '../utils'
let config
describe('server listen', () => {
beforeAll(async () => {
config = await loadFixture('empty')
})
test('should throw error when listening on same port (prod)', async () => {
const nuxt = new Nuxt(config)
await nuxt.ready()
const port = await getPort()
const listen = () => nuxt.server.listen(port, 'localhost')
// Listen for first time
await listen()
expect(nuxt.server.listeners[0].port).toBe(port)
// Listen for second time
await expect(listen()).rejects.toThrow(`Address \`localhost:${port}\` is already in use.`)
await nuxt.close()
})
test('should assign a random port when listening on same port (dev)', async () => {
const nuxt = new Nuxt({ ...config, dev: true })
await nuxt.ready()
const port = await getPort()
const listen = () => nuxt.server.listen(port, 'localhost')
// Listen for first time
await listen()
expect(nuxt.server.listeners[0].port).toBe(port)
// Listen for second time
await listen()
expect(nuxt.server.listeners[1].port).not.toBe(nuxt.server.listeners[0].port)
expect(consola.warn).toHaveBeenCalledTimes(1)
expect(consola.warn).toHaveBeenCalledWith(`Address \`localhost:${port}\` is already in use.`)
await nuxt.close()
})
test('should skip the use of default port when listening on port 0', async () => {
// Stub process.env.PORT
const stubDetails = {
originalValue: process.env.PORT,
hasProperty: 'PORT' in process.env
}
const DEFAULT_PORT = '2999'
process.env.PORT = DEFAULT_PORT
// Setup test
const nuxt = new Nuxt({ ...config, dev: true })
await nuxt.ready()
const listen = () => nuxt.server.listen(0, 'localhost') // Use port 0 to let allow host to randomly assign a free PORT
const toString = (x = '') => `${x}`
// Nuxt server should not be listening on the DEFAULT_PORT
await listen()
expect(toString(nuxt.server.listeners[0].port)).not.toBe(DEFAULT_PORT)
// Reset stub for process.env.PORT
if (stubDetails.hasProperty) {
process.env.PORT = stubDetails.originalValue
} else {
delete process.env.PORT
}
// Finalize test
await nuxt.close()
})
})