1 | import { chalk } from '@tarojs/helper'
|
2 | import * as path from 'path'
|
3 |
|
4 | import { run } from './utils'
|
5 |
|
6 | jest.mock('../doctor', () => {
|
7 | return {
|
8 | __esModule: true,
|
9 | default: {
|
10 | validators: [() => ({
|
11 | desc: 'configValidator',
|
12 | lines: [{
|
13 | desc: 'A',
|
14 | valid: false
|
15 | }]
|
16 | }), () => ({
|
17 | desc: 'packageValidator',
|
18 | lines: [{
|
19 | desc: 'B',
|
20 | valid: true
|
21 | }, {
|
22 | desc: 'C',
|
23 | valid: true,
|
24 | solution: 'c'
|
25 | }]
|
26 | }), () => ({
|
27 | desc: 'recommandValidator',
|
28 | lines: []
|
29 | }), () => ({
|
30 | desc: 'eslintValidator',
|
31 | raw: 'eslint msg'
|
32 | })]
|
33 | }
|
34 | }
|
35 | })
|
36 |
|
37 | jest.mock('ora', () => {
|
38 | const ora = jest.fn()
|
39 | ora.mockReturnValue({
|
40 | start () {
|
41 | return {
|
42 | succeed () {}
|
43 | }
|
44 | }
|
45 | })
|
46 | return ora
|
47 | })
|
48 |
|
49 | const runDoctor = run('doctor', ['commands/doctor'])
|
50 |
|
51 | describe('doctor', () => {
|
52 | it('should exit because there isn\'t a Taro project', async () => {
|
53 | const exitSpy = jest.spyOn(process, 'exit') as jest.SpyInstance<void, any>
|
54 | const logSpy = jest.spyOn(console, 'log')
|
55 |
|
56 | exitSpy.mockImplementation(() => {
|
57 | throw new Error()
|
58 | })
|
59 | logSpy.mockImplementation(() => {})
|
60 |
|
61 | try {
|
62 | await runDoctor('', { options: { disableGlobalConfig: true } })
|
63 | } catch (error) {}
|
64 |
|
65 | expect(exitSpy).toBeCalledWith(1)
|
66 | expect(logSpy).toBeCalledWith(chalk.red('找不到项目配置文件config/index,请确定当前目录是 Taro 项目根目录!'))
|
67 |
|
68 | exitSpy.mockRestore()
|
69 | logSpy.mockRestore()
|
70 | })
|
71 |
|
72 | it('should log reports', async () => {
|
73 | const NOTE_ALL_RIGHT = chalk.green('[✓] ')
|
74 | const NOTE_VALID = chalk.yellow('[!] ')
|
75 | const NOTE_INVALID = chalk.red('[✗] ')
|
76 |
|
77 | const titleChalk = chalk.hex('#aaa')
|
78 | const lineChalk = chalk.hex('#fff')
|
79 | const solutionChalk = chalk.hex('#999')
|
80 |
|
81 | const logSpy = jest.spyOn(console, 'log')
|
82 | logSpy.mockImplementation(() => {})
|
83 |
|
84 | await runDoctor(path.join(__dirname, 'fixtures/default'))
|
85 |
|
86 | expect(logSpy).nthCalledWith(1, '\n' + titleChalk('configValidator'))
|
87 | expect(logSpy).nthCalledWith(2, ' ' + NOTE_INVALID + lineChalk('A'))
|
88 |
|
89 | expect(logSpy).nthCalledWith(3, '\n' + titleChalk('packageValidator'))
|
90 | expect(logSpy).nthCalledWith(4, ' ' + NOTE_VALID + lineChalk('B'))
|
91 | expect(logSpy).nthCalledWith(5, ' ' + NOTE_VALID + lineChalk('C'))
|
92 | expect(logSpy).nthCalledWith(6, ' ' + solutionChalk('c'))
|
93 |
|
94 | expect(logSpy).nthCalledWith(7, '\n' + titleChalk('recommandValidator'))
|
95 | expect(logSpy).nthCalledWith(8, ` ${NOTE_ALL_RIGHT}没有发现问题`)
|
96 |
|
97 | expect(logSpy).nthCalledWith(9, '\n' + titleChalk('eslintValidator'))
|
98 | expect(logSpy).nthCalledWith(10, 'eslint msg')
|
99 |
|
100 | logSpy.mockRestore()
|
101 | })
|
102 | })
|