1 | import { Kernel } from '@tarojs/service'
|
2 | import { getPkgVersion } from '../util'
|
3 | import CLI from '../cli'
|
4 |
|
5 | jest.mock('@tarojs/service')
|
6 | const MockedKernel = (Kernel as unknown) as (jest.Mock<Kernel>)
|
7 | const APP_PATH = '/a/b/c'
|
8 |
|
9 | function setProcessArgv (cmd: string) {
|
10 |
|
11 | process.argv = [null, ...cmd.split(' ')]
|
12 | }
|
13 |
|
14 | describe('inspect', () => {
|
15 | let cli: CLI
|
16 |
|
17 | beforeAll(() => {
|
18 | cli = new CLI(APP_PATH)
|
19 | })
|
20 |
|
21 | beforeEach(() => {
|
22 | MockedKernel.mockClear()
|
23 | process.argv = []
|
24 | delete process.env.NODE_ENV
|
25 | delete process.env.TARO_ENV
|
26 | })
|
27 |
|
28 | afterEach(() => {
|
29 | MockedKernel.mockClear()
|
30 | process.argv = []
|
31 | delete process.env.NODE_ENV
|
32 | delete process.env.TARO_ENV
|
33 | })
|
34 |
|
35 | describe('build', () => {
|
36 | const baseOpts = {
|
37 | _: [
|
38 | 'build'
|
39 | ],
|
40 | options: {
|
41 | platform: undefined,
|
42 | isWatch: false,
|
43 | env: undefined,
|
44 | blended: false
|
45 | },
|
46 | isHelp: false
|
47 | }
|
48 |
|
49 | it('should make configs', () => {
|
50 | const platform = 'weapp'
|
51 | setProcessArgv('taro build --type weapp --watch --port 8080')
|
52 | cli.run()
|
53 | const ins = MockedKernel.mock.instances[0]
|
54 |
|
55 | const opts = Object.assign({}, baseOpts)
|
56 | opts.options = Object.assign({}, baseOpts.options, {
|
57 | platform,
|
58 | isWatch: true,
|
59 | port: 8080,
|
60 | deviceType: undefined,
|
61 | resetCache: false,
|
62 | qr: false
|
63 | })
|
64 |
|
65 | expect(ins.run).toHaveBeenCalledWith({
|
66 | name: 'build',
|
67 | opts
|
68 | })
|
69 | })
|
70 |
|
71 | it('should not set node env again', () => {
|
72 | process.env.NODE_ENV = 'development'
|
73 | setProcessArgv('taro build --type weapp')
|
74 | cli.run()
|
75 | expect(process.env.NODE_ENV).toEqual('development')
|
76 | })
|
77 |
|
78 | it.skip('should make plugin config', () => {
|
79 | setProcessArgv('taro build --plugin')
|
80 | cli.run()
|
81 | const ins = MockedKernel.mock.instances[0]
|
82 | expect(ins.run).toHaveBeenCalledWith({
|
83 | name: 'build',
|
84 | opts: Object.assign({}, baseOpts, {
|
85 | platform: 'plugin',
|
86 | plugin: 'weapp'
|
87 | })
|
88 | })
|
89 | expect(process.env.NODE_ENV).toEqual('production')
|
90 | expect(process.env.TARO_ENV).toEqual('plugin')
|
91 | })
|
92 | })
|
93 |
|
94 | describe('init', () => {
|
95 | it('should make configs', () => {
|
96 | const projectName = 'temp'
|
97 | const templateSource = 'https://url'
|
98 | const template = 'mobx'
|
99 | const css = 'sass'
|
100 | setProcessArgv('taro init temp --typescript --template-source=https://url --clone --template mobx --css sass')
|
101 | cli.run()
|
102 | const ins = MockedKernel.mock.instances[0]
|
103 | expect(ins.run).toHaveBeenCalledWith({
|
104 | name: 'init',
|
105 | opts: {
|
106 | _: [
|
107 | 'init',
|
108 | 'temp'
|
109 | ],
|
110 | options: {
|
111 | appPath: APP_PATH,
|
112 | projectName,
|
113 | typescript: true,
|
114 | templateSource,
|
115 | description: undefined,
|
116 | clone: true,
|
117 | template,
|
118 | css
|
119 | },
|
120 | isHelp: false
|
121 | }
|
122 | })
|
123 | })
|
124 |
|
125 | it('should set project name', () => {
|
126 | const projectName = 'demo'
|
127 | setProcessArgv('taro init --name demo')
|
128 | cli.run()
|
129 | const ins = MockedKernel.mock.instances[0]
|
130 | expect(ins.run).toHaveBeenCalledWith({
|
131 | name: 'init',
|
132 | opts: {
|
133 | _: [
|
134 | 'init'
|
135 | ],
|
136 | options: {
|
137 | appPath: APP_PATH,
|
138 | projectName,
|
139 | typescript: undefined,
|
140 | templateSource: undefined,
|
141 | description: undefined,
|
142 | clone: false,
|
143 | template: undefined,
|
144 | css: undefined
|
145 | },
|
146 | isHelp: false
|
147 | }
|
148 | })
|
149 | })
|
150 | })
|
151 |
|
152 | describe('convert', () => {
|
153 | it('should make configs', () => {
|
154 | setProcessArgv('taro convert')
|
155 | cli.run()
|
156 | const ins = MockedKernel.mock.instances[0]
|
157 | expect(ins.run).toHaveBeenCalledWith({
|
158 | name: 'convert',
|
159 | opts: {
|
160 | _: ['convert'],
|
161 | options: {},
|
162 | isHelp: false
|
163 | }
|
164 | })
|
165 | })
|
166 | })
|
167 |
|
168 | describe('customCommand', () => {
|
169 | it('should make configs', () => {
|
170 | const cmd = 'inspect'
|
171 | const _ = [cmd, 'entry']
|
172 | const type = 'weapp'
|
173 | setProcessArgv('taro inspect entry --type weapp -h --version')
|
174 | cli.run()
|
175 | const ins = MockedKernel.mock.instances[0]
|
176 | expect(ins.run).toHaveBeenCalledWith({
|
177 | name: cmd,
|
178 | opts: {
|
179 | _,
|
180 | options: {
|
181 | type
|
182 | },
|
183 | isHelp: true
|
184 | }
|
185 | })
|
186 | })
|
187 | })
|
188 |
|
189 | describe('others', () => {
|
190 | it('should log helps', () => {
|
191 | const spy = jest.spyOn(console, 'log')
|
192 | spy.mockImplementation(() => {})
|
193 |
|
194 | setProcessArgv('taro -h')
|
195 | cli.run()
|
196 | expect(spy).toBeCalledTimes(17)
|
197 |
|
198 | spy.mockRestore()
|
199 | })
|
200 |
|
201 | it('should log version', () => {
|
202 | const spy = jest.spyOn(console, 'log')
|
203 | spy.mockImplementation(() => {})
|
204 |
|
205 | setProcessArgv('taro -v')
|
206 | cli.run()
|
207 | expect(spy).toBeCalledWith(getPkgVersion())
|
208 |
|
209 | spy.mockRestore()
|
210 | })
|
211 | })
|
212 | })
|