UNPKG

17.5 kBPlain TextView Raw
1import validator from '../doctor/configValidator'
2
3const baseConfig = {
4 projectName: 'test',
5 sourceRoot: 'src',
6 outputRoot: 'dist',
7 framework: 'react'
8}
9
10function getConfig (config) {
11 return {
12 projectConfig: {
13 ...baseConfig,
14 ...config
15 },
16 configPath: ''
17 }
18}
19
20describe('config validator of doctor', () => {
21 it('should config include\'s all the required values', async () => {
22 let { lines } = await validator({
23 projectConfig: {},
24 configPath: ''
25 })
26
27 expect(lines.length).toEqual(4)
28 let msgs = lines.map(line => line.desc)
29 expect(msgs.includes('projectName 必须填写')).toBeTruthy()
30 expect(msgs.includes('sourceRoot 必须填写')).toBeTruthy()
31 expect(msgs.includes('outputRoot 必须填写')).toBeTruthy()
32 expect(msgs.includes('framework 必须填写')).toBeTruthy()
33
34 const res = await validator({
35 projectConfig: {
36 projectName: '',
37 sourceRoot: '',
38 outputRoot: '',
39 framework: ''
40 },
41 configPath: ''
42 })
43 lines = res.lines
44
45 expect(lines.length).toEqual(4)
46 msgs = lines.map(line => line.desc)
47 expect(msgs.includes('projectName "projectName" is not allowed to be empty')).toBeTruthy()
48 expect(msgs.includes('sourceRoot "sourceRoot" is not allowed to be empty')).toBeTruthy()
49 expect(msgs.includes('outputRoot "outputRoot" is not allowed to be empty')).toBeTruthy()
50 expect(msgs.includes('framework "framework" must be one of [nerv, react, preact, vue, vue3]')).toBeTruthy()
51 })
52
53 it('date', async () => {
54 let { lines } = await validator(getConfig({
55 date: '2020-5-26'
56 }))
57
58 expect(lines.length).toEqual(0)
59
60 const res = await validator(getConfig({
61 date: 'abc'
62 }))
63 lines = res.lines
64
65 expect(lines.length).toEqual(1)
66 expect(lines[0].desc).toEqual('date 应该为一个日期')
67 })
68
69 it('framework', async () => {
70 let res = await validator(getConfig({
71 framework: 'react'
72 }))
73 expect(res.lines.length).toEqual(0)
74
75 res = await validator(getConfig({
76 framework: 'vue'
77 }))
78 expect(res.lines.length).toEqual(0)
79
80 res = await validator(getConfig({
81 framework: 'nerv'
82 }))
83 expect(res.lines.length).toEqual(0)
84
85 res = await validator(getConfig({
86 framework: 'vue3'
87 }))
88 expect(res.lines.length).toEqual(0)
89
90 res = await validator(getConfig({
91 framework: 'other'
92 }))
93 expect(res.lines.length).toEqual(1)
94 expect(res.lines[0].desc).toEqual('framework "framework" must be one of [nerv, react, preact, vue, vue3]')
95 })
96
97 it('designWidth', async () => {
98 let res = await validator(getConfig({
99 designWidth: '750'
100 }))
101 expect(res.lines.length).toEqual(0)
102
103 res = await validator(getConfig({
104 designWidth: 'a'
105 }))
106 expect(res.lines.length).toEqual(1)
107
108 res = await validator(getConfig({
109 designWidth: 700.5
110 }))
111 expect(res.lines.length).toEqual(1)
112
113 res = await validator(getConfig({
114 designWidth: -640
115 }))
116 expect(res.lines.length).toEqual(1)
117 })
118
119 it('deviceRatio', async () => {
120 let res = await validator(getConfig({
121 deviceRatio: {
122 640: 2.34 / 2,
123 750: 1,
124 828: 1.81 / 2
125 }
126 }))
127 expect(res.lines.length).toEqual(0)
128
129 res = await validator(getConfig({
130 deviceRatio: {
131 640: 2.34 / 2,
132 750: 'a',
133 828: 1.81 / 2
134 }
135 }))
136 expect(res.lines.length).toEqual(1)
137 })
138
139 it('plugins', async () => {
140 let res = await validator(getConfig({
141 plugins: [
142 '@tarojs/plugin-mock',
143 ['@tarojs/plugin-mock', {
144 mocks: {
145 '/api/user/1': {
146 name: 'judy',
147 desc: 'Mental guy'
148 }
149 }
150 }],
151 ['@tarojs/plugin-mock'],
152 '/absulute/path/plugin/filename'
153 ]
154 }))
155 expect(res.lines.length).toEqual(0)
156
157 res = await validator(getConfig({
158 plugins: [
159 1,
160 {},
161 ['x', 1],
162 [{}, 'x'],
163 ['x', {}, 1],
164 ['']
165 ]
166 }))
167 expect(res.lines.length).toEqual(6)
168 })
169
170 it('presets', async () => {
171 let res = await validator(getConfig({
172 presets: [
173 '@tarojs/plugin-mock',
174 ['@tarojs/plugin-mock', {
175 mocks: {
176 '/api/user/1': {
177 name: 'judy',
178 desc: 'Mental guy'
179 }
180 }
181 }],
182 ['@tarojs/plugin-mock'],
183 '/absulute/path/plugin/filename'
184 ]
185 }))
186 expect(res.lines.length).toEqual(0)
187
188 res = await validator(getConfig({
189 presets: [
190 1,
191 {},
192 ['x', 1],
193 [{}, 'x'],
194 ['x', {}, 1],
195 ['']
196 ]
197 }))
198 expect(res.lines.length).toEqual(6)
199 })
200
201 it('terser', async () => {
202 let res = await validator(getConfig({
203 terser: {
204 enable: true,
205 config: {
206 }
207 }
208 }))
209 expect(res.lines.length).toEqual(0)
210
211 res = await validator(getConfig({
212 terser: {
213 enables: true,
214 enable: 1,
215 config: []
216 }
217 }))
218 expect(res.lines.length).toEqual(3)
219 })
220
221 it('csso', async () => {
222 let res = await validator(getConfig({
223 csso: {
224 enable: true,
225 config: {
226 }
227 }
228 }))
229 expect(res.lines.length).toEqual(0)
230
231 res = await validator(getConfig({
232 csso: {
233 enables: true,
234 enable: 1,
235 config: []
236 }
237 }))
238 expect(res.lines.length).toEqual(3)
239 })
240
241 it('uglify', async () => {
242 let res = await validator(getConfig({
243 uglify: {
244 enable: true,
245 config: {
246 }
247 }
248 }))
249 expect(res.lines.length).toEqual(0)
250
251 res = await validator(getConfig({
252 uglify: {
253 enables: true,
254 enable: 1,
255 config: []
256 }
257 }))
258 expect(res.lines.length).toEqual(3)
259 })
260
261 it('sass', async () => {
262 let res = await validator(getConfig({
263 sass: {
264 resource: '/src/styles/variable.scss'
265 }
266 }))
267 expect(res.lines.length).toEqual(0)
268
269 res = await validator(getConfig({
270 sass: {
271 resource: [
272 './src/styles/variable.scss',
273 './src/styles/mixins.scss'
274 ],
275 projectDirectory: '/root',
276 data: '$nav-height: 48px;'
277 }
278 }))
279 expect(res.lines.length).toEqual(0)
280
281 res = await validator(getConfig({
282 sass: {
283 resource: {},
284 projectDirectory: 1,
285 data: 1
286 }
287 }))
288 expect(res.lines.length).toEqual(3)
289 })
290
291 it('env', async () => {
292 const res = await validator(getConfig({
293 env: {
294 NODE_ENV: '"development"'
295 }
296 }))
297 expect(res.lines.length).toEqual(0)
298 })
299
300 it('defineConstants', async () => {
301 const res = await validator(getConfig({
302 defineConstants: {
303 A: '"a"'
304 }
305 }))
306 expect(res.lines.length).toEqual(0)
307 })
308
309 it('alias', async () => {
310 let res = await validator(getConfig({
311 alias: {
312 '@/components': 'src/components'
313 }
314 }))
315 expect(res.lines.length).toEqual(0)
316
317 res = await validator(getConfig({
318 alias: {
319 '@/components': ['src/components'],
320 '@/utils': 1,
321 '@/project': {}
322 }
323 }))
324 expect(res.lines.length).toEqual(3)
325 })
326
327 it('copy', async () => {
328 let res = await validator(getConfig({
329 copy: {
330 patterns: [
331 { from: 'src/asset/tt/', to: 'dist/asset/tt/', ignore: ['*.js'] },
332 { from: 'src/asset/tt/sd.jpg', to: 'dist/asset/tt/sd.jpg' }
333 ]
334 }
335 }))
336 expect(res.lines.length).toEqual(0)
337
338 res = await validator(getConfig({
339 copy: {
340 options: {
341 ignore: ['*.js', '*.css']
342 }
343 }
344 }))
345 expect(res.lines.length).toEqual(0)
346
347 res = await validator(getConfig({
348 copy: {
349 patterns: [
350 { ignore: ['*.js'] },
351 { from: 'src/asset/tt/sd.jpg' },
352 { to: 'dist/asset/tt/sd.jpg' }
353 ],
354 options: {
355 ignore: [1, true, {}]
356 }
357 }
358 }))
359 expect(res.lines.length).toEqual(7)
360 })
361
362 it('mini.compile', async () => {
363 let res = await validator(getConfig({
364 mini: {
365 compile: {
366 exclude: [
367 'src/pages/index/vod-wx-sdk-v2.js',
368 modulePath => modulePath.indexOf('vod-wx-sdk-v2') >= 0
369 ],
370 include: [
371 'src/pages/index/vod-wx-sdk-v2.js',
372 modulePath => modulePath.indexOf('vod-wx-sdk-v2') >= 0
373 ]
374 }
375 }
376 }))
377 expect(res.lines.length).toEqual(0)
378
379 res = await validator(getConfig({
380 mini: {
381 compile: {
382 exclude: [null, []],
383 include: [1, {}]
384 }
385 }
386 }))
387 expect(res.lines.length).toEqual(4)
388 })
389
390 it('mini.webpackChain', async () => {
391 let res = await validator(getConfig({
392 mini: {
393 webpackChain (chain, webpack) {
394 console.log(chain, webpack)
395 }
396 }
397 }))
398 expect(res.lines.length).toEqual(0)
399
400 res = await validator(getConfig({
401 mini: {
402 webpackChain: 'some'
403 }
404 }))
405 expect(res.lines.length).toEqual(1)
406 })
407
408 it('mini.commonChunks', async () => {
409 let res = await validator(getConfig({
410 mini: {
411 commonChunks: ['runtime', 'vendors', 'taro', 'common']
412 }
413 }))
414 expect(res.lines.length).toEqual(0)
415
416 res = await validator(getConfig({
417 mini: {
418 commonChunks (commonChunks) {
419 commonChunks.push('yourCustomCommonChunkName')
420 return commonChunks
421 }
422 }
423 }))
424 expect(res.lines.length).toEqual(0)
425
426 res = await validator(getConfig({
427 mini: {
428 commonChunks: 1
429 }
430 }))
431 expect(res.lines.length).toEqual(1)
432 })
433
434 it('mini.addChunkPages', async () => {
435 let res = await validator(getConfig({
436 mini: {
437 addChunkPages (pages, pagesNames) {
438 console.log(pages, pagesNames)
439 }
440 }
441 }))
442 expect(res.lines.length).toEqual(0)
443
444 res = await validator(getConfig({
445 mini: {
446 addChunkPages: 1
447 }
448 }))
449 expect(res.lines.length).toEqual(1)
450 })
451
452 it('mini.postcss', async () => {
453 let res = await validator(getConfig({
454 mini: {
455 postcss: {
456 autoprefixer: {
457 enable: true,
458 config: {}
459 }
460 }
461 }
462 }))
463 expect(res.lines.length).toEqual(0)
464
465 res = await validator(getConfig({
466 mini: {
467 postcss: {
468 autoprefixer: {
469 enable: 1,
470 config: 'a'
471 },
472 pxtransform: true
473 }
474 }
475 }))
476 expect(res.lines.length).toEqual(3)
477 })
478
479 it('mini.output', async () => {
480 let res = await validator(getConfig({
481 mini: {
482 output: {}
483 }
484 }))
485 expect(res.lines.length).toEqual(0)
486
487 res = await validator(getConfig({
488 mini: {
489 output: 1
490 }
491 }))
492 expect(res.lines.length).toEqual(1)
493 })
494
495 it('mini\'s third party options', async () => {
496 const res = await validator(getConfig({
497 mini: {
498 cssLoaderOption: {},
499 styleLoaderOption: {},
500 sassLoaderOption: {},
501 lessLoaderOption: {},
502 stylusLoaderOption: {},
503 mediaUrlLoaderOption: {},
504 fontUrlLoaderOption: {},
505 imageUrlLoaderOption: {},
506 miniCssExtractPluginOption: {}
507 }
508 }))
509 expect(res.lines.length).toEqual(0)
510 })
511
512 it('h5.devServer', async () => {
513 let res = await validator(getConfig({
514 h5: {
515 devServer: {
516 port: 10086
517 }
518 }
519 }))
520 expect(res.lines.length).toEqual(0)
521
522 res = await validator(getConfig({
523 h5: {
524 devServer: 1
525 }
526 }))
527 expect(res.lines.length).toEqual(1)
528 })
529
530 it('h5.output', async () => {
531 let res = await validator(getConfig({
532 h5: {
533 output: {
534 filename: 'js/[name].[hash:8].js',
535 chunkFilename: 'js/[name].[chunkhash:8].js'
536 }
537 }
538 }))
539 expect(res.lines.length).toEqual(0)
540
541 res = await validator(getConfig({
542 h5: {
543 output: 1
544 }
545 }))
546 expect(res.lines.length).toEqual(1)
547 })
548
549 it('h5.publicPath', async () => {
550 let res = await validator(getConfig({
551 h5: {
552 publicPath: '/'
553 }
554 }))
555 expect(res.lines.length).toEqual(0)
556
557 res = await validator(getConfig({
558 h5: {
559 publicPath: 1
560 }
561 }))
562 expect(res.lines.length).toEqual(1)
563 })
564
565 it('h5.staticDirectory', async () => {
566 let res = await validator(getConfig({
567 h5: {
568 staticDirectory: '/'
569 }
570 }))
571 expect(res.lines.length).toEqual(0)
572
573 res = await validator(getConfig({
574 h5: {
575 staticDirectory: 1
576 }
577 }))
578 expect(res.lines.length).toEqual(1)
579 })
580
581 it('h5.chunkDirectory', async () => {
582 let res = await validator(getConfig({
583 h5: {
584 chunkDirectory: '/'
585 }
586 }))
587 expect(res.lines.length).toEqual(0)
588
589 res = await validator(getConfig({
590 h5: {
591 chunkDirectory: 1
592 }
593 }))
594 expect(res.lines.length).toEqual(1)
595 })
596
597 it('h5.webpackChain', async () => {
598 let res = await validator(getConfig({
599 h5: {
600 webpackChain (chain, webpack) {
601 console.log(chain, webpack)
602 }
603 }
604 }))
605 expect(res.lines.length).toEqual(0)
606
607 res = await validator(getConfig({
608 h5: {
609 webpackChain: 'some'
610 }
611 }))
612 expect(res.lines.length).toEqual(1)
613 })
614
615 it('h5.webpack[DEPRECATED]', async () => {
616 const res = await validator(getConfig({
617 h5: {
618 webpack: 1
619 }
620 }))
621 expect(res.lines.length).toEqual(1)
622 expect(res.lines[0].desc).toEqual('h5.webpack "h5.webpack" is not allowed')
623 })
624
625 it('h5.router', async () => {
626 let res = await validator(getConfig({
627 h5: {
628 router: {
629 mode: 'hash'
630 }
631 }
632 }))
633 expect(res.lines.length).toEqual(0)
634
635 res = await validator(getConfig({
636 h5: {
637 router: 1
638 }
639 }))
640 expect(res.lines.length).toEqual(1)
641 })
642
643 it('h5.entry', async () => {
644 let res = await validator(getConfig({
645 h5: {
646 entry: './path/to/my/entry/file.js'
647 }
648 }))
649 expect(res.lines.length).toEqual(0)
650
651 res = await validator(getConfig({
652 h5: {
653 entry: ['./path/to/my/entry/file.js', './path/other/file.js']
654 }
655 }))
656 expect(res.lines.length).toEqual(0)
657
658 res = await validator(getConfig({
659 h5: {
660 entry: {
661 main: './path/to/my/entry/file.js'
662 }
663 }
664 }))
665 expect(res.lines.length).toEqual(0)
666
667 res = await validator(getConfig({
668 h5: {
669 entry: {
670 main: ['./path/to/my/entry/file.js', './path/other/file.js']
671 }
672 }
673 }))
674 expect(res.lines.length).toEqual(0)
675
676 res = await validator(getConfig({
677 h5: {
678 entry: () => './demo'
679 }
680 }))
681 expect(res.lines.length).toEqual(0)
682 })
683
684 it('h5.enableSourceMap', async () => {
685 let res = await validator(getConfig({
686 h5: {
687 enableSourceMap: true
688 }
689 }))
690 expect(res.lines.length).toEqual(0)
691
692 res = await validator(getConfig({
693 h5: {
694 enableSourceMap: 1
695 }
696 }))
697 expect(res.lines.length).toEqual(1)
698 })
699
700 it('h5.enableExtract', async () => {
701 let res = await validator(getConfig({
702 h5: {
703 enableExtract: true
704 }
705 }))
706 expect(res.lines.length).toEqual(0)
707
708 res = await validator(getConfig({
709 h5: {
710 enableExtract: 1
711 }
712 }))
713 expect(res.lines.length).toEqual(1)
714 })
715
716 it('h5.esnextModules', async () => {
717 let res = await validator(getConfig({
718 h5: {
719 esnextModules: ['taro-ui']
720 }
721 }))
722 expect(res.lines.length).toEqual(0)
723
724 res = await validator(getConfig({
725 h5: {
726 esnextModules: [1, true, {}]
727 }
728 }))
729 expect(res.lines.length).toEqual(3)
730
731 res = await validator(getConfig({ h5: 1 }))
732 expect(res.lines.length).toEqual(1)
733 })
734
735 it('h5.postcss', async () => {
736 let res = await validator(getConfig({
737 h5: {
738 postcss: {
739 autoprefixer: {
740 enable: true,
741 config: {}
742 }
743 }
744 }
745 }))
746 expect(res.lines.length).toEqual(0)
747
748 res = await validator(getConfig({
749 h5: {
750 postcss: {
751 autoprefixer: {
752 enable: 1,
753 config: 'a'
754 },
755 pxtransform: true
756 }
757 }
758 }))
759 expect(res.lines.length).toEqual(3)
760 })
761
762 it('h5\'s third party options', async () => {
763 const res = await validator(getConfig({
764 h5: {
765 cssLoaderOption: {},
766 styleLoaderOption: {},
767 sassLoaderOption: {},
768 lessLoaderOption: {},
769 stylusLoaderOption: {},
770 mediaUrlLoaderOption: {},
771 fontUrlLoaderOption: {},
772 imageUrlLoaderOption: {},
773 miniCssExtractPluginOption: {}
774 }
775 }))
776 expect(res.lines.length).toEqual(0)
777 })
778
779 it('unknow', async () => {
780 const res = await validator(getConfig({
781 unknow: {}
782 }))
783 expect(res.lines.length).toEqual(0)
784 })
785
786 it('mini.unknow', async () => {
787 const res = await validator(getConfig({
788 mini: {
789 unknow: {}
790 }
791 }))
792 expect(res.lines.length).toEqual(0)
793 })
794
795 it('h5.unknow', async () => {
796 const res = await validator(getConfig({
797 h5: {
798 unknow: {}
799 }
800 }))
801 expect(res.lines.length).toEqual(0)
802 })
803})