@putout/test 3.7.4 → 4.1.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
@@ -1,11 +1,9 @@
1
- # @putout/test [![NPM version][NPMIMGURL]][NPMURL] [![Dependency Status][DependencyStatusIMGURL]][DependencyStatusURL]
1
+ # @putout/test [![NPM version][NPMIMGURL]][NPMURL]
2
2
 
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
- [DependencyStatusURL]: https://david-dm.org/coderaiser/putout?path=packages/test-runner
6
- [DependencyStatusIMGURL]: https://david-dm.org/coderaiser/putout.svg?path=packages/test-runner
7
5
 
8
- Test runner for [`putout plugins`](https://github.com/coderaiser/putout#plugins-api). Basically it is [supercharged `tape`](https://github.com/coderaiser/supertape) with aditional asseritions:
6
+ Test runner for 🐊[`Putout`](https://github.com/coderaiser/putout#plugins-api). Basically it is [supercharged `tape`](https://github.com/coderaiser/supertape) with aditional asseritions:
9
7
 
10
8
  ## Install
11
9
 
@@ -23,9 +21,24 @@ UPDATE=1 tape test/*.js
23
21
 
24
22
  ## Plugins API
25
23
 
26
- ### report(filename, message)
24
+ All plugins 🐊`Putout` plugins written in `CommonJS`, since `ESLint` written on `CommonJS` and we have a huge `ESLint`-based ecosystem wich is good to reuse.
25
+ `🐊`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
+ When [async rules will be supported](https://github.com/eslint/eslint/issues/15394) we can switch to `ESM`.
27
27
 
28
- checks error message of a plugin
28
+ To write test for your plugins you need initialize `test` using `createTest`:
29
+
30
+ ```js
31
+ import {createTest} from '@putout/test';
32
+ const rmVars = require('@putout/plugin-remove-unused-variables');
33
+
34
+ const test = createTest(import.meta.url, {
35
+ 'remove-unused-variables': rmVars,
36
+ });
37
+ ```
38
+
39
+ ### `report(filename, message | []messages)`
40
+
41
+ checks error message (or messages) of a plugin
29
42
 
30
43
  ```js
31
44
  test('remove usless variables: for-of', (t) => {
@@ -34,7 +47,7 @@ test('remove usless variables: for-of', (t) => {
34
47
  });
35
48
  ```
36
49
 
37
- ### reportCode(input, message)
50
+ ### `reportCode(input, message)`
38
51
 
39
52
  checks error message of a plugin from `input` code
40
53
 
@@ -45,7 +58,7 @@ test('remove debugger: report', (t) => {
45
58
  });
46
59
  ```
47
60
 
48
- ### transform(filename [, output, plugins])
61
+ ### `transform(filename [, output, plugins])`
49
62
 
50
63
  check transform of `filename.js` -> `filename-fix.js` in `test/fixtures` directory
51
64
 
@@ -58,7 +71,7 @@ test('remove usless variables: for-of', (t) => {
58
71
  });
59
72
  ```
60
73
 
61
- ### transformCode(input, output)
74
+ ### `transformCode(input, output)`
62
75
 
63
76
  check transform of `input` -> `output` code
64
77
 
@@ -69,7 +82,7 @@ test('remove-console: property identifier: code', (t) => {
69
82
  });
70
83
  ```
71
84
 
72
- ### reportWithOptions(filename, options)
85
+ ### `reportWithOptions(filename, options)`
73
86
 
74
87
  check report of `filename.js` with `options`
75
88
 
@@ -85,7 +98,7 @@ test('putout: test: reportWithOptions', (t) => {
85
98
  });
86
99
  ```
87
100
 
88
- ### noReportWithOptions(filename, options)
101
+ ### `noReportWithOptions(filename, options)`
89
102
 
90
103
  check no report of `filename.js` with `options`
91
104
 
@@ -100,7 +113,7 @@ test('putout: test: noReportWithOptions', (t) => {
100
113
  });
101
114
  ```
102
115
 
103
- ### transformWithOptions(filename, options)
116
+ ### `transformWithOptions(filename, options)`
104
117
 
105
118
  check transform of `filename.js` with `options`
106
119
 
@@ -109,12 +122,11 @@ test('putout: plugin: declare-undefined-variables: transform: parse', (t) => {
109
122
  t.transformWithOptions('parse', {
110
123
  dismiss: ['assign', 'stringify'],
111
124
  });
112
-
113
125
  t.end();
114
126
  });
115
127
  ```
116
128
 
117
- ### noTransformWithOptions(filename, options)
129
+ ### `noTransformWithOptions(filename, options)`
118
130
 
119
131
  When file should not be transformed:
120
132
 
@@ -125,7 +137,7 @@ test('test: declared', (t) => {
125
137
  });
126
138
  ```
127
139
 
128
- ### noTransformWithOptions(filename, options)
140
+ ### `noTransformWithOptions(filename, options)`
129
141
 
130
142
  check transform of `filename.js` with `options`
131
143
 
@@ -138,7 +150,7 @@ test('putout: plugin: declare-undefined-variables: transform: assign: dismiss',
138
150
  });
139
151
  ```
140
152
 
141
- ### noReport(filename)
153
+ ### `noReport(filename)`
142
154
 
143
155
  checks error message of a plugin not produces
144
156
 
@@ -149,9 +161,9 @@ test('plugin-putout: check-replace-code: no report: typescript', (t) => {
149
161
  });
150
162
  ```
151
163
 
152
- ### noReportAfterTransform(filename)
164
+ ### `noReportAfterTransform(filename)`
153
165
 
154
- checks error message of a plugin not produces
166
+ checks error message of a plugin not produced
155
167
 
156
168
  ```js
157
169
  test('test: no report after transform', (t) => {
@@ -160,9 +172,9 @@ test('test: no report after transform', (t) => {
160
172
  });
161
173
  ```
162
174
 
163
- ### noTransform(filename)
175
+ ### `noTransform(filename)`
164
176
 
165
- check transform of `filename.js` produce nothing new
177
+ check transform of `filename.js` produce nothing
166
178
 
167
179
  ```js
168
180
  test('plugin-apply-numeric-separators: no transform: hex', (t) => {
@@ -171,29 +183,46 @@ test('plugin-apply-numeric-separators: no transform: hex', (t) => {
171
183
  });
172
184
  ```
173
185
 
174
- ### format(formatter, filename)
186
+ ## Formatters API
187
+
188
+ First you need to create test with:
189
+
190
+ ```js
191
+ import {createTest} from '@putout/test';
192
+ import rmVars from '@putout/plugin-remove-unused-variables';
193
+
194
+ const test = createTest(import.meta.url, {
195
+ 'remove-unused-variables': rmVars,
196
+ });
197
+ ```
198
+
199
+ ### `format(formatter, filename)`
175
200
 
176
201
  check file name formatting (pass `process.env.UPDATE=1` to save fixture)
177
202
 
178
- ### noFormat
203
+ ```js
204
+ test('formatter: codeframe', async ({format}) => {
205
+ await format(codeframe);
206
+ });
207
+ ```
208
+
209
+ ### `noFormat`
179
210
 
180
211
  check that there is no formatting for for such file
181
212
 
182
213
  ```js
183
- test('formatter: codeframe: no', (t) => {
184
- t.noFormat(codeframe, 'no');
185
- t.end();
214
+ test('formatter: codeframe: no', async ({noFormat}) => {
215
+ await noFormat(codeframe, 'no');
186
216
  });
187
217
  ```
188
218
 
189
- ### formatMany(formatter, [filename1, filename2])
219
+ ### `formatMany(formatter, [filename1, filename2])`
190
220
 
191
221
  check file name formatting (pass `process.env.UPDATE=1` to save fixture)
192
222
 
193
223
  ```js
194
- test('formatter: dump: many', (t) => {
195
- t.formatMany(dump, ['var', 'var']);
196
- t.end();
224
+ test('formatter: dump: many', async ({formatMany}) => {
225
+ await formatMany(dump, ['var', 'var']);
197
226
  });
198
227
  ```
199
228
 
@@ -244,7 +273,7 @@ const test = createTest(__dirname, {
244
273
 
245
274
  ```
246
275
 
247
- ### process(filename [, plugins, ])
276
+ ### `process(filename [, plugins, ])`
248
277
 
249
278
  Example:
250
279
 
@@ -258,19 +287,19 @@ test('putout: processor: json', async ({process}) => {
258
287
  });
259
288
  ```
260
289
 
261
- ### noProcess(filename [, plugins, processors])
290
+ ### `noProcess(filename [, plugins, processors])`
262
291
 
263
292
  Check that filename would not be processed.
264
293
 
265
294
  Example:
266
295
 
267
296
  ```js
268
- test('putout: process: json: no process', async (t) => {
269
- await t.noProcess('eslintrc', [], ['json']);
297
+ test('putout: process: json: no process', async ({noProcess}) => {
298
+ await noProcess('eslintrc', [], ['json']);
270
299
  });
271
300
  ```
272
301
 
273
- ### comparePlaces(filename, places)
302
+ ### `comparePlaces(filename, places)`
274
303
 
275
304
  ```js
276
305
  test('putout: processor: css: places', async ({comparePlaces}) => {
package/lib/test.js CHANGED
@@ -19,6 +19,15 @@ const {isArray} = Array;
19
19
  const {keys, entries} = Object;
20
20
 
21
21
  const {UPDATE} = process.env;
22
+ global.__putout_test_update = UPDATE;
23
+ global.__putout_test_fs = {
24
+ readFileSync,
25
+ writeFileSync,
26
+ existsSync,
27
+ };
28
+
29
+ const isUpdate = () => UPDATE || global.__putout_test_update;
30
+
22
31
  const TS = {
23
32
  ENABLED: true,
24
33
  DISABLED: false,
@@ -33,7 +42,12 @@ const readFixture = (name) => {
33
42
  return [readFileSync(`${name}.js`, 'utf8'), TS.DISABLED];
34
43
  };
35
44
 
36
- module.exports = (dir, plugin, rules) => {
45
+ module.exports = createTest;
46
+ module.exports.createTest = createTest;
47
+
48
+ function createTest(dir, plugin, rules) {
49
+ const update = isUpdate();
50
+
37
51
  dir = join(dir, 'fixture');
38
52
  const plugins = getPlugins(plugin);
39
53
 
@@ -59,14 +73,14 @@ module.exports = (dir, plugin, rules) => {
59
73
  }),
60
74
 
61
75
  formatSave: formatSave({dir, plugins, rules}),
62
- format: (UPDATE ? formatSave : format)({dir, plugins, rules}),
76
+ format: (update ? formatSave : format)({dir, plugins, rules}),
63
77
  formatManySave: formatManySave({dir, plugins, rules}),
64
- formatMany: (UPDATE ? formatManySave : formatMany)({dir, plugins, rules}),
78
+ formatMany: (update ? formatManySave : formatMany)({dir, plugins, rules}),
65
79
  noFormat: noFormat({dir, plugins, rules}),
66
80
  });
67
- };
81
+ }
68
82
 
69
- const format = currify(({dir, plugins, rules}, t, formatter, name, formatterOptions = {}) => {
83
+ const format = currify(({dir, plugins, rules}, t) => async (formatter, name, formatterOptions = {}) => {
70
84
  const full = join(dir, name);
71
85
  const outputName = `${full}-format`;
72
86
  const [input, isTS] = readFixture(full);
@@ -75,7 +89,7 @@ const format = currify(({dir, plugins, rules}, t, formatter, name, formatterOpti
75
89
  const {places} = putout(input, {fixCount: 1, isTS, plugins, rules});
76
90
 
77
91
  const report = putout.initReport();
78
- const result = report(formatter, {
92
+ const result = await report(formatter, {
79
93
  formatterOptions,
80
94
  name,
81
95
  source: input,
@@ -87,14 +101,14 @@ const format = currify(({dir, plugins, rules}, t, formatter, name, formatterOpti
87
101
  return {is, output, result};
88
102
  });
89
103
 
90
- const noFormat = currify(({dir, plugins, rules}, t, formatter, name, formatterOptions = {}) => {
104
+ const noFormat = currify(({dir, plugins, rules}, t) => async (formatter, name, formatterOptions = {}) => {
91
105
  const full = join(dir, name);
92
106
  const [input] = readFixture(full);
93
107
 
94
108
  const {places} = putout(input, {plugins, rules});
95
109
 
96
110
  const report = putout.initReport();
97
- const result = report(formatter, {
111
+ const result = await report(formatter, {
98
112
  name,
99
113
  places,
100
114
  formatterOptions,
@@ -105,7 +119,7 @@ const noFormat = currify(({dir, plugins, rules}, t, formatter, name, formatterOp
105
119
  return {is, output, result};
106
120
  });
107
121
 
108
- const formatMany = currify(({dir, plugins, rules}, t, formatter, names, formatterOptions = {}) => {
122
+ const formatMany = currify(({dir, plugins, rules}, t) => async (formatter, names, formatterOptions = {}) => {
109
123
  const joinTwo = (a) => (b) => join(a, b);
110
124
  const fullNames = names.map(joinTwo(dir));
111
125
 
@@ -124,7 +138,7 @@ const formatMany = currify(({dir, plugins, rules}, t, formatter, names, formatte
124
138
  rules,
125
139
  });
126
140
 
127
- result += report(formatter, {
141
+ result += await report(formatter, {
128
142
  name,
129
143
  formatterOptions,
130
144
  source: input,
@@ -142,52 +156,54 @@ const formatMany = currify(({dir, plugins, rules}, t, formatter, names, formatte
142
156
  return {is, output, result};
143
157
  });
144
158
 
145
- const formatManySave = currify(({dir, plugins, rules}, t, formatter, names, options = {}) => {
159
+ const formatManySave = currify(({dir, plugins, rules}, t) => async (formatter, names, options = {}) => {
160
+ const {
161
+ existsSync,
162
+ writeFileSync,
163
+ } = global.__putout_test_fs;
164
+
146
165
  const name = `${names.join('-')}-format.js`;
147
166
  const outputName = join(dir, name);
148
167
 
149
168
  if (!existsSync(outputName))
150
169
  writeFileSync(outputName, '');
151
170
 
171
+ const runFormat = await formatMany({dir, plugins, rules}, t);
172
+
152
173
  const {
153
174
  is,
154
175
  output,
155
176
  result,
156
- } = formatMany(
157
- {dir,
158
- plugins,
159
- rules},
160
- t,
161
- formatter,
162
- names,
163
- options,
164
- );
177
+ } = await runFormat(formatter, names, options);
165
178
 
166
179
  writeFileSync(outputName, result);
167
180
 
168
181
  return {is, output, result};
169
182
  });
170
183
 
171
- const formatSave = currify(({dir, plugins, rules}, t, formatter, name, options = {}) => {
184
+ const formatSave = currify(({dir, plugins, rules}, t) => async (formatter, name, options = {}) => {
185
+ const {
186
+ existsSync,
187
+ writeFileSync,
188
+ } = global.__putout_test_fs;
189
+
172
190
  const full = join(dir, name);
173
191
  const outputName = `${full}-format.js`;
174
192
 
175
193
  if (!existsSync(outputName))
176
194
  writeFileSync(outputName, '');
177
195
 
196
+ const runFormat = format({
197
+ dir,
198
+ plugins,
199
+ rules,
200
+ }, t);
201
+
178
202
  const {
179
203
  is,
180
204
  output,
181
205
  result,
182
- } = format(
183
- {dir,
184
- plugins,
185
- rules},
186
- t,
187
- formatter,
188
- name,
189
- options,
190
- );
206
+ } = await runFormat(formatter, name, options);
191
207
 
192
208
  writeFileSync(outputName, result);
193
209
 
@@ -209,17 +225,19 @@ const transform = currify(({dir, plugins, rules}, t, name, transformed = null, a
209
225
 
210
226
  addons = addons || {};
211
227
 
212
- plugins[0] = {
213
- ...plugins[0],
214
- ...addons,
215
- };
216
-
217
- const {code} = putout(input, {isTS, plugins, rules});
228
+ const {code} = putout(input, {
229
+ isTS,
230
+ rules,
231
+ plugins: [{
232
+ ...plugins[0],
233
+ ...addons,
234
+ }],
235
+ });
218
236
 
219
- if (UPDATE)
237
+ if (isUpdate())
220
238
  writeFileSync(`${full}-fix.js`, code);
221
239
 
222
- return t.equal(code, output, 'should equal');
240
+ return t.equal(code, output);
223
241
  });
224
242
 
225
243
  const transformWithOptions = currify(({dir, plugins}, t, name, options) => {
@@ -236,10 +254,10 @@ const transformWithOptions = currify(({dir, plugins}, t, name, options) => {
236
254
 
237
255
  const {code} = putout(input, {isTS, plugins, rules});
238
256
 
239
- if (UPDATE)
257
+ if (isUpdate())
240
258
  writeFileSync(`${full}-fix.js`, code);
241
259
 
242
- return t.equal(code, output, 'should equal');
260
+ return t.equal(code, output);
243
261
  });
244
262
 
245
263
  const noTransformWithOptions = currify(({dir, plugins}, t, name, options) => {
@@ -255,7 +273,7 @@ const noTransformWithOptions = currify(({dir, plugins}, t, name, options) => {
255
273
 
256
274
  const {code} = putout(input, {isTS, plugins, rules});
257
275
 
258
- return t.equal(code, input, 'should equal');
276
+ return t.equal(code, input);
259
277
  });
260
278
 
261
279
  const noTransform = currify(({dir, plugins, rules}, t, name, addons = {}) => {
@@ -267,12 +285,12 @@ const noTransform = currify(({dir, plugins, rules}, t, name, addons = {}) => {
267
285
 
268
286
  const transformCode = currify(({plugins, rules}, t, input, output, isTS = false) => {
269
287
  const {code} = putout(input, {isTS, plugins, rules});
270
- return t.equal(code, output, 'should equal');
288
+ return t.equal(code, output);
271
289
  });
272
290
 
273
291
  const noTransformCode = currify(({plugins, rules}, t, input) => {
274
292
  const {code} = putout(input, {plugins, rules});
275
- return t.equal(code, input, 'should equal');
293
+ return t.equal(code, input);
276
294
  });
277
295
 
278
296
  const getMessage = ({message}) => message;
@@ -342,7 +360,7 @@ const reportCode = currify(({plugins, rules, isTS}, t, source, message) => {
342
360
  if (isArray(message))
343
361
  return t.deepEqual(resultMessages, message, 'should equal');
344
362
 
345
- return t.equal(resultMessages[0], message, 'should equal');
363
+ return t.equal(resultMessages[0], message);
346
364
  });
347
365
 
348
366
  const noReportCode = currify(({plugins, rules, isTS}, t, source) => {
package/lib/test.mjs ADDED
@@ -0,0 +1,9 @@
1
+ import {createCommons} from 'simport';
2
+ import create from './test.js';
3
+
4
+ export default create;
5
+
6
+ export const createTest = (url, plugins) => {
7
+ const {__dirname} = createCommons(url);
8
+ return create(__dirname, plugins);
9
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@putout/test",
3
- "version": "3.7.4",
3
+ "version": "4.1.0",
4
4
  "author": "coderaiser <mnemonic.enemy@gmail.com> (https://github.com/coderaiser)",
5
5
  "description": "test runner for putout plugins ",
6
6
  "homepage": "https://github.com/coderaiser/putout/tree/master/packages/test#readme",
@@ -9,8 +9,12 @@
9
9
  "tape": "bin/test.mjs"
10
10
  },
11
11
  "exports": {
12
- ".": "./lib/test.js",
13
- "./processor": "./lib/processor/index.js"
12
+ ".": {
13
+ "require": "./lib/test.js",
14
+ "import": "./lib/test.mjs"
15
+ },
16
+ "./processor": "./lib/processor/index.js",
17
+ "./formatter": "./lib/formatter/index.mjs"
14
18
  },
15
19
  "release": false,
16
20
  "tag": false,
@@ -33,6 +37,7 @@
33
37
  "@putout/engine-processor": "*",
34
38
  "currify": "^4.0.0",
35
39
  "putout": "*",
40
+ "simport": "^1.2.0",
36
41
  "supertape": "^6.0.0",
37
42
  "try-catch": "^3.0.0"
38
43
  },
@@ -44,16 +49,17 @@
44
49
  "runner"
45
50
  ],
46
51
  "devDependencies": {
47
- "@cloudcmd/stub": "^3.0.0",
48
- "@putout/formatter-dump": "^2.0.1",
49
- "@putout/formatter-progress": "^2.0.0",
50
- "@putout/plugin-remove-console": "^3.1.0",
51
- "@putout/plugin-remove-empty": "^5.1.0",
52
+ "@putout/formatter-dump": "*",
53
+ "@putout/formatter-progress": "*",
54
+ "@putout/plugin-extract-object-properties": "*",
55
+ "@putout/plugin-putout": "*",
56
+ "@putout/plugin-remove-console": "*",
57
+ "@putout/plugin-remove-empty": "*",
52
58
  "@putout/plugin-remove-unused-variables": "*",
53
59
  "c8": "^7.5.0",
54
- "eslint": "^8.0.0-beta.0",
60
+ "eslint": "^8.0.1",
55
61
  "eslint-plugin-node": "^11.0.0",
56
- "eslint-plugin-putout": "^9.0.0",
62
+ "eslint-plugin-putout": "^12.0.0",
57
63
  "lerna": "^4.0.0",
58
64
  "madrun": "^8.0.1",
59
65
  "mock-require": "^3.0.3",