forked from nuxt/nuxt
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.test.js
More file actions
233 lines (196 loc) · 7.25 KB
/
utils.test.js
File metadata and controls
233 lines (196 loc) · 7.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
import { getDefaultNuxtConfig } from '@nuxt/config'
import { consola } from '../utils'
import { loadNuxtConfig } from '../../src/utils/config'
import * as utils from '../../src/utils'
import { showBanner } from '../../src/utils/banner'
import { showMemoryUsage } from '../../src/utils/memory'
import * as fmt from '../../src/utils/formatting'
jest.mock('std-env', () => ({
test: false,
minimalCLI: false
}))
jest.mock('boxen', () => text => `[boxen] ${text}`)
describe('cli/utils', () => {
afterEach(() => jest.resetAllMocks())
test('loadNuxtConfig: defaults', async () => {
const argv = {
_: ['.'],
'config-file': 'nuxt.config.js',
universal: true
}
const options = await loadNuxtConfig(argv)
expect(options.rootDir).toBe(process.cwd())
expect(options.mode).toBe('universal')
expect(options.server.host).toBe('localhost')
expect(options.server.port).toBe(3000)
expect(options.server.socket).not.toBeDefined()
})
test('loadNuxtConfig: config-file', async () => {
const argv = {
_: [__dirname],
'config-file': '../fixtures/nuxt.config.js',
spa: true
}
const options = await loadNuxtConfig(argv)
expect(options.testOption).toBe(true)
expect(options.rootDir).toBe('/some/path')
expect(options.mode).toBe('spa')
expect(options.server.host).toBe('nuxt-host')
expect(options.server.port).toBe(3001)
expect(options.server.socket).toBe('/var/run/nuxt.sock')
})
test('loadNuxtConfig: not-existing config-file', async () => {
const argv = {
_: [__dirname],
'config-file': '../fixtures/nuxt.doesnt-exist.js'
}
const options = await loadNuxtConfig(argv)
expect(options.testOption).not.toBeDefined()
expect(consola.fatal).toHaveBeenCalledTimes(1)
expect(consola.fatal).toHaveBeenCalledWith(expect.stringMatching(/Config file not found/))
})
test('loadNuxtConfig: async config-file', async () => {
const argv = {
_: [__dirname],
'config-file': '../fixtures/nuxt.async-config.js',
hostname: 'async-host',
port: 3002,
'unix-socket': '/var/run/async.sock'
}
const options = await loadNuxtConfig(argv)
expect(options.testOption).toBe(true)
expect(options.mode).toBe('supercharged')
expect(options.server.host).toBe('async-host')
expect(options.server.port).toBe(3002)
expect(options.server.socket).toBe('/var/run/async.sock')
})
test('loadNuxtConfig: passes context to config fn', async () => {
const argv = {
_: [__dirname],
'config-file': '../fixtures/nuxt.fn-config.js'
}
const context = { command: 'test', dev: true }
const options = await loadNuxtConfig(argv, context)
expect(options.context.command).toBe('test')
expect(options.context.dev).toBe(true)
})
test('loadNuxtConfig: async config-file with error', async () => {
const argv = {
_: [__dirname],
'config-file': '../fixtures/nuxt.async-error.js'
}
const options = await loadNuxtConfig(argv)
expect(options.testOption).not.toBeDefined()
expect(consola.error).toHaveBeenCalledTimes(1)
expect(consola.error).toHaveBeenCalledWith(new Error('Async Config Error'))
expect(consola.fatal).toHaveBeenCalledWith('Error while fetching async configuration')
})
test('normalizeArg: normalize string argument in command', () => {
expect(utils.normalizeArg('true')).toBe(true)
expect(utils.normalizeArg('false')).toBe(false)
expect(utils.normalizeArg(true)).toBe(true)
expect(utils.normalizeArg(false)).toBe(false)
expect(utils.normalizeArg('')).toBe(true)
expect(utils.normalizeArg(undefined, 'default')).toBe('default')
expect(utils.normalizeArg('text')).toBe('text')
})
test('nuxtServerConfig: server env', () => {
const options = getDefaultNuxtConfig({
env: {
...process.env,
HOST: 'env-host',
PORT: 3003,
UNIX_SOCKET: '/var/run/env.sock'
}
})
expect(options.server.host).toBe('env-host')
expect(options.server.port).toBe(3003)
expect(options.server.socket).toBe('/var/run/env.sock')
})
test('indent', () => {
expect(fmt.indent(4)).toBe(' ')
})
test('indent custom char', () => {
expect(fmt.indent(4, '-')).toBe('----')
})
test('showBanner prints full-info box with memory usage', () => {
const stdout = jest.spyOn(process.stdout, 'write').mockImplementation(() => {})
const successBox = jest.fn().mockImplementation((m, t) => t + m)
jest.spyOn(fmt, 'successBox').mockImplementation(successBox)
const badgeMessages = ['badgeMessage']
const bannerColor = 'green'
const listeners = [
{ url: 'first' },
{ url: 'second' }
]
showBanner({
options: {
cli: {
badgeMessages,
bannerColor
}
},
server: {
listeners
}
})
expect(successBox).toHaveBeenCalledTimes(1)
expect(stdout).toHaveBeenCalledTimes(1)
expect(stdout).toHaveBeenCalledWith(expect.stringMatching('Nuxt.js'))
expect(stdout).toHaveBeenCalledWith(expect.stringMatching(`Listening on: ${listeners[0].url}`))
expect(stdout).toHaveBeenCalledWith(expect.stringMatching(`Listening on: ${listeners[1].url}`))
expect(stdout).toHaveBeenCalledWith(expect.stringMatching('Memory usage'))
expect(stdout).toHaveBeenCalledWith(expect.stringMatching('badgeMessage'))
stdout.mockRestore()
})
test('showBanner doesnt print memory usage', () => {
const stdout = jest.spyOn(process.stdout, 'write').mockImplementation(() => {})
const successBox = jest.fn().mockImplementation((m, t) => t + m)
jest.spyOn(fmt, 'successBox').mockImplementation(successBox)
showBanner({
options: {
cli: {
badgeMessages: [],
bannerColor: 'green'
}
},
server: {
listeners: []
}
}, false)
expect(successBox).toHaveBeenCalledTimes(1)
expect(stdout).toHaveBeenCalledTimes(1)
expect(stdout).toHaveBeenCalledWith(expect.stringMatching('Nuxt.js'))
expect(stdout).not.toHaveBeenCalledWith(expect.stringMatching('Memory usage'))
stdout.mockRestore()
})
test('showMemoryUsage prints memory usage', () => {
showMemoryUsage()
expect(consola.info).toHaveBeenCalledTimes(1)
expect(consola.info).toHaveBeenCalledWith(expect.stringMatching('Memory usage'))
})
test('forceExit exits after timeout', () => {
jest.useFakeTimers()
const exit = jest.spyOn(process, 'exit').mockImplementation(() => {})
const stderr = jest.spyOn(process.stderr, 'write').mockImplementation(() => {})
utils.forceExit('test', 1)
expect(exit).not.toHaveBeenCalled()
jest.runAllTimers()
expect(stderr).toHaveBeenCalledWith(expect.stringMatching('Nuxt.js will now force exit'))
expect(exit).toHaveBeenCalledTimes(1)
stderr.mockRestore()
exit.mockRestore()
jest.useRealTimers()
})
test('forceExit exits immediately without timeout', () => {
jest.useFakeTimers()
const exit = jest.spyOn(process, 'exit').mockImplementation(() => {})
const stderr = jest.spyOn(process.stderr, 'write').mockImplementation(() => {})
utils.forceExit('test', false)
expect(stderr).not.toHaveBeenCalledWith()
expect(exit).toHaveBeenCalledTimes(1)
stderr.mockRestore()
exit.mockRestore()
jest.useRealTimers()
})
})