1 | import { chalk, fs } from '@tarojs/helper'
|
2 | import * as path from 'path'
|
3 |
|
4 | import validator from '../doctor/recommandValidator'
|
5 |
|
6 | jest.mock('@tarojs/helper', () => {
|
7 | const helper = jest.requireActual('@tarojs/helper')
|
8 | const fs = helper.fs
|
9 | return {
|
10 | __esModule: true,
|
11 | ...helper,
|
12 | fs: {
|
13 | ...fs,
|
14 | readdirSync: jest.fn(),
|
15 | existsSync: jest.fn()
|
16 | }
|
17 | }
|
18 | })
|
19 |
|
20 | describe('recommand validator of doctor', () => {
|
21 | const existsSyncMocked = fs.existsSync as jest.Mock<any>
|
22 | const readdirSyncMocked = fs.readdirSync as jest.Mock<any>
|
23 |
|
24 | beforeEach(() => {
|
25 | jest.resetModules()
|
26 | existsSyncMocked.mockReset()
|
27 | readdirSyncMocked.mockReset()
|
28 | existsSyncMocked.mockReturnValue(true)
|
29 | })
|
30 |
|
31 | it('should exit because there isn\'t a Taro project', async () => {
|
32 | const exitSpy = jest.spyOn(process, 'exit') as jest.SpyInstance<void, any>
|
33 | const logSpy = jest.spyOn(console, 'log')
|
34 |
|
35 | exitSpy.mockImplementation(() => {
|
36 | throw new Error()
|
37 | })
|
38 | logSpy.mockImplementation(() => {})
|
39 | existsSyncMocked.mockReturnValue(false)
|
40 |
|
41 | try {
|
42 | await validator({ appPath: 'src/' })
|
43 | } catch (error) {}
|
44 |
|
45 | expect(exitSpy).toBeCalledWith(1)
|
46 | expect(logSpy).toBeCalledWith(chalk.red('找不到src/package.json,请确定当前目录是Taro项目根目录!'))
|
47 |
|
48 | exitSpy.mockRestore()
|
49 | logSpy.mockRestore()
|
50 | })
|
51 |
|
52 | it('should warn when test framework not found', async () => {
|
53 | jest.doMock('./fixtures/default/package.json', () => ({
|
54 | devDependencies: {
|
55 | eslint: 1
|
56 | }
|
57 | }))
|
58 | readdirSyncMocked.mockReturnValue(['readme', '.gitignore', '.editorconfig'])
|
59 |
|
60 | const { lines } = await validator({ appPath: path.join(__dirname, './fixtures/default') })
|
61 |
|
62 | expect(lines.length).toBe(1)
|
63 | expect(lines[0].desc).toBe('没有检查到常见的测试依赖(jest/mocha/ava/tape/jesmine/karma), 配置测试可以帮助提升项目质量')
|
64 | expect(lines[0].valid).toBe(true)
|
65 | expect(lines[0].solution).toBe('可以参考 https://github.com/NervJS/taro-ui-sample 项目, 其中已经包含了完整的测试配置与范例')
|
66 |
|
67 | jest.dontMock('./fixtures/default/package.json')
|
68 | })
|
69 |
|
70 | it('should warn when linters not found', async () => {
|
71 | jest.doMock('./fixtures/default/package.json', () => ({
|
72 | devDependencies: {
|
73 | jest: 1
|
74 | }
|
75 | }))
|
76 | readdirSyncMocked.mockReturnValue(['readme.md', '.gitignore', '.editorconfig'])
|
77 |
|
78 | const { lines } = await validator({ appPath: path.join(__dirname, './fixtures/default') })
|
79 |
|
80 | expect(lines.length).toBe(1)
|
81 | expect(lines[0].desc).toBe('没有检查到常见的 linter (eslint/jslint/jshint/tslint), 配置 linter 可以帮助提升项目质量')
|
82 | expect(lines[0].valid).toBe(true)
|
83 | expect(lines[0].solution).toBe('Taro 还提供了定制的 ESLint 规则, 可以帮助开发者避免一些常见的问题. 使用 taro cli 创建新项目即可体验')
|
84 |
|
85 | jest.dontMock('./fixtures/default/package.json')
|
86 | })
|
87 |
|
88 | it('should warn when Readme not found', async () => {
|
89 | jest.doMock('./fixtures/default/package.json', () => ({
|
90 | devDependencies: {
|
91 | mocha: 1,
|
92 | jslint: 1
|
93 | }
|
94 | }))
|
95 | readdirSyncMocked.mockReturnValue(['.gitignore', '.editorconfig'])
|
96 |
|
97 | const { lines } = await validator({ appPath: path.join(__dirname, './fixtures/default') })
|
98 |
|
99 | expect(lines.length).toBe(1)
|
100 | expect(lines[0].desc).toBe('没有检查到 Readme (readme/readme.md/readme.markdown), 编写 Readme 可以方便其他人了解项目')
|
101 | expect(lines[0].valid).toBe(true)
|
102 |
|
103 | jest.dontMock('./fixtures/default/package.json')
|
104 | })
|
105 |
|
106 | it('should warn when .gitignore not found', async () => {
|
107 | jest.doMock('./fixtures/default/package.json', () => ({
|
108 | devDependencies: {
|
109 | jesmine: 1,
|
110 | tslint: 1
|
111 | }
|
112 | }))
|
113 | readdirSyncMocked.mockReturnValue(['readme.markdown', '.editorconfig'])
|
114 |
|
115 | const { lines } = await validator({ appPath: path.join(__dirname, './fixtures/default') })
|
116 |
|
117 | expect(lines.length).toBe(1)
|
118 | expect(lines[0].desc).toBe('没有检查到 .gitignore 配置, 配置 .gitignore 以避免将敏感信息或不必要的内容提交到代码仓库')
|
119 | expect(lines[0].valid).toBe(true)
|
120 |
|
121 | jest.dontMock('./fixtures/default/package.json')
|
122 | })
|
123 |
|
124 | it('should warn when .editorconfig not found', async () => {
|
125 | jest.doMock('./fixtures/default/package.json', () => ({
|
126 | devDependencies: {
|
127 | karma: 1,
|
128 | jshint: 1
|
129 | }
|
130 | }))
|
131 | readdirSyncMocked.mockReturnValue(['readme', '.gitignore'])
|
132 |
|
133 | const { lines } = await validator({ appPath: path.join(__dirname, './fixtures/default') })
|
134 |
|
135 | expect(lines.length).toBe(1)
|
136 | expect(lines[0].desc).toBe('没有检查到 .editconfig 配置, 配置 editconfig 以统一项目成员编辑器的代码风格')
|
137 | expect(lines[0].valid).toBe(true)
|
138 |
|
139 | jest.dontMock('./fixtures/default/package.json')
|
140 | })
|
141 | })
|