1 | import * as path from 'path'
|
2 | import chalk from 'chalk'
|
3 | import { fs } from '@tarojs/helper'
|
4 | import { run } from './utils'
|
5 |
|
6 | jest.mock('cli-highlight', () => {
|
7 | return {
|
8 | __esModule: true,
|
9 | default (str) {
|
10 | return str
|
11 | }
|
12 | }
|
13 | })
|
14 |
|
15 | jest.mock('@tarojs/helper', () => {
|
16 | const helper = jest.requireActual('@tarojs/helper')
|
17 | const fs = jest.requireActual('fs-extra')
|
18 | return {
|
19 | __esModule: true,
|
20 | ...helper,
|
21 | fs: {
|
22 | ...fs,
|
23 | writeFileSync: jest.fn()
|
24 | }
|
25 | }
|
26 | })
|
27 |
|
28 | const runInspect = run('inspect')
|
29 |
|
30 | describe('inspect', () => {
|
31 | it('should exit because there isn\'t a Taro project', async () => {
|
32 | const exitSpy = jest.spyOn(process, 'exit') as jest.SpyInstance<void, any>
|
33 | const logSpy = jest.spyOn(console, 'log')
|
34 |
|
35 | exitSpy.mockImplementation(() => {
|
36 | throw new Error()
|
37 | })
|
38 | logSpy.mockImplementation(() => {})
|
39 |
|
40 | try {
|
41 | await runInspect('')
|
42 | } catch (error) {}
|
43 |
|
44 | expect(exitSpy).toBeCalledWith(1)
|
45 | expect(logSpy).toBeCalledWith(chalk.red('找不到项目配置文件config/index,请确定当前目录是 Taro 项目根目录!'))
|
46 |
|
47 | exitSpy.mockRestore()
|
48 | logSpy.mockRestore()
|
49 | })
|
50 |
|
51 | it('should exit when user haven\'t pass correct type', 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 runInspect(path.resolve(__dirname, 'fixtures/default'))
|
62 | } catch (error) {}
|
63 |
|
64 | expect(exitSpy).toBeCalledWith(0)
|
65 | expect(logSpy).toBeCalledWith(chalk.red('请传入正确的编译类型!'))
|
66 |
|
67 | exitSpy.mockRestore()
|
68 | logSpy.mockRestore()
|
69 | })
|
70 |
|
71 | it('should log config', async () => {
|
72 | const exitSpy = jest.spyOn(process, 'exit') as jest.SpyInstance<void, any>
|
73 | const logSpy = jest.spyOn(console, 'log')
|
74 |
|
75 | exitSpy.mockImplementation(() => {
|
76 | throw new Error()
|
77 | })
|
78 | logSpy.mockImplementation(() => {})
|
79 |
|
80 | try {
|
81 | const appPath = path.resolve(__dirname, 'fixtures/default')
|
82 | await runInspect(appPath, {
|
83 | options: {
|
84 | type: 'weapp'
|
85 | }
|
86 | })
|
87 | } catch (error) {}
|
88 |
|
89 | expect(exitSpy).toBeCalledWith(0)
|
90 | expect(logSpy).toBeCalledTimes(2)
|
91 |
|
92 | exitSpy.mockRestore()
|
93 | logSpy.mockRestore()
|
94 | })
|
95 |
|
96 | it('should log specific config', async () => {
|
97 | const exitSpy = jest.spyOn(process, 'exit') as jest.SpyInstance<void, any>
|
98 | const logSpy = jest.spyOn(console, 'log')
|
99 | const errorSpy = jest.spyOn(console, 'error')
|
100 |
|
101 | exitSpy.mockImplementation(() => {
|
102 | throw new Error()
|
103 | })
|
104 | logSpy.mockImplementation(() => {})
|
105 | errorSpy.mockImplementation(() => {})
|
106 |
|
107 | try {
|
108 | const appPath = path.resolve(__dirname, 'fixtures/default')
|
109 | await runInspect(appPath, {
|
110 | options: {
|
111 | type: 'h5'
|
112 | },
|
113 | args: ['resolve.mainFields.0']
|
114 | })
|
115 | } catch (error) {}
|
116 |
|
117 | expect(exitSpy).toBeCalledWith(0)
|
118 | expect(logSpy).toBeCalledTimes(1)
|
119 | expect(logSpy).toBeCalledWith('\'main:h5\'')
|
120 |
|
121 | exitSpy.mockRestore()
|
122 | logSpy.mockRestore()
|
123 | errorSpy.mockRestore()
|
124 | })
|
125 |
|
126 | it('should output config', async () => {
|
127 | const exitSpy = jest.spyOn(process, 'exit') as jest.SpyInstance<void, any>
|
128 | const writeFileSync = fs.writeFileSync as jest.Mock<any>
|
129 | const outputPath = 'project-config.js'
|
130 |
|
131 | exitSpy.mockImplementation(() => {
|
132 | throw new Error()
|
133 | })
|
134 |
|
135 | try {
|
136 | const appPath = path.resolve(__dirname, 'fixtures/default')
|
137 | await runInspect(appPath, {
|
138 | options: {
|
139 | type: 'alipay',
|
140 | output: outputPath
|
141 | },
|
142 | args: ['resolve.mainFields.0']
|
143 | })
|
144 | } catch (error) {}
|
145 |
|
146 | expect(exitSpy).toBeCalledWith(0)
|
147 | expect(writeFileSync).toBeCalledWith(outputPath, '\'browser\'')
|
148 |
|
149 | exitSpy.mockRestore()
|
150 | })
|
151 | })
|