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