@putout/test 5.0.0 → 5.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +60 -27
- package/lib/eslint/eslint.mjs +5 -2
- package/lib/processor/index.mjs +10 -0
- package/lib/test.js +23 -1
- package/package.json +7 -4
package/README.md
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
[NPMIMGURL]: https://img.shields.io/npm/v/@putout/test.svg?style=flat&longCache=true
|
|
4
4
|
[NPMURL]: https://npmjs.org/package/@putout/test "npm"
|
|
5
5
|
|
|
6
|
-
Test runner for 🐊[
|
|
6
|
+
Test runner for 🐊[**Putout**](https://github.com/coderaiser/putout#plugins-api). Basically it is [supercharged **tape**](https://github.com/coderaiser/supertape) with additional asseritions:
|
|
7
7
|
|
|
8
8
|
## Install
|
|
9
9
|
|
|
@@ -15,21 +15,24 @@ npm i @putout/test -D
|
|
|
15
15
|
|
|
16
16
|
Set environment variable `UPDATE=1` to update `transform` and `format` fixtures.
|
|
17
17
|
|
|
18
|
+
☝️ *Remember that `-fix.js` fixtures will be removed when used in `noReport`, `noTransform` and `noTransformWithOptions`*
|
|
19
|
+
|
|
18
20
|
```sh
|
|
19
21
|
UPDATE=1 tape test/*.js
|
|
20
22
|
```
|
|
21
23
|
|
|
22
24
|
## Plugins API
|
|
23
25
|
|
|
24
|
-
All
|
|
25
|
-
|
|
26
|
+
All 🐊**Putout** plugins written in `CommonJS`, since `ESLint` written on `CommonJS` and we have a huge `ESLint`-based ecosystem which is good to reuse.
|
|
27
|
+
|
|
28
|
+
🐊**Putout** can be used in all IDE's supported by`ESLint` as [`eslint-plugin-putout`](https://github.com/coderaiser/putout/tree/packages/eslint-plugin-putout).
|
|
26
29
|
When [async rules will be supported](https://github.com/eslint/eslint/issues/15394) we can switch to `ESM`.
|
|
27
30
|
|
|
28
31
|
To write test for your plugins you need initialize `test` using `createTest`:
|
|
29
32
|
|
|
30
33
|
```js
|
|
31
34
|
import {createTest} from '@putout/test';
|
|
32
|
-
|
|
35
|
+
import rmVars from '@putout/plugin-remove-unused-variables';
|
|
33
36
|
|
|
34
37
|
const test = createTest(import.meta.url, {
|
|
35
38
|
'remove-unused-variables': rmVars,
|
|
@@ -38,7 +41,7 @@ const test = createTest(import.meta.url, {
|
|
|
38
41
|
|
|
39
42
|
### `report(filename, message | []messages)`
|
|
40
43
|
|
|
41
|
-
|
|
44
|
+
Check error message (or messages) of a plugin:
|
|
42
45
|
|
|
43
46
|
```js
|
|
44
47
|
test('remove usless variables: for-of', (t) => {
|
|
@@ -47,9 +50,18 @@ test('remove usless variables: for-of', (t) => {
|
|
|
47
50
|
});
|
|
48
51
|
```
|
|
49
52
|
|
|
53
|
+
When you want to check that report called exact count of times pass an array of messages:
|
|
54
|
+
|
|
55
|
+
```js
|
|
56
|
+
test('remove usless variables: for-of', (t) => {
|
|
57
|
+
t.report('dot', ['Dot files should be added to .gitignore']);
|
|
58
|
+
t.end();
|
|
59
|
+
});
|
|
60
|
+
```
|
|
61
|
+
|
|
50
62
|
### `reportCode(input, message)`
|
|
51
63
|
|
|
52
|
-
|
|
64
|
+
Check error message of a plugin from `input` code:
|
|
53
65
|
|
|
54
66
|
```js
|
|
55
67
|
test('remove debugger: report', (t) => {
|
|
@@ -60,7 +72,7 @@ test('remove debugger: report', (t) => {
|
|
|
60
72
|
|
|
61
73
|
### `transform(filename [, output, plugins])`
|
|
62
74
|
|
|
63
|
-
|
|
75
|
+
Check transform of `filename.js` -> `filename-fix.js` in `test/fixtures` directory:
|
|
64
76
|
|
|
65
77
|
```js
|
|
66
78
|
test('remove usless variables: for-of', (t) => {
|
|
@@ -73,7 +85,7 @@ test('remove usless variables: for-of', (t) => {
|
|
|
73
85
|
|
|
74
86
|
### `transformCode(input, output)`
|
|
75
87
|
|
|
76
|
-
|
|
88
|
+
Check transform of `input` -> `output` code:
|
|
77
89
|
|
|
78
90
|
```js
|
|
79
91
|
test('remove-console: property identifier: code', (t) => {
|
|
@@ -84,7 +96,7 @@ test('remove-console: property identifier: code', (t) => {
|
|
|
84
96
|
|
|
85
97
|
### `reportWithOptions(filename, options)`
|
|
86
98
|
|
|
87
|
-
|
|
99
|
+
Check report of `filename.js` with `options`:
|
|
88
100
|
|
|
89
101
|
```js
|
|
90
102
|
test('putout: test: reportWithOptions', (t) => {
|
|
@@ -100,7 +112,7 @@ test('putout: test: reportWithOptions', (t) => {
|
|
|
100
112
|
|
|
101
113
|
### `noReportWithOptions(filename, options)`
|
|
102
114
|
|
|
103
|
-
|
|
115
|
+
Check no report of `filename.js` with `options`:
|
|
104
116
|
|
|
105
117
|
```js
|
|
106
118
|
test('putout: test: noReportWithOptions', (t) => {
|
|
@@ -115,7 +127,7 @@ test('putout: test: noReportWithOptions', (t) => {
|
|
|
115
127
|
|
|
116
128
|
### `transformWithOptions(filename, options)`
|
|
117
129
|
|
|
118
|
-
|
|
130
|
+
Check transform of `filename.js` with `options`:
|
|
119
131
|
|
|
120
132
|
```js
|
|
121
133
|
test('putout: plugin: declare-undefined-variables: transform: parse', (t) => {
|
|
@@ -139,7 +151,7 @@ test('test: declared', (t) => {
|
|
|
139
151
|
|
|
140
152
|
### `noTransformWithOptions(filename, options)`
|
|
141
153
|
|
|
142
|
-
|
|
154
|
+
Check transform of `filename.js` with `options`:
|
|
143
155
|
|
|
144
156
|
```js
|
|
145
157
|
test('putout: plugin: declare-undefined-variables: transform: assign: dismiss', (t) => {
|
|
@@ -152,7 +164,7 @@ test('putout: plugin: declare-undefined-variables: transform: assign: dismiss',
|
|
|
152
164
|
|
|
153
165
|
### `noReport(filename)`
|
|
154
166
|
|
|
155
|
-
|
|
167
|
+
Check error message of a plugin not produces
|
|
156
168
|
|
|
157
169
|
```js
|
|
158
170
|
test('plugin-putout: check-replace-code: no report: typescript', (t) => {
|
|
@@ -163,7 +175,7 @@ test('plugin-putout: check-replace-code: no report: typescript', (t) => {
|
|
|
163
175
|
|
|
164
176
|
### `noReportAfterTransform(filename)`
|
|
165
177
|
|
|
166
|
-
|
|
178
|
+
Check error message of a plugin not produced
|
|
167
179
|
|
|
168
180
|
```js
|
|
169
181
|
test('test: no report after transform', (t) => {
|
|
@@ -174,7 +186,7 @@ test('test: no report after transform', (t) => {
|
|
|
174
186
|
|
|
175
187
|
### `noTransform(filename)`
|
|
176
188
|
|
|
177
|
-
|
|
189
|
+
Check transform of `filename.js` produce nothing
|
|
178
190
|
|
|
179
191
|
```js
|
|
180
192
|
test('plugin-apply-numeric-separators: no transform: hex', (t) => {
|
|
@@ -198,7 +210,7 @@ const test = createTest(import.meta.url, {
|
|
|
198
210
|
|
|
199
211
|
### `format(formatter, filename)`
|
|
200
212
|
|
|
201
|
-
|
|
213
|
+
Check file name formatting (pass `process.env.UPDATE=1` to save `fixture`):
|
|
202
214
|
|
|
203
215
|
```js
|
|
204
216
|
test('formatter: codeframe', async ({format}) => {
|
|
@@ -208,7 +220,7 @@ test('formatter: codeframe', async ({format}) => {
|
|
|
208
220
|
|
|
209
221
|
### `noFormat`
|
|
210
222
|
|
|
211
|
-
|
|
223
|
+
Check that there is no formatting for for such file:
|
|
212
224
|
|
|
213
225
|
```js
|
|
214
226
|
test('formatter: codeframe: no', async ({noFormat}) => {
|
|
@@ -218,7 +230,7 @@ test('formatter: codeframe: no', async ({noFormat}) => {
|
|
|
218
230
|
|
|
219
231
|
### `formatMany(formatter, [filename1, filename2])`
|
|
220
232
|
|
|
221
|
-
|
|
233
|
+
Check file name formatting (pass `process.env.UPDATE=1` to save `fixture`):
|
|
222
234
|
|
|
223
235
|
```js
|
|
224
236
|
test('formatter: dump: many', async ({formatMany}) => {
|
|
@@ -270,7 +282,7 @@ Works in similar to [transform](#transformfilename--output-plugins) way:
|
|
|
270
282
|
|
|
271
283
|
- ✅ reads `operator-linebreak.js`;
|
|
272
284
|
- ✅ transforms it;
|
|
273
|
-
- ✅
|
|
285
|
+
- ✅ check that transformed is equal to `operator-linebreak-fix.js`;
|
|
274
286
|
|
|
275
287
|
Example:
|
|
276
288
|
|
|
@@ -304,21 +316,42 @@ test('test: eslint: noProcess', async ({noProcess}) => {
|
|
|
304
316
|
});
|
|
305
317
|
```
|
|
306
318
|
|
|
307
|
-
### `comparePlaces(filename, places)`
|
|
319
|
+
### `comparePlaces(filename, places[, overrides])`
|
|
308
320
|
|
|
309
321
|
```js
|
|
310
322
|
test('eslint-config: operator-line-break', async ({comparePlaces}) => {
|
|
311
323
|
await comparePlaces('operator-linebreak', [{
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
324
|
+
message: 'There should be no line break before or after \'=\'.',
|
|
325
|
+
position: {
|
|
326
|
+
column: 1,
|
|
327
|
+
line: 2,
|
|
316
328
|
},
|
|
317
|
-
|
|
329
|
+
rule: 'operator-linebreak (eslint)',
|
|
318
330
|
}]);
|
|
319
331
|
});
|
|
320
332
|
```
|
|
321
333
|
|
|
334
|
+
with `overrides`:
|
|
335
|
+
|
|
336
|
+
```js
|
|
337
|
+
test('eslint-config: operator-line-break', async ({comparePlaces}) => {
|
|
338
|
+
const overrides = {
|
|
339
|
+
extends: [
|
|
340
|
+
'plugin:putout/safe',
|
|
341
|
+
],
|
|
342
|
+
};
|
|
343
|
+
|
|
344
|
+
await comparePlaces('operator-linebreak', [{
|
|
345
|
+
message: 'There should be no line break before or after \'=\'.',
|
|
346
|
+
position: {
|
|
347
|
+
column: 1,
|
|
348
|
+
line: 2,
|
|
349
|
+
},
|
|
350
|
+
rule: 'operator-linebreak (eslint)',
|
|
351
|
+
}], overrides);
|
|
352
|
+
});
|
|
353
|
+
```
|
|
354
|
+
|
|
322
355
|
## Processors API
|
|
323
356
|
|
|
324
357
|
With `processors api` you can test `processors` in a simplest possible way.
|
|
@@ -326,9 +359,9 @@ With `processors api` you can test `processors` in a simplest possible way.
|
|
|
326
359
|
First things first, init `test` with:
|
|
327
360
|
|
|
328
361
|
```js
|
|
329
|
-
|
|
362
|
+
import {createTest} from '@putout/test/processor';
|
|
330
363
|
|
|
331
|
-
const test = createTest(
|
|
364
|
+
const test = createTest(import.meta.url, {
|
|
332
365
|
extension: 'json',
|
|
333
366
|
processors: [
|
|
334
367
|
'json',
|
package/lib/eslint/eslint.mjs
CHANGED
|
@@ -59,14 +59,17 @@ export const createTest = (url) => {
|
|
|
59
59
|
|
|
60
60
|
return operator.equal(source, code);
|
|
61
61
|
},
|
|
62
|
-
comparePlaces: (operator) => async (name, expected) => {
|
|
62
|
+
comparePlaces: (operator) => async (name, expected, override) => {
|
|
63
63
|
const full = join(fixtureDir, name);
|
|
64
64
|
const [resolvedName, code] = await read(full);
|
|
65
65
|
|
|
66
66
|
const [, places] = await eslint({
|
|
67
|
-
config,
|
|
68
67
|
name: resolvedName,
|
|
69
68
|
code,
|
|
69
|
+
config: {
|
|
70
|
+
...config,
|
|
71
|
+
...override,
|
|
72
|
+
},
|
|
70
73
|
});
|
|
71
74
|
|
|
72
75
|
return operator.deepEqual(places, expected);
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import {dirname} from 'path';
|
|
2
|
+
import {fileURLToPath} from 'url';
|
|
3
|
+
import processor from './index.js';
|
|
4
|
+
|
|
5
|
+
export const createTest = (url, plugins) => {
|
|
6
|
+
const __filename = fileURLToPath(url);
|
|
7
|
+
const __dirname = dirname(__filename);
|
|
8
|
+
|
|
9
|
+
return processor.createTest(__dirname, plugins);
|
|
10
|
+
};
|
package/lib/test.js
CHANGED
|
@@ -5,6 +5,7 @@ const {
|
|
|
5
5
|
readFileSync,
|
|
6
6
|
writeFileSync,
|
|
7
7
|
existsSync,
|
|
8
|
+
unlinkSync,
|
|
8
9
|
} = require('fs');
|
|
9
10
|
|
|
10
11
|
const tryCatch = require('try-catch');
|
|
@@ -22,6 +23,7 @@ global.__putout_test_fs = {
|
|
|
22
23
|
readFileSync,
|
|
23
24
|
writeFileSync,
|
|
24
25
|
existsSync,
|
|
26
|
+
unlinkSync,
|
|
25
27
|
};
|
|
26
28
|
|
|
27
29
|
const isUpdate = () => Boolean(Number(process.env.UPDATE));
|
|
@@ -40,6 +42,16 @@ const readFixture = (name) => {
|
|
|
40
42
|
return [readFileSync(`${name}.js`, 'utf8'), TS.DISABLED];
|
|
41
43
|
};
|
|
42
44
|
|
|
45
|
+
const rmFixture = (name) => {
|
|
46
|
+
const {unlinkSync} = global.__putout_test_fs;
|
|
47
|
+
|
|
48
|
+
if (!isUpdate())
|
|
49
|
+
return;
|
|
50
|
+
|
|
51
|
+
tryCatch(unlinkSync, `${name}.js`);
|
|
52
|
+
tryCatch(unlinkSync, `${name}.ts`);
|
|
53
|
+
};
|
|
54
|
+
|
|
43
55
|
module.exports = createTest;
|
|
44
56
|
module.exports.createTest = createTest;
|
|
45
57
|
|
|
@@ -125,6 +137,7 @@ const formatMany = currify(({dir, plugins, rules}, t) => async (formatter, names
|
|
|
125
137
|
|
|
126
138
|
const count = names.length;
|
|
127
139
|
const report = putout.initReport();
|
|
140
|
+
|
|
128
141
|
for (let index = 0; index < count; index++) {
|
|
129
142
|
const name = names[index];
|
|
130
143
|
const full = fullNames[index];
|
|
@@ -266,6 +279,8 @@ const noTransformWithOptions = currify(({dir, plugins}, t, name, options) => {
|
|
|
266
279
|
const full = join(dir, name);
|
|
267
280
|
const [input, isTS] = readFixture(full);
|
|
268
281
|
|
|
282
|
+
rmFixture(`${full}-fix`);
|
|
283
|
+
|
|
269
284
|
const [plugin] = plugins;
|
|
270
285
|
const [rule] = keys(plugin);
|
|
271
286
|
|
|
@@ -282,6 +297,8 @@ const noTransform = currify(({dir, plugins, rules}, t, name, addons = {}) => {
|
|
|
282
297
|
const full = join(dir, name);
|
|
283
298
|
const [fixture] = readFixture(full);
|
|
284
299
|
|
|
300
|
+
rmFixture(`${full}-fix`);
|
|
301
|
+
|
|
285
302
|
return transform({dir, plugins, rules}, t, name, fixture, addons);
|
|
286
303
|
});
|
|
287
304
|
|
|
@@ -308,6 +325,8 @@ const noReport = currify(({dir, plugins, rules}, t, name) => {
|
|
|
308
325
|
const full = join(dir, name);
|
|
309
326
|
const [source, isTS] = readFixture(full);
|
|
310
327
|
|
|
328
|
+
rmFixture(`${full}-fix`);
|
|
329
|
+
|
|
311
330
|
return noReportCode({plugins, rules, isTS}, t, source);
|
|
312
331
|
});
|
|
313
332
|
module.exports._createNoReport = noReport;
|
|
@@ -338,6 +357,8 @@ const noReportWithOptions = currify(({dir, plugins}, t, name, options) => {
|
|
|
338
357
|
const full = join(dir, name);
|
|
339
358
|
const [source, isTS] = readFixture(full);
|
|
340
359
|
|
|
360
|
+
rmFixture(`${full}-fix`);
|
|
361
|
+
|
|
341
362
|
const [plugin] = plugins;
|
|
342
363
|
const [rule] = keys(plugin);
|
|
343
364
|
|
|
@@ -360,7 +381,7 @@ const reportCode = currify(({plugins, rules, isTS}, t, source, message) => {
|
|
|
360
381
|
const resultMessages = places.map(getMessage);
|
|
361
382
|
|
|
362
383
|
if (isArray(message))
|
|
363
|
-
return t.deepEqual(resultMessages, message
|
|
384
|
+
return t.deepEqual(resultMessages, message);
|
|
364
385
|
|
|
365
386
|
return t.equal(resultMessages[0], message);
|
|
366
387
|
});
|
|
@@ -420,6 +441,7 @@ function preTest(test, plugin) {
|
|
|
420
441
|
}, options);
|
|
421
442
|
|
|
422
443
|
const entries = Object.entries(rules);
|
|
444
|
+
|
|
423
445
|
for (const [entryName, plugin] of entries) {
|
|
424
446
|
preTest(test, {
|
|
425
447
|
[`${name}/${entryName}`]: plugin,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@putout/test",
|
|
3
|
-
"version": "5.
|
|
3
|
+
"version": "5.3.0",
|
|
4
4
|
"type": "commonjs",
|
|
5
5
|
"author": "coderaiser <mnemonic.enemy@gmail.com> (https://github.com/coderaiser)",
|
|
6
6
|
"description": "test runner for putout plugins ",
|
|
@@ -14,7 +14,10 @@
|
|
|
14
14
|
"require": "./lib/test.js",
|
|
15
15
|
"import": "./lib/test.mjs"
|
|
16
16
|
},
|
|
17
|
-
"./processor":
|
|
17
|
+
"./processor": {
|
|
18
|
+
"require": "./lib/processor/index.js",
|
|
19
|
+
"import": "./lib/processor/index.mjs"
|
|
20
|
+
},
|
|
18
21
|
"./formatter": "./lib/formatter/index.mjs",
|
|
19
22
|
"./eslint": "./lib/eslint/eslint.mjs"
|
|
20
23
|
},
|
|
@@ -61,8 +64,8 @@
|
|
|
61
64
|
"c8": "^7.5.0",
|
|
62
65
|
"eslint": "^8.0.1",
|
|
63
66
|
"eslint-plugin-node": "^11.0.0",
|
|
64
|
-
"eslint-plugin-putout": "^
|
|
65
|
-
"lerna": "^
|
|
67
|
+
"eslint-plugin-putout": "^15.0.0",
|
|
68
|
+
"lerna": "^5.0.0",
|
|
66
69
|
"madrun": "^9.0.0",
|
|
67
70
|
"mock-require": "^3.0.3",
|
|
68
71
|
"nodemon": "^2.0.1"
|