@putout/test 4.2.0 → 4.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 CHANGED
@@ -255,6 +255,64 @@ 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
+ Example:
270
+
271
+ ```js
272
+ test('test: eslint: transform', async ({process}) => {
273
+ await process('operator-linebreak');
274
+ });
275
+
276
+ test('test: eslint: transform', async ({process}) => {
277
+ await process('operator-linebreak', {
278
+ rules: {
279
+ 'putout/putout': {
280
+ rules: {
281
+ 'convert-esm-to-commonjs': 'on',
282
+ },
283
+ },
284
+ },
285
+ });
286
+ });
287
+ ```
288
+
289
+ ### `noProcess(filename)`
290
+
291
+ Check that filename would not be processed.
292
+
293
+ Example:
294
+
295
+ ```js
296
+ test('test: eslint: noProcess', async ({noProcess}) => {
297
+ await noProcess('operator-linebreak-fix');
298
+ });
299
+ ```
300
+
301
+ ### `comparePlaces(filename, places)`
302
+
303
+ ```js
304
+ test('eslint-config: operator-line-break', async ({comparePlaces}) => {
305
+ await comparePlaces('operator-linebreak', [{
306
+ "message": "There should be no line break before or after '='.",
307
+ "position": {
308
+ "column": 1,
309
+ "line": 2,
310
+ },
311
+ "rule": "operator-linebreak (eslint)",
312
+ }]);
313
+ });
314
+ ```
315
+
258
316
  ## Processors API
259
317
 
260
318
  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
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@putout/test",
3
- "version": "4.2.0",
3
+ "version": "4.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 ",
@@ -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,
@@ -39,7 +40,8 @@
39
40
  "currify": "^4.0.0",
40
41
  "putout": "*",
41
42
  "supertape": "^6.0.0",
42
- "try-catch": "^3.0.0"
43
+ "try-catch": "^3.0.0",
44
+ "try-to-catch": "^3.0.0"
43
45
  },
44
46
  "keywords": [
45
47
  "putout",