UNPKG

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