/
EsLint.spec.ts
292 lines (259 loc) · 9.84 KB
/
EsLint.spec.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
import { join } from 'path';
import process from 'process';
import { readFixture } from './sandbox/Fixture';
import { Sandbox, createSandbox } from './sandbox/Sandbox';
import {
createWebpackDevServerDriver,
WEBPACK_CLI_VERSION,
WEBPACK_DEV_SERVER_VERSION,
} from './sandbox/WebpackDevServerDriver';
import { FORK_TS_CHECKER_WEBPACK_PLUGIN_VERSION } from './sandbox/Plugin';
const ignored = process.version.startsWith('v10');
describe('EsLint', () => {
let sandbox: Sandbox;
beforeAll(async () => {
sandbox = await createSandbox();
});
beforeEach(async () => {
await sandbox.reset();
});
afterAll(async () => {
await sandbox.cleanup();
});
it.each([
{ async: false, webpack: '4.0.0', eslint: '^6.0.0', absolute: false },
{ async: true, webpack: '^4.0.0', eslint: '^7.0.0', absolute: true },
{ async: false, webpack: '^5.0.0', eslint: '^7.0.0', absolute: true },
{
async: true,
webpack: '^5.0.0',
eslint: '^8.0.0',
absolute: false,
},
])('reports lint error for %p', async ({ async, webpack, eslint, absolute }) => {
if (ignored) {
console.warn('Ignoring test - incompatible node version');
return;
}
await sandbox.load([
await readFixture(join(__dirname, 'fixtures/environment/eslint-basic.fixture'), {
FORK_TS_CHECKER_WEBPACK_PLUGIN_VERSION: JSON.stringify(
FORK_TS_CHECKER_WEBPACK_PLUGIN_VERSION
),
TS_LOADER_VERSION: JSON.stringify('^5.0.0'),
ESLINT_VERSION: JSON.stringify(eslint),
TYPESCRIPT_VERSION: JSON.stringify('~3.8.0'),
WEBPACK_VERSION: JSON.stringify(webpack),
WEBPACK_CLI_VERSION: JSON.stringify(WEBPACK_CLI_VERSION),
WEBPACK_DEV_SERVER_VERSION: JSON.stringify(WEBPACK_DEV_SERVER_VERSION),
ASYNC: JSON.stringify(async),
}),
await readFixture(join(__dirname, 'fixtures/implementation/typescript-basic.fixture')),
]);
if (absolute) {
// test case for providing absolute path to files
await sandbox.patch(
'webpack.config.js',
"files: './src/**/*.{ts,tsx,js,jsx}'",
"files: path.resolve(__dirname, './src/**/*.{ts,tsx,js,jsx}')"
);
}
const driver = createWebpackDevServerDriver(sandbox.spawn('npm run webpack-dev-server'), async);
// first compilation contains 2 warnings
expect(await driver.waitForErrors()).toEqual([
[
'WARNING in src/authenticate.ts:14:34',
'@typescript-eslint/no-explicit-any: Unexpected any. Specify a different type.',
' 12 | }',
' 13 |',
' > 14 | async function logout(): Promise<any> {',
' | ^^^',
' 15 | const response = await fetch(',
" 16 | '/logout',",
' 17 | {',
].join('\n'),
[
'WARNING in src/index.ts:31:44',
"@typescript-eslint/no-unused-vars: 'event' is defined but never used.",
' 29 | }',
' 30 | });',
" > 31 | loginForm.addEventListener('submit', async event => {",
' | ^^^^^',
' 32 | const user = await login(email, password);',
' 33 |',
" 34 | if (user.role === 'admin') {",
].join('\n'),
]);
// fix these issues
await Promise.all([
sandbox.patch(
'src/authenticate.ts',
'async function logout(): Promise<any> {',
'async function logout(): Promise<unknown> {'
),
sandbox.patch(
'src/index.ts',
"loginForm.addEventListener('submit', async event => {",
"loginForm.addEventListener('submit', async () => {"
),
]);
// next iteration should have no errors
await driver.waitForNoErrors();
// add a file that shouldn't be linted
await sandbox.write('src/style.css', 'body { background: red; }');
await sandbox.patch(
'src/index.ts',
"import { getUserName } from './model/User';",
"import { getUserName } from './model/User';\nimport './style.css';"
);
// next iteration should have no errors
await driver.waitForNoErrors();
// modify the css again
await sandbox.patch('src/style.css', 'body { background: red; }', 'body { background: blue; }');
// next iteration should have no errors
await driver.waitForNoErrors();
// add a new error
await sandbox.patch(
'src/model/User.ts',
[' lastName?: string;', '}'].join('\n'),
[' lastName?: string;', '}', '', 'let temporary: any;', ''].join('\n')
);
expect(await driver.waitForErrors()).toEqual([
[
'WARNING in src/model/User.ts:11:5',
"@typescript-eslint/no-unused-vars: 'temporary' is defined but never used.",
' 9 | }',
' 10 |',
' > 11 | let temporary: any;',
' | ^^^^^^^^^^^^^^',
' 12 |',
' 13 |',
' 14 | function getUserName(user: User): string {',
].join('\n'),
[
'WARNING in src/model/User.ts:11:16',
'@typescript-eslint/no-explicit-any: Unexpected any. Specify a different type.',
' 9 | }',
' 10 |',
' > 11 | let temporary: any;',
' | ^^^',
' 12 |',
' 13 |',
' 14 | function getUserName(user: User): string {',
].join('\n'),
]);
});
it('adds files dependencies to webpack', async () => {
if (ignored) {
console.warn('Ignoring test - incompatible node version');
return;
}
await sandbox.load([
await readFixture(join(__dirname, 'fixtures/environment/eslint-basic.fixture'), {
FORK_TS_CHECKER_WEBPACK_PLUGIN_VERSION: JSON.stringify(
FORK_TS_CHECKER_WEBPACK_PLUGIN_VERSION
),
TS_LOADER_VERSION: JSON.stringify('^5.0.0'),
ESLINT_VERSION: JSON.stringify('~6.8.0'),
TYPESCRIPT_VERSION: JSON.stringify('~3.8.0'),
WEBPACK_VERSION: JSON.stringify('^4.0.0'),
WEBPACK_CLI_VERSION: JSON.stringify(WEBPACK_CLI_VERSION),
WEBPACK_DEV_SERVER_VERSION: JSON.stringify(WEBPACK_DEV_SERVER_VERSION),
ASYNC: JSON.stringify(false),
}),
await readFixture(join(__dirname, 'fixtures/implementation/typescript-basic.fixture')),
]);
// update configuration
await sandbox.patch(
'webpack.config.js',
"files: './src/**/*.{ts,tsx,js,jsx}'",
"files: './outside/**/*.{ts,tsx,js,jsx}'"
);
// create a file with lint error
await sandbox.write('./outside/test.ts', 'const x = 4;');
const driver = createWebpackDevServerDriver(sandbox.spawn('npm run webpack-dev-server'), false);
// initially we should have 1 error
expect(await driver.waitForErrors()).toEqual([
[
'WARNING in outside/test.ts:1:7',
"@typescript-eslint/no-unused-vars: 'x' is assigned a value but never used.",
' > 1 | const x = 4;',
' | ^',
].join('\n'),
]);
// let's fix that error
await sandbox.write('./outside/test.ts', 'export const x = 4;');
await driver.waitForNoErrors();
// add a new file in this directory
await sandbox.write('./outside/another.ts', '');
await driver.waitForNoErrors();
// update another.ts with a code that has 1 lint error
await sandbox.write('./outside/another.ts', 'const y = 5;');
expect(await driver.waitForErrors()).toEqual([
[
'WARNING in outside/another.ts:1:7',
"@typescript-eslint/no-unused-vars: 'y' is assigned a value but never used.",
' > 1 | const y = 5;',
' | ^',
].join('\n'),
]);
// let's remove this file - this will check if we handle remove events
await sandbox.remove('./outside/another.ts');
await driver.waitForNoErrors();
});
it.each([{ eslint: '^6.0.0' }, { eslint: '^7.0.0' }, { eslint: '^8.0.0' }])(
'fixes errors with `fix: true` option for %p',
async ({ eslint }) => {
if (ignored) {
console.warn('Ignoring test - incompatible node version');
return;
}
await sandbox.load([
await readFixture(join(__dirname, 'fixtures/environment/eslint-basic.fixture'), {
FORK_TS_CHECKER_WEBPACK_PLUGIN_VERSION: JSON.stringify(
FORK_TS_CHECKER_WEBPACK_PLUGIN_VERSION
),
TS_LOADER_VERSION: JSON.stringify('^5.0.0'),
ESLINT_VERSION: JSON.stringify(eslint),
TYPESCRIPT_VERSION: JSON.stringify('~3.8.0'),
WEBPACK_VERSION: JSON.stringify('^4.0.0'),
WEBPACK_CLI_VERSION: JSON.stringify(WEBPACK_CLI_VERSION),
WEBPACK_DEV_SERVER_VERSION: JSON.stringify(WEBPACK_DEV_SERVER_VERSION),
ASYNC: JSON.stringify(false),
}),
await readFixture(join(__dirname, 'fixtures/implementation/typescript-basic.fixture')),
]);
// fix initial issues
await sandbox.patch(
'src/authenticate.ts',
'async function logout(): Promise<any> {',
'async function logout(): Promise<unknown> {'
);
await sandbox.patch(
'src/index.ts',
"loginForm.addEventListener('submit', async event => {",
"loginForm.addEventListener('submit', async () => {"
);
// set fix option for the eslint
await sandbox.write(
'fork-ts-checker.config.js',
'module.exports = { eslint: { enabled: true, options: { fix: true } } };'
);
// add fixable issue
await sandbox.patch(
'src/authenticate.ts',
'const response = await fetch(',
'let response = await fetch('
);
const driver = createWebpackDevServerDriver(
sandbox.spawn('npm run webpack-dev-server'),
false
);
// it should be automatically fixed
await driver.waitForNoErrors();
// check if issue has been fixed
const content = await sandbox.read('src/authenticate.ts');
expect(content).not.toContain('let response = await fetch(');
}
);
});