1 | import * as path from 'path'
|
2 | import { run } from './utils'
|
3 | import { getPkgVersion } from '../util'
|
4 | import { exec } from 'child_process'
|
5 | import {
|
6 | chalk,
|
7 | fs,
|
8 | shouldUseCnpm,
|
9 | shouldUseYarn,
|
10 | PROJECT_CONFIG
|
11 | } from '@tarojs/helper'
|
12 |
|
13 | const runUpdate = run('update')
|
14 | const lastestVersion = getPkgVersion()
|
15 |
|
16 | jest.mock('child_process', () => {
|
17 | const exec = jest.fn()
|
18 | exec.mockReturnValue({
|
19 | stdout: {
|
20 | on () {}
|
21 | },
|
22 | stderr: {
|
23 | on () {}
|
24 | }
|
25 | })
|
26 | return {
|
27 | __esModule: true,
|
28 | exec
|
29 | }
|
30 | })
|
31 |
|
32 | jest.mock('ora', () => {
|
33 | const ora = jest.fn()
|
34 | ora.mockReturnValue({
|
35 | start () {}
|
36 | })
|
37 | return ora
|
38 | })
|
39 |
|
40 | jest.mock('@tarojs/helper', () => {
|
41 | const helper = jest.requireActual('@tarojs/helper')
|
42 | const fs = jest.requireActual('fs-extra')
|
43 | return {
|
44 | __esModule: true,
|
45 | ...helper,
|
46 | shouldUseCnpm: jest.fn(),
|
47 | shouldUseYarn: jest.fn(),
|
48 | chalk: {
|
49 | red: jest.fn(),
|
50 | green () {}
|
51 | },
|
52 | fs: {
|
53 | ...fs,
|
54 | writeJson: jest.fn()
|
55 | }
|
56 | }
|
57 | })
|
58 |
|
59 | jest.mock('latest-version', () => () => lastestVersion)
|
60 |
|
61 | function updatePkg (pkgPath: string, version: string) {
|
62 | let packageMap = require(pkgPath)
|
63 | packageMap = {
|
64 | ...packageMap,
|
65 | dependencies: {
|
66 | ...packageMap.dependencies,
|
67 | '@tarojs/shared': version,
|
68 | '@tarojs/taro': version,
|
69 | '@tarojs/cli': version,
|
70 | '@tarojs/components': version,
|
71 | '@tarojs/taro-h5': version,
|
72 | '@tarojs/helper': version,
|
73 | '@tarojs/taro-loader': version,
|
74 | '@tarojs/mini-runner': version,
|
75 | '@tarojs/react': version,
|
76 | '@tarojs/router': version,
|
77 | '@tarojs/runner-utils': version,
|
78 | '@tarojs/runtime': version,
|
79 | '@tarojs/service': version,
|
80 | '@tarojs/webpack-runner': version,
|
81 | '@tarojs/with-weapp': version,
|
82 | '@tarojs/taroize': version,
|
83 | '@tarojs/plugin-platform-weapp': version,
|
84 | '@tarojs/plugin-platform-alipay': version,
|
85 | '@tarojs/plugin-platform-swan': version,
|
86 | '@tarojs/plugin-platform-tt': version,
|
87 | '@tarojs/plugin-platform-jd': version,
|
88 | '@tarojs/plugin-platform-qq': version
|
89 | },
|
90 | devDependencies: {
|
91 | ...packageMap.devDependencies,
|
92 | 'babel-preset-taro': version,
|
93 | 'eslint-config-taro': version,
|
94 | 'babel-plugin-transform-taroapi': version,
|
95 | 'eslint-plugin-taro': version,
|
96 | 'postcss-plugin-constparse': version,
|
97 | 'postcss-pxtransform': version
|
98 | }
|
99 | }
|
100 | return packageMap
|
101 | }
|
102 |
|
103 | describe('update', () => {
|
104 | const execMocked = (exec as unknown) as jest.Mock<any>
|
105 | const shouldUseCnpmMocked = shouldUseCnpm as jest.Mock<any>
|
106 | const shouldUseYarnMocked = shouldUseYarn as jest.Mock<any>
|
107 | const writeJson = fs.writeJson as jest.Mock<any>
|
108 |
|
109 | beforeEach(() => {
|
110 | shouldUseCnpmMocked.mockReturnValue(false)
|
111 | shouldUseYarnMocked.mockReturnValue(false)
|
112 | })
|
113 |
|
114 | afterEach(() => {
|
115 | execMocked.mockClear()
|
116 | shouldUseCnpmMocked.mockReset()
|
117 | shouldUseYarnMocked.mockReset()
|
118 | writeJson.mockClear()
|
119 | })
|
120 |
|
121 | it('should log errors', async () => {
|
122 | const spy = jest.spyOn(console, 'log')
|
123 | spy.mockImplementation(() => {})
|
124 | await runUpdate('')
|
125 | expect(spy).toBeCalledTimes(3)
|
126 | spy.mockRestore()
|
127 | })
|
128 |
|
129 | it('should update self', async () => {
|
130 | await runUpdate('', {
|
131 | args: ['self']
|
132 | })
|
133 | expect(execMocked).toBeCalledWith(`npm i -g @tarojs/cli@${lastestVersion}`)
|
134 | })
|
135 |
|
136 | it('should update self using cnpm', async () => {
|
137 | shouldUseCnpmMocked.mockReturnValue(true)
|
138 | await runUpdate('', {
|
139 | args: ['self']
|
140 | })
|
141 | expect(execMocked).toBeCalledWith(`cnpm i -g @tarojs/cli@${lastestVersion}`)
|
142 | })
|
143 |
|
144 | it('should update self to specific version', async () => {
|
145 | const version = '3.0.0-beta.0'
|
146 | await runUpdate('', {
|
147 | args: ['self', version]
|
148 | })
|
149 | expect(execMocked).toBeCalledWith(`npm i -g @tarojs/cli@${version}`)
|
150 | })
|
151 |
|
152 | it('should throw when there isn\'t a Taro project', async () => {
|
153 | const chalkMocked = (chalk.red as unknown) as jest.Mock<any>
|
154 | const exitSpy = jest.spyOn(process, 'exit')
|
155 | const logSpy = jest.spyOn(console, 'log')
|
156 | exitSpy.mockImplementation(() => {
|
157 | throw new Error()
|
158 | })
|
159 | logSpy.mockImplementation(() => {})
|
160 | try {
|
161 | await runUpdate('', {
|
162 | args: ['project']
|
163 | })
|
164 | } catch (error) {}
|
165 | expect(exitSpy).toBeCalledWith(1)
|
166 | expect(chalkMocked).toBeCalledWith(`找不到项目配置文件${PROJECT_CONFIG},请确定当前目录是Taro项目根目录!`)
|
167 | exitSpy.mockRestore()
|
168 | logSpy.mockRestore()
|
169 | })
|
170 |
|
171 | it('should update project', async () => {
|
172 | const appPath = path.resolve(__dirname, 'fixtures/default')
|
173 | const pkgPath = path.join(appPath, 'package.json')
|
174 | const packageMap = updatePkg(pkgPath, lastestVersion)
|
175 |
|
176 | const logSpy = jest.spyOn(console, 'log')
|
177 | logSpy.mockImplementation(() => {})
|
178 |
|
179 | await runUpdate(appPath, {
|
180 | args: ['project']
|
181 | })
|
182 | expect(writeJson.mock.calls[0][0]).toEqual(pkgPath)
|
183 | expect(writeJson.mock.calls[0][1]).toEqual(packageMap)
|
184 | expect(execMocked).toBeCalledWith('npm install')
|
185 |
|
186 | logSpy.mockRestore()
|
187 | })
|
188 |
|
189 | it('should update project to specific version', async () => {
|
190 | const version = '3.0.0-beta.4'
|
191 | const appPath = path.resolve(__dirname, 'fixtures/default')
|
192 | const pkgPath = path.join(appPath, 'package.json')
|
193 | const packageMap = updatePkg(pkgPath, version)
|
194 |
|
195 | const logSpy = jest.spyOn(console, 'log')
|
196 | logSpy.mockImplementation(() => {})
|
197 |
|
198 | await runUpdate(appPath, {
|
199 | args: ['project', version]
|
200 | })
|
201 | expect(writeJson.mock.calls[0][0]).toEqual(pkgPath)
|
202 | expect(writeJson.mock.calls[0][1]).toEqual(packageMap)
|
203 | expect(execMocked).toBeCalledWith('npm install')
|
204 |
|
205 | logSpy.mockRestore()
|
206 | })
|
207 |
|
208 | it('should update project with yarn', async () => {
|
209 | const appPath = path.resolve(__dirname, 'fixtures/default')
|
210 |
|
211 | const logSpy = jest.spyOn(console, 'log')
|
212 | logSpy.mockImplementation(() => {})
|
213 | shouldUseYarnMocked.mockReturnValue(true)
|
214 |
|
215 | await runUpdate(appPath, {
|
216 | args: ['project']
|
217 | })
|
218 | expect(execMocked).toBeCalledWith('yarn')
|
219 |
|
220 | logSpy.mockRestore()
|
221 | })
|
222 |
|
223 | it('should update project with cnpm', async () => {
|
224 | const appPath = path.resolve(__dirname, 'fixtures/default')
|
225 |
|
226 | const logSpy = jest.spyOn(console, 'log')
|
227 | logSpy.mockImplementation(() => {})
|
228 | shouldUseCnpmMocked.mockReturnValue(true)
|
229 |
|
230 | await runUpdate(appPath, {
|
231 | args: ['project']
|
232 | })
|
233 | expect(execMocked).toBeCalledWith('cnpm install')
|
234 |
|
235 | logSpy.mockRestore()
|
236 | })
|
237 | })
|