forked from vuejs/vue-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathService.spec.js
387 lines (342 loc) · 9.54 KB
/
Service.spec.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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
jest.mock('fs')
jest.mock('/vue.config.js', () => ({ lintOnSave: false }), { virtual: true })
jest.mock('/vue.config.cjs', () => ({ lintOnSave: true }), { virtual: true })
jest.mock('vue-cli-plugin-foo', () => () => {}, { virtual: true })
const fs = require('fs')
const path = require('path')
const Service = require('../lib/Service')
const mockPkg = json => {
fs.writeFileSync('/package.json', JSON.stringify(json, null, 2))
}
const createMockService = (plugins = [], init = true, mode) => {
const service = new Service('/', {
plugins,
useBuiltIn: false
})
if (init) {
service.init(mode)
}
return service
}
beforeEach(() => {
mockPkg({})
delete process.env.NODE_ENV
delete process.env.BABEL_ENV
delete process.env.FOO
delete process.env.BAR
delete process.env.BAZ
})
test('env loading', () => {
process.env.FOO = 0
fs.writeFileSync('/.env.local', `FOO=1\nBAR=2`)
fs.writeFileSync('/.env', `BAR=3\nBAZ=4`)
createMockService()
expect(process.env.FOO).toBe('0')
expect(process.env.BAR).toBe('2')
expect(process.env.BAZ).toBe('4')
fs.unlinkSync('/.env.local')
fs.unlinkSync('/.env')
})
test('env loading for custom mode', () => {
process.env.VUE_CLI_TEST_TESTING_ENV = true
fs.writeFileSync('/.env', 'FOO=1')
fs.writeFileSync('/.env.staging', 'FOO=2\nNODE_ENV=production')
createMockService([], true, 'staging')
expect(process.env.FOO).toBe('2')
expect(process.env.NODE_ENV).toBe('production')
process.env.VUE_CLI_TEST_TESTING_ENV = false
fs.unlinkSync('/.env')
fs.unlinkSync('/.env.staging')
})
test('loading plugins from package.json', () => {
mockPkg({
devDependencies: {
bar: '^1.0.0',
'@vue/cli-plugin-babel': '^4.2.0',
'vue-cli-plugin-foo': '^1.0.0'
}
})
const service = new Service('/') // this one needs to read from package.json
expect(service.plugins.some(({ id }) => id === '@vue/cli-plugin-babel')).toBe(true)
expect(service.plugins.some(({ id }) => id === 'vue-cli-plugin-foo')).toBe(true)
expect(service.plugins.some(({ id }) => id === 'bar')).toBe(false)
})
test('load project options from package.json', () => {
mockPkg({
vue: {
lintOnSave: 'default'
}
})
const service = createMockService()
expect(service.projectOptions.lintOnSave).toBe('default')
})
test('handle option publicPath and outputDir correctly', () => {
mockPkg({
vue: {
publicPath: 'https://foo.com/bar',
outputDir: '/public/'
}
})
const service = createMockService()
expect(service.projectOptions.publicPath).toBe('https://foo.com/bar/')
expect(service.projectOptions.outputDir).toBe('/public')
})
test('normalize publicPath when relative', () => {
mockPkg({
vue: {
publicPath: './foo/bar'
}
})
const service = createMockService()
expect(service.projectOptions.publicPath).toBe('foo/bar/')
})
test('allow custom protocol in publicPath', () => {
mockPkg({
vue: {
publicPath: 'customprotocol://foo/bar'
}
})
const service = createMockService()
expect(service.projectOptions.publicPath).toBe('customprotocol://foo/bar/')
})
test('keep publicPath when empty', () => {
mockPkg({
vue: {
publicPath: ''
}
})
const service = createMockService()
expect(service.projectOptions.publicPath).toBe('')
})
test('load project options from vue.config.js', () => {
process.env.VUE_CLI_SERVICE_CONFIG_PATH = `/vue.config.js`
fs.writeFileSync('/vue.config.js', `module.exports = { lintOnSave: false }`)
mockPkg({
vue: {
lintOnSave: 'default'
}
})
const service = createMockService()
fs.unlinkSync('/vue.config.js')
delete process.env.VUE_CLI_SERVICE_CONFIG_PATH
// vue.config.js has higher priority
expect(service.projectOptions.lintOnSave).toBe(false)
})
test('load project options from vue.config.cjs', () => {
fs.writeFileSync('/vue.config.cjs', '')
const service = createMockService()
fs.unlinkSync('/vue.config.cjs')
expect(service.projectOptions.lintOnSave).toBe(true)
})
test('load project options from vue.config.js', () => {
process.env.VUE_CLI_SERVICE_CONFIG_PATH = `/vue.config.js`
fs.writeFileSync('/vue.config.js', '') // only to ensure fs.existsSync returns true
jest.mock('/vue.config.js', () => function () { return { lintOnSave: false } }, { virtual: true })
mockPkg({
vue: {
lintOnSave: 'default'
}
})
const service = createMockService()
fs.unlinkSync('/vue.config.js')
delete process.env.VUE_CLI_SERVICE_CONFIG_PATH
// vue.config.js has higher priority
expect(service.projectOptions.lintOnSave).toBe(false)
})
test('api: assertVersion', () => {
const plugin = {
id: 'test-assertVersion',
apply: api => {
expect(() => api.assertVersion(4)).not.toThrow()
expect(() => api.assertVersion('^4.0.0-0')).not.toThrow()
// expect(() => api.assertVersion('>= 4')).not.toThrow()
expect(() => api.assertVersion(4.1)).toThrow('Expected string or integer value')
expect(() => api.assertVersion('^100')).toThrow('Require @vue/cli-service "^100"')
}
}
createMockService([plugin], true /* init */)
})
test('api: registerCommand', () => {
let args
const service = createMockService([{
id: 'test',
apply: api => {
api.registerCommand('foo', _args => {
args = _args
})
}
}])
service.run('foo', { n: 1 })
expect(args).toEqual({ _: [], n: 1 })
})
test('api: --skip-plugins', () => {
let untouched = true
const service = createMockService([{
id: 'test-command',
apply: api => {
api.registerCommand('foo', _args => {
})
}
},
{
id: 'vue-cli-plugin-test-plugin',
apply: api => {
untouched = false
}
}], false)
service.run('foo', { 'skip-plugins': 'test-plugin' })
expect(untouched).toEqual(true)
})
test('api: defaultModes', () => {
fs.writeFileSync('/.env.foo', `FOO=5\nBAR=6`)
fs.writeFileSync('/.env.foo.local', `FOO=7\nBAZ=8`)
const plugin1 = {
id: 'test-defaultModes',
apply: api => {
expect(process.env.FOO).toBe('7')
expect(process.env.BAR).toBe('6')
expect(process.env.BAZ).toBe('8')
// for NODE_ENV & BABEL_ENV
// any mode that is not test or production defaults to development
expect(process.env.NODE_ENV).toBe('development')
expect(process.env.BABEL_ENV).toBe('development')
api.registerCommand('foo', () => {})
}
}
plugin1.apply.defaultModes = {
foo: 'foo'
}
createMockService([plugin1], false /* init */).run('foo')
delete process.env.NODE_ENV
delete process.env.BABEL_ENV
const plugin2 = {
id: 'test-defaultModes',
apply: api => {
expect(process.env.NODE_ENV).toBe('test')
expect(process.env.BABEL_ENV).toBe('test')
api.registerCommand('test', () => {})
}
}
plugin2.apply.defaultModes = {
test: 'test'
}
createMockService([plugin2], false /* init */).run('test')
})
test('api: chainWebpack', () => {
const service = createMockService([{
id: 'test',
apply: api => {
api.chainWebpack(config => {
config.output.path('test-dist')
})
}
}])
const config = service.resolveWebpackConfig()
expect(config.output.path).toBe('test-dist')
})
test('api: configureWebpack', () => {
const service = createMockService([{
id: 'test',
apply: api => {
api.configureWebpack(config => {
config.output = {
path: 'test-dist-2'
}
})
}
}])
const config = service.resolveWebpackConfig()
expect(config.output.path).toBe('test-dist-2')
})
test('api: configureWebpack returning object', () => {
const service = createMockService([{
id: 'test',
apply: api => {
api.configureWebpack(config => {
return {
output: {
path: 'test-dist-3'
}
}
})
}
}])
const config = service.resolveWebpackConfig()
expect(config.output.path).toBe('test-dist-3')
})
test('api: configureWebpack preserve ruleNames', () => {
const service = createMockService([
{
id: 'babel',
apply: require('@vue/cli-plugin-babel')
},
{
id: 'test',
apply: api => {
api.configureWebpack({
module: {
rules: []
}
})
}
}
])
const config = service.resolveWebpackConfig()
expect(config.module.rules[0].__ruleNames).toEqual(['js'])
})
test('internal: should correctly set VUE_CLI_ENTRY_FILES', () => {
const service = createMockService([{
id: 'test',
apply: api => {
api.configureWebpack(config => {
config.entry = {
page1: './src/page1.js',
page2: './src/page2.js'
}
})
}
}])
service.resolveWebpackConfig()
expect(process.env.VUE_CLI_ENTRY_FILES).toEqual(
JSON.stringify([
path.resolve('/', './src/page1.js'),
path.resolve('/', './src/page2.js')
])
)
})
test('api: configureDevServer', () => {
const cb = () => {}
const service = createMockService([{
id: 'test',
apply: api => {
api.configureDevServer(cb)
}
}])
expect(service.devServerConfigFns).toContain(cb)
})
test('api: resolve', () => {
createMockService([{
id: 'test',
apply: api => {
expect(api.resolve('foo.js')).toBe(path.resolve('/', 'foo.js'))
}
}])
})
test('api: hasPlugin', () => {
createMockService([
{
id: 'vue-cli-plugin-foo',
apply: api => {
expect(api.hasPlugin('bar')).toBe(true)
expect(api.hasPlugin('@vue/cli-plugin-bar')).toBe(true)
}
},
{
id: '@vue/cli-plugin-bar',
apply: api => {
expect(api.hasPlugin('foo')).toBe(true)
expect(api.hasPlugin('vue-cli-plugin-foo')).toBe(true)
}
}
])
})