@putout/test 4.2.0 → 5.0.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 CHANGED
@@ -255,6 +255,70 @@ test('test: declared', (t) => {
255
255
  });
256
256
  ```
257
257
 
258
+ ## ESLint API
259
+
260
+ First you need to create test with:
261
+
262
+ ```js
263
+ import {createTest} from '@putout/test/eslint';
264
+ const test = createTest(import.meta.url);
265
+ ```
266
+
267
+ ### `process(filename [, config])`
268
+
269
+ Works in similar to [transform](#transformfilename--output-plugins) way:
270
+
271
+ - ✅ reads `operator-linebreak.js`;
272
+ - ✅ transforms it;
273
+ - ✅ checks that transformed is equal to `operator-linebreak-fix.js`;
274
+
275
+ Example:
276
+
277
+ ```js
278
+ test('test: eslint: transform', async ({process}) => {
279
+ await process('operator-linebreak');
280
+ });
281
+
282
+ test('test: eslint: transform', async ({process}) => {
283
+ await process('operator-linebreak', {
284
+ rules: {
285
+ 'putout/putout': {
286
+ rules: {
287
+ 'convert-esm-to-commonjs': 'on',
288
+ },
289
+ },
290
+ },
291
+ });
292
+ });
293
+ ```
294
+
295
+ ### `noProcess(filename)`
296
+
297
+ Check that filename would not be processed.
298
+
299
+ Example:
300
+
301
+ ```js
302
+ test('test: eslint: noProcess', async ({noProcess}) => {
303
+ await noProcess('operator-linebreak-fix');
304
+ });
305
+ ```
306
+
307
+ ### `comparePlaces(filename, places)`
308
+
309
+ ```js
310
+ test('eslint-config: operator-line-break', async ({comparePlaces}) => {
311
+ await comparePlaces('operator-linebreak', [{
312
+ "message": "There should be no line break before or after '='.",
313
+ "position": {
314
+ "column": 1,
315
+ "line": 2,
316
+ },
317
+ "rule": "operator-linebreak (eslint)",
318
+ }]);
319
+ });
320
+ ```
321
+
258
322
  ## Processors API
259
323
 
260
324
  With `processors api` you can test `processors` in a simplest possible way.
@@ -0,0 +1,76 @@
1
+ import {readFile} from 'fs/promises';
2
+ import {join} from 'path';
3
+
4
+ import eslint from 'putout/eslint';
5
+ import tryToCatch from 'try-to-catch';
6
+ import {extend} from 'supertape';
7
+
8
+ const config = {
9
+ extends: [
10
+ 'plugin:node/recommended',
11
+ 'plugin:eslint-plugin/recommended',
12
+ 'plugin:putout/recommended',
13
+ ],
14
+ };
15
+
16
+ const read = async (name) => {
17
+ const [, data] = await tryToCatch(readFile, `${name}.js`, 'utf8');
18
+
19
+ if (data)
20
+ return [`${name}.js`, data];
21
+
22
+ return [`${name}.ts`, await readFile(`${name}.ts`, 'utf8')];
23
+ };
24
+
25
+ export const createTest = (url) => {
26
+ const fixtureDir = new URL('fixture', url).pathname;
27
+
28
+ return extend({
29
+ process: (operator) => async (name, override) => {
30
+ const full = join(fixtureDir, name);
31
+ const [resolvedName, code] = await read(full);
32
+ const [, fixture] = await read(`${full}-fix`);
33
+ const fix = true;
34
+
35
+ const [source] = await eslint({
36
+ name: resolvedName,
37
+ code,
38
+ fix,
39
+ putout: true,
40
+ config: {
41
+ ...config,
42
+ ...override,
43
+ },
44
+ });
45
+
46
+ return operator.equal(source, fixture);
47
+ },
48
+ noProcess: (operator) => async (name) => {
49
+ const full = join(fixtureDir, name);
50
+ const [resolvedName, code] = await read(full);
51
+ const fix = true;
52
+
53
+ const [source] = await eslint({
54
+ name: resolvedName,
55
+ config,
56
+ code,
57
+ fix,
58
+ });
59
+
60
+ return operator.equal(source, code);
61
+ },
62
+ comparePlaces: (operator) => async (name, expected) => {
63
+ const full = join(fixtureDir, name);
64
+ const [resolvedName, code] = await read(full);
65
+
66
+ const [, places] = await eslint({
67
+ config,
68
+ name: resolvedName,
69
+ code,
70
+ });
71
+
72
+ return operator.deepEqual(places, expected);
73
+ },
74
+ });
75
+ };
76
+
package/lib/test.js CHANGED
@@ -18,15 +18,13 @@ const isString = (a) => typeof a === 'string';
18
18
  const {isArray} = Array;
19
19
  const {keys, entries} = Object;
20
20
 
21
- const {UPDATE} = process.env;
22
- global.__putout_test_update = UPDATE;
23
21
  global.__putout_test_fs = {
24
22
  readFileSync,
25
23
  writeFileSync,
26
24
  existsSync,
27
25
  };
28
26
 
29
- const isUpdate = () => UPDATE || global.__putout_test_update;
27
+ const isUpdate = () => Boolean(Number(process.env.UPDATE));
30
28
 
31
29
  const TS = {
32
30
  ENABLED: true,
@@ -214,6 +212,8 @@ const formatSave = currify(({dir, plugins, rules}, t) => async (formatter, name,
214
212
  });
215
213
 
216
214
  const transform = currify(({dir, plugins, rules}, t, name, transformed = null, addons = {}) => {
215
+ const {writeFileSync} = global.__putout_test_fs;
216
+
217
217
  const full = join(dir, name);
218
218
  const [input, isTS] = readFixture(full);
219
219
  const isStr = isString(transformed);
@@ -234,13 +234,15 @@ const transform = currify(({dir, plugins, rules}, t, name, transformed = null, a
234
234
  }],
235
235
  });
236
236
 
237
- if (isUpdate())
237
+ if (isUpdate() && !isStr) {
238
238
  writeFileSync(`${full}-fix.js`, code);
239
+ }
239
240
 
240
241
  return t.equal(code, output);
241
242
  });
242
243
 
243
244
  const transformWithOptions = currify(({dir, plugins}, t, name, options) => {
245
+ const {writeFileSync} = global.__putout_test_fs;
244
246
  const full = join(dir, name);
245
247
  const [input, isTS] = readFixture(full);
246
248
 
@@ -407,11 +409,15 @@ function preTest(test, plugin) {
407
409
  match,
408
410
  }] = entries(plugin).pop();
409
411
 
412
+ const options = {
413
+ checkDuplicates: false,
414
+ };
415
+
410
416
  if (rules) {
411
417
  test(`${name}: rules is an object`, (t) => {
412
418
  t.equal(typeof rules, 'object', 'should export "rules" object');
413
419
  t.end();
414
- }, {checkDuplicates: false});
420
+ }, options);
415
421
 
416
422
  const entries = Object.entries(rules);
417
423
  for (const [entryName, plugin] of entries) {
@@ -423,10 +429,6 @@ function preTest(test, plugin) {
423
429
  return;
424
430
  }
425
431
 
426
- const options = {
427
- checkDuplicates: false,
428
- };
429
-
430
432
  test(`${name}: report: is function`, (t) => {
431
433
  t.equal(typeof report, 'function', 'should export "report" function');
432
434
  t.end();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@putout/test",
3
- "version": "4.2.0",
3
+ "version": "5.0.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 ",
@@ -15,7 +15,8 @@
15
15
  "import": "./lib/test.mjs"
16
16
  },
17
17
  "./processor": "./lib/processor/index.js",
18
- "./formatter": "./lib/formatter/index.mjs"
18
+ "./formatter": "./lib/formatter/index.mjs",
19
+ "./eslint": "./lib/eslint/eslint.mjs"
19
20
  },
20
21
  "release": false,
21
22
  "tag": false,
@@ -38,8 +39,9 @@
38
39
  "@putout/engine-processor": "*",
39
40
  "currify": "^4.0.0",
40
41
  "putout": "*",
41
- "supertape": "^6.0.0",
42
- "try-catch": "^3.0.0"
42
+ "supertape": "^7.0.0",
43
+ "try-catch": "^3.0.0",
44
+ "try-to-catch": "^3.0.0"
43
45
  },
44
46
  "keywords": [
45
47
  "putout",
@@ -61,13 +63,13 @@
61
63
  "eslint-plugin-node": "^11.0.0",
62
64
  "eslint-plugin-putout": "^13.0.0",
63
65
  "lerna": "^4.0.0",
64
- "madrun": "^8.0.1",
66
+ "madrun": "^9.0.0",
65
67
  "mock-require": "^3.0.3",
66
68
  "nodemon": "^2.0.1"
67
69
  },
68
70
  "license": "MIT",
69
71
  "engines": {
70
- "node": ">=14"
72
+ "node": ">=16"
71
73
  },
72
74
  "publishConfig": {
73
75
  "access": "public"