UNPKG

2.71 kBPlain TextView Raw
1import { chalk } from '@tarojs/helper'
2import * as path from 'path'
3
4import { getPkgVersion } from '../util'
5import { run } from './utils'
6
7jest.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
18const runInfo = run('info', ['commands/info'])
19
20describe('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) {} // eslint-disable-line no-empty
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 // envinfo 还不支持 yarn workspace
56 // expect('npmPackages' in info).toBeTruthy()
57 expect(Object.keys(info.System)).toEqual(['OS', 'Shell'])
58 expect(Object.keys(info.Binaries)).toEqual(['Node', 'Yarn', 'npm'])
59 // expect(info.npmPackages.hasOwnProperty('@tarojs/helper')).toBeTruthy()
60 // expect(info.npmPackages.hasOwnProperty('@tarojs/mini-runner')).toBeTruthy()
61 // expect(info.npmPackages.hasOwnProperty('@tarojs/service')).toBeTruthy()
62 // expect(info.npmPackages.hasOwnProperty('@tarojs/taro')).toBeTruthy()
63 // expect(info.npmPackages.hasOwnProperty('@tarojs/taroize')).toBeTruthy()
64 // expect(info.npmPackages.hasOwnProperty('@tarojs/webpack-runner')).toBeTruthy()
65 // expect(info.npmPackages.hasOwnProperty('babel-plugin-transform-taroapi')).toBeTruthy()
66 // expect(info.npmPackages.hasOwnProperty('eslint-config-taro')).toBeTruthy()
67 // expect(info.npmPackages.hasOwnProperty('eslint-plugin-taro')).toBeTruthy()
68 // expect(info.npmPackages.hasOwnProperty('postcss-pxtransform')).toBeTruthy()
69
70 logSpy.mockRestore()
71 })
72})