1 | import { chalk } from '@tarojs/helper'
|
2 | import * as path from 'path'
|
3 |
|
4 | import { getPkgVersion } from '../util'
|
5 | import { run } from './utils'
|
6 |
|
7 | jest.mock('envinfo', () => {
|
8 | const envinfo = jest.requireActual('envinfo')
|
9 | return {
|
10 | __esModule: true,
|
11 | async run (data, options) {
|
12 | const res = await envinfo.run(data, { ...options, json: true })
|
13 | return JSON.parse(res)
|
14 | }
|
15 | }
|
16 | })
|
17 |
|
18 | const runInfo = run('info', ['commands/info'])
|
19 |
|
20 | describe('info', () => {
|
21 | it('should exit because there isn\'t a Taro project', async () => {
|
22 | const exitSpy = jest.spyOn(process, 'exit') as jest.SpyInstance<void, any>
|
23 | const logSpy = jest.spyOn(console, 'log')
|
24 |
|
25 | exitSpy.mockImplementation(() => {
|
26 | throw new Error()
|
27 | })
|
28 | logSpy.mockImplementation(() => {})
|
29 |
|
30 | try {
|
31 | await runInfo('')
|
32 | } catch (error) {}
|
33 |
|
34 | expect(exitSpy).toBeCalledWith(1)
|
35 | expect(logSpy).toBeCalledWith(chalk.red('找不到项目配置文件config/index,请确定当前目录是 Taro 项目根目录!'))
|
36 |
|
37 | exitSpy.mockRestore()
|
38 | logSpy.mockRestore()
|
39 | })
|
40 |
|
41 | it('should log information', async () => {
|
42 | const logSpy = jest.spyOn(console, 'log')
|
43 | logSpy.mockImplementation(() => {})
|
44 |
|
45 | const appPath = path.resolve(__dirname, 'fixtures/default')
|
46 | await runInfo(appPath)
|
47 |
|
48 | expect(logSpy).toBeCalledTimes(1)
|
49 | const res = logSpy.mock.calls[0][0]
|
50 | const title = `Taro CLI ${getPkgVersion()} environment info`
|
51 | expect(res.hasOwnProperty(title)).toBeTruthy()
|
52 | const info = res[title]
|
53 | expect('System' in info).toBeTruthy()
|
54 | expect('Binaries' in info).toBeTruthy()
|
55 |
|
56 |
|
57 | expect(Object.keys(info.System)).toEqual(['OS', 'Shell'])
|
58 | expect(Object.keys(info.Binaries)).toEqual(['Node', 'Yarn', 'npm'])
|
59 |
|
60 |
|
61 |
|
62 |
|
63 |
|
64 |
|
65 |
|
66 |
|
67 |
|
68 |
|
69 |
|
70 | logSpy.mockRestore()
|
71 | })
|
72 | })
|