-
-
Notifications
You must be signed in to change notification settings - Fork 4.8k
/
options.test.js
286 lines (245 loc) · 10.2 KB
/
options.test.js
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
import path from 'path'
import consola from 'consola'
import { getNuxtConfig } from '../src/options'
jest.mock('std-env', () => ({
browser: false,
test: 'test',
dev: false,
production: true,
debug: false,
ci: true,
windows: false,
darwin: false,
linux: true
}))
jest.mock('@nuxt/utils', () => ({
...jest.requireActual('@nuxt/utils'),
getMainModule: () => ({ paths: ['/var/nuxt/node_modules'] })
}))
describe('config: options', () => {
test('should return default nuxt config', () => {
jest.spyOn(process, 'cwd').mockReturnValue('/var/nuxt/test')
jest.spyOn(path, 'resolve').mockImplementation((...args) => args.join('/').replace(/\\+/, '/'))
jest.spyOn(path, 'join').mockImplementation((...args) => args.join('/').replace(/\\+/, '/'))
expect(getNuxtConfig({})).toMatchSnapshot()
process.cwd.mockRestore()
path.resolve.mockRestore()
path.join.mockRestore()
})
test('should prevent duplicate calls with same options', () => {
const options = {}
const firstConfig = getNuxtConfig(options)
const secondConfig = getNuxtConfig(firstConfig)
expect(firstConfig).toBe(secondConfig)
expect(firstConfig.__normalized__).toBe(true)
})
test('should return default loading when loading is true', () => {
const { loading } = getNuxtConfig({ loading: true })
expect(loading).toEqual({
color: 'black',
failedColor: 'red',
height: '2px',
throttle: 200,
duration: 5000,
continuous: false,
rtl: false,
css: true
})
})
test('[Compatibility] should transform transition to pageTransition', () => {
const { pageTransition, transition } = getNuxtConfig({
transition: 'test-tran'
})
expect(pageTransition).toMatchObject({ name: 'test-tran' })
expect(transition).toBeUndefined()
})
test('should transform pageTransition/layoutTransition to name', () => {
const { pageTransition, layoutTransition } = getNuxtConfig({
pageTransition: 'test-tran',
layoutTransition: 'test-layout-tran'
})
expect(pageTransition).toMatchObject({ name: 'test-tran' })
expect(layoutTransition).toMatchObject({ name: 'test-layout-tran' })
})
test('should transform extensions to array', () => {
const { extensions } = getNuxtConfig({ extensions: 'ext' })
expect(extensions).toEqual(['js', 'mjs', 'ext'])
})
test('should support custom global name', () => {
const { globalName } = getNuxtConfig({ globalName: 'globalNuxt' })
expect(globalName).toEqual('globalnuxt')
})
test('should detect store dir', () => {
const { store } = getNuxtConfig({ rootDir: path.resolve(__dirname, 'fixtures') })
expect(store).toEqual(true)
})
test('should unset and warn when etag.hash not a function', () => {
const { render: { etag } } = getNuxtConfig({ render: { etag: { hash: true } } })
expect(etag).toMatchObject({ hash: undefined })
expect(consola.warn).not.toHaveBeenCalledWith('render.etag.hash should be a function, received boolean instead')
const { render: { etag: etagDev } } = getNuxtConfig({ dev: true, render: { etag: { hash: true } } })
expect(etagDev).toMatchObject({ hash: undefined })
expect(consola.warn).toHaveBeenCalledWith('render.etag.hash should be a function, received boolean instead')
})
test('should enable csp', () => {
const { render: { csp } } = getNuxtConfig({ render: { csp: { allowedSources: true, test: true } } })
expect(csp).toEqual({
hashAlgorithm: 'sha256',
addMeta: false,
unsafeInlineCompatiblity: false,
allowedSources: true,
policies: undefined,
reportOnly: false,
test: true
})
})
test('should check unknown mode', () => {
const { build, render } = getNuxtConfig({ mode: 'test' })
expect(consola.warn).toHaveBeenCalledWith('Unknown mode: test. Falling back to universal')
expect(build.ssr).toEqual(true)
expect(render.ssr).toEqual(true)
})
test('should add appear true in pageTransition when no ssr', () => {
const { pageTransition } = getNuxtConfig({ render: { ssr: false } })
expect(pageTransition.appear).toEqual(true)
})
test('should return 200.html as default generate.fallback', () => {
const { generate: { fallback } } = getNuxtConfig({})
expect(fallback).toEqual('200.html')
})
test('should return 404.html when generate.fallback is true', () => {
const { generate: { fallback } } = getNuxtConfig({ generate: { fallback: true } })
expect(fallback).toEqual('404.html')
})
test('should return fallback html when generate.fallback is string', () => {
const { generate: { fallback } } = getNuxtConfig({ generate: { fallback: 'fallback.html' } })
expect(fallback).toEqual('fallback.html')
})
test('should disable parallel if extractCSS is enabled', () => {
const { build: { parallel } } = getNuxtConfig({ build: { extractCSS: true, parallel: true } })
expect(parallel).toEqual(false)
expect(consola.warn).toHaveBeenCalledWith('extractCSS cannot work with parallel build due to limited work pool in thread-loader')
})
describe('config: router dir', () => {
test('should transform middleware to array', () => {
const { router: { middleware } } = getNuxtConfig({ router: { middleware: 'midd' } })
expect(middleware).toEqual(['midd'])
})
test('should set _routerBaseSpecified when base is specified', () => {
const { _routerBaseSpecified } = getNuxtConfig({ router: { base: '/test' } })
expect(_routerBaseSpecified).toEqual(true)
})
})
describe('config: options dir', () => {
test('should support custom root dir', () => {
const { rootDir } = getNuxtConfig({
rootDir: 'root'
})
expect(rootDir).toEqual(path.resolve('root'))
})
test('should support custom src dir', () => {
const { srcDir } = getNuxtConfig({
rootDir: 'root',
srcDir: 'src'
})
expect(srcDir).toEqual(path.resolve('root', 'src'))
})
test('should support custom generate dir', () => {
const { generate: { dir } } = getNuxtConfig({
rootDir: 'root',
generate: { dir: 'generate' }
})
expect(dir).toEqual(path.resolve('root', 'generate'))
})
})
describe('config: options template', () => {
test('should use default appTemplatePath', () => {
const { appTemplatePath } = getNuxtConfig({})
expect(appTemplatePath).toEqual(path.resolve('.nuxt', 'views', 'app.template.html'))
})
test('should use custom appTemplatePath', () => {
const { appTemplatePath } = getNuxtConfig({ appTemplatePath: 'templates' })
expect(appTemplatePath).toEqual(path.resolve('templates'))
})
test('should use custom app.html', () => {
const { appTemplatePath } = getNuxtConfig({ rootDir: path.resolve(__dirname, 'fixtures') })
expect(appTemplatePath).toEqual(path.resolve(__dirname, 'fixtures', 'app.html'))
})
})
describe('config: options publicPath', () => {
test('should fallback to default when publicPath is falsy', () => {
const { build: { publicPath } } = getNuxtConfig({ build: { publicPath: false } })
expect(publicPath).toEqual('/_nuxt/')
})
test('should append slash in publicPath', () => {
const { build: { publicPath } } = getNuxtConfig({ build: { publicPath: '/nuxt_public' } })
expect(publicPath).toEqual('/nuxt_public/')
})
test('should ignore url publicPath in dev', () => {
const { build: { publicPath } } = getNuxtConfig({ dev: true, build: { publicPath: 'http://nuxt_public' } })
expect(publicPath).toEqual('/_nuxt/')
})
})
describe('config: options babel', () => {
test('should replace and deprecate @nuxtjs/babel-preset-app', () => {
const { build: { babel } } = getNuxtConfig({
build: { babel: { presets: ['@nuxtjs/babel-preset-app'] } }
})
expect(consola.warn).toHaveBeenCalledWith('@nuxtjs/babel-preset-app has been deprecated, please use @nuxt/babel-preset-app.')
expect(babel).toEqual({
configFile: false,
babelrc: false,
cacheDirectory: false,
presets: ['@nuxt/babel-preset-app']
})
})
test('should support options in babel presets', () => {
const { build: { babel } } = getNuxtConfig({
build: { babel: { presets: [['@nuxt/babel-preset-app', { test: true }]] } }
})
expect(babel).toEqual({
configFile: false,
babelrc: false,
cacheDirectory: false,
presets: [['@nuxt/babel-preset-app', { test: true }]]
})
})
})
describe('config: options deprecated', () => {
test('should deprecate render.gzip', () => {
getNuxtConfig({ render: { gzip: true } })
expect(consola.warn).toHaveBeenCalledWith('render.gzip is deprecated and will be removed in a future version! Please switch to render.compressor')
})
test('should deprecate build.vendor', () => {
getNuxtConfig({ build: { vendor: ['lodash'] } })
expect(consola.warn).toHaveBeenCalledWith('vendor has been deprecated due to webpack4 optimization')
})
test('should deprecate devModules', () => {
const config = getNuxtConfig({ devModules: ['foo'], buildModules: ['bar'] })
expect(consola.warn).toHaveBeenCalledWith('`devModules` has been renamed to `buildModules` and will be removed in Nuxt 3.')
expect(config.devModules).toBe(undefined)
expect(config.buildModules).toEqual(['bar', 'foo'])
})
test('should deprecate build.extractCSS.allChunks', () => {
getNuxtConfig({ build: { extractCSS: { allChunks: true } } })
expect(consola.warn).toHaveBeenCalledWith('build.extractCSS.allChunks has no effect from v2.0.0. Please use build.optimization.splitChunks settings instead.')
})
})
})
describe('config: serverMiddleware', () => {
test('should transform serverMiddleware hash', () => {
const serverMiddleware = {
'/resource': (req, res, next) => {
}
}
const config = getNuxtConfig({ serverMiddleware })
expect(config.serverMiddleware[0].path).toBe('/resource')
expect(config.serverMiddleware[0].handler).toBe(serverMiddleware['/resource'])
})
})
describe('config: router', () => {
test('should sanitize router.base', () => {
const config = getNuxtConfig({ router: { base: '/foo' } })
expect(config.router.base).toBe('/foo/')
})
})