forked from umijs/father
-
Notifications
You must be signed in to change notification settings - Fork 0
/
g.lint.test.ts
115 lines (100 loc) · 3.42 KB
/
g.lint.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import { existsSync, readFileSync, unlinkSync, writeFileSync } from 'fs';
import path from 'path';
import * as cli from '../src/cli/cli';
import { GeneratorHelper } from '../src/commands/generators/utils';
const mockInstall = jest.fn();
jest
.spyOn(GeneratorHelper.prototype, 'installDeps')
.mockImplementation(mockInstall);
const warnSpy = jest.spyOn(console, 'warn').mockImplementation(() => {});
const CASES_DIR = path.join(__dirname, 'fixtures/generator');
describe('lint generator', () => {
const eslintConfPath = path.join(CASES_DIR, '.eslintrc.js');
const styleConfPath = path.join(CASES_DIR, '.stylelintrc');
process.env.APP_ROOT = path.join(CASES_DIR);
afterEach(() => {
[eslintConfPath, styleConfPath].forEach((path) => {
if (existsSync(path)) {
unlinkSync(path);
}
});
writeFileSync(path.join(CASES_DIR, 'package.json'), '{}');
warnSpy.mockReset();
});
describe('eslint', function () {
test('g eslint', async () => {
await cli.run({
args: { _: ['g', 'eslint'], $0: 'node' },
});
const pkg = JSON.parse(
readFileSync(path.join(CASES_DIR, 'package.json'), 'utf-8'),
);
expect(existsSync(eslintConfPath)).toBeTruthy();
expect(pkg['scripts']).toMatchObject({
'lint:es': 'eslint "{src,test}/**/*.{js,jsx,ts,tsx}"',
});
expect(pkg['devDependencies']).toMatchObject({
'@umijs/lint': '^4',
eslint: '^8.23.0',
});
expect(mockInstall).toBeCalled();
});
test('warning when eslint config exists', async () => {
writeFileSync(eslintConfPath, '{}');
await cli.run({
args: { _: ['g', 'eslint'], $0: 'node' },
});
expect(warnSpy.mock.calls[0][1]).toBe(
'ESLint has already enabled. You can remove .eslintrc, then run this again to re-setup.',
);
});
});
describe('stylelint', function () {
test('g stylelint', async () => {
await cli.run({
args: { _: ['g', 'stylelint'], $0: 'node' },
});
const pkg = JSON.parse(
readFileSync(path.join(CASES_DIR, 'package.json'), 'utf-8'),
);
expect(existsSync(styleConfPath)).toBeTruthy();
expect(pkg['scripts']).toMatchObject({
'lint:css': 'stylelint "{src,test}/**/*.{css,less}"',
});
expect(pkg['devDependencies']).toMatchObject({
'@umijs/lint': '^4',
stylelint: '^14.11.0',
});
expect(mockInstall).toBeCalled();
});
test('warning when stylelint config exists', async () => {
writeFileSync(styleConfPath, '{}');
await cli.run({
args: { _: ['g', 'stylelint'], $0: 'node' },
});
expect(warnSpy.mock.calls[0][1]).toBe(
'Stylelint has already enabled. You can remove .stylelintrc/stylelint.config.js, then run this again to re-setup.',
);
});
});
describe('lint', function () {
test('g lint', async () => {
await cli.run({
args: { _: ['g', 'lint'], $0: 'node' },
});
const pkg = JSON.parse(
readFileSync(path.join(CASES_DIR, 'package.json'), 'utf-8'),
);
expect(existsSync(styleConfPath)).toBeTruthy();
expect(pkg['scripts']).toMatchObject({
lint: 'pnpm run lint:es && pnpm run lint:css',
});
expect(pkg['devDependencies']).toMatchObject({
'@umijs/lint': '^4',
eslint: '^8.23.0',
stylelint: '^14.11.0',
});
expect(mockInstall).toBeCalled();
});
});
});