@putout/test 3.7.5 → 4.2.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.
Files changed (4) hide show
  1. package/README.md +67 -35
  2. package/lib/test.js +48 -24
  3. package/lib/test.mjs +12 -0
  4. package/package.json +16 -12
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,7 +21,22 @@ UPDATE=1 tape test/*.js
23
21
 
24
22
  ## Plugins API
25
23
 
26
- ### report(filename, message | []messages)
24
+ All plugins 🐊`Putout` plugins written in `CommonJS`, since `ESLint` written on `CommonJS` and we have a huge `ESLint`-based ecosystem which 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
+
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)`
27
40
 
28
41
  checks error message (or messages) of a plugin
29
42
 
@@ -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
 
@@ -202,8 +231,11 @@ test('formatter: dump: many', (t) => {
202
231
  Here is example of tests for [remove-console](https://github.com/coderaiser/putout/tree/master/packages/plugin-remove-console):
203
232
 
204
233
  ```js
205
- const test = require('@putout/test')(__dirname, {
206
- 'remove-console': require('..'),
234
+ const {createTest} = require('@putout/test');
235
+ const removeConsole = require('@putout/plugin-remove-console');
236
+
237
+ const test = createTest(__dirname, {
238
+ 'remove-console': removeConsole,
207
239
  });
208
240
 
209
241
  test('remove-console: report', (t) => {
@@ -244,7 +276,7 @@ const test = createTest(__dirname, {
244
276
 
245
277
  ```
246
278
 
247
- ### process(filename [, plugins, ])
279
+ ### `process(filename [, plugins, ])`
248
280
 
249
281
  Example:
250
282
 
@@ -258,19 +290,19 @@ test('putout: processor: json', async ({process}) => {
258
290
  });
259
291
  ```
260
292
 
261
- ### noProcess(filename [, plugins, processors])
293
+ ### `noProcess(filename [, plugins, processors])`
262
294
 
263
295
  Check that filename would not be processed.
264
296
 
265
297
  Example:
266
298
 
267
299
  ```js
268
- test('putout: process: json: no process', async (t) => {
269
- await t.noProcess('eslintrc', [], ['json']);
300
+ test('putout: process: json: no process', async ({noProcess}) => {
301
+ await noProcess('eslintrc', [], ['json']);
270
302
  });
271
303
  ```
272
304
 
273
- ### comparePlaces(filename, places)
305
+ ### `comparePlaces(filename, places)`
274
306
 
275
307
  ```js
276
308
  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,44 +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({dir, plugins, rules}, t, formatter, names, options);
177
+ } = await runFormat(formatter, names, options);
157
178
 
158
179
  writeFileSync(outputName, result);
159
180
 
160
181
  return {is, output, result};
161
182
  });
162
183
 
163
- 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
+
164
190
  const full = join(dir, name);
165
191
  const outputName = `${full}-format.js`;
166
192
 
167
193
  if (!existsSync(outputName))
168
194
  writeFileSync(outputName, '');
169
195
 
196
+ const runFormat = format({
197
+ dir,
198
+ plugins,
199
+ rules,
200
+ }, t);
201
+
170
202
  const {
171
203
  is,
172
204
  output,
173
205
  result,
174
- } = format(
175
- {dir,
176
- plugins,
177
- rules},
178
- t,
179
- formatter,
180
- name,
181
- options,
182
- );
206
+ } = await runFormat(formatter, name, options);
183
207
 
184
208
  writeFileSync(outputName, result);
185
209
 
@@ -210,7 +234,7 @@ const transform = currify(({dir, plugins, rules}, t, name, transformed = null, a
210
234
  }],
211
235
  });
212
236
 
213
- if (UPDATE)
237
+ if (isUpdate())
214
238
  writeFileSync(`${full}-fix.js`, code);
215
239
 
216
240
  return t.equal(code, output);
@@ -230,7 +254,7 @@ const transformWithOptions = currify(({dir, plugins}, t, name, options) => {
230
254
 
231
255
  const {code} = putout(input, {isTS, plugins, rules});
232
256
 
233
- if (UPDATE)
257
+ if (isUpdate())
234
258
  writeFileSync(`${full}-fix.js`, code);
235
259
 
236
260
  return t.equal(code, output);
package/lib/test.mjs ADDED
@@ -0,0 +1,12 @@
1
+ import {dirname} from 'path';
2
+ import {fileURLToPath} from 'url';
3
+ import create from './test.js';
4
+
5
+ export default create;
6
+
7
+ export const createTest = (url, plugins) => {
8
+ const __filename = fileURLToPath(url);
9
+ const __dirname = dirname(__filename);
10
+
11
+ return create(__dirname, plugins);
12
+ };
package/package.json CHANGED
@@ -1,6 +1,7 @@
1
1
  {
2
2
  "name": "@putout/test",
3
- "version": "3.7.5",
3
+ "version": "4.2.0",
4
+ "type": "commonjs",
4
5
  "author": "coderaiser <mnemonic.enemy@gmail.com> (https://github.com/coderaiser)",
5
6
  "description": "test runner for putout plugins ",
6
7
  "homepage": "https://github.com/coderaiser/putout/tree/master/packages/test#readme",
@@ -9,8 +10,12 @@
9
10
  "tape": "bin/test.mjs"
10
11
  },
11
12
  "exports": {
12
- ".": "./lib/test.js",
13
- "./processor": "./lib/processor/index.js"
13
+ ".": {
14
+ "require": "./lib/test.js",
15
+ "import": "./lib/test.mjs"
16
+ },
17
+ "./processor": "./lib/processor/index.js",
18
+ "./formatter": "./lib/formatter/index.mjs"
14
19
  },
15
20
  "release": false,
16
21
  "tag": false,
@@ -44,18 +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-extract-object-properties": "^6.0.1",
51
- "@putout/plugin-putout": "^7.2.0",
52
- "@putout/plugin-remove-console": "^3.1.0",
53
- "@putout/plugin-remove-empty": "^6.0.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": "*",
54
58
  "@putout/plugin-remove-unused-variables": "*",
55
59
  "c8": "^7.5.0",
56
- "eslint": "^8.0.0-beta.0",
60
+ "eslint": "^8.0.1",
57
61
  "eslint-plugin-node": "^11.0.0",
58
- "eslint-plugin-putout": "^10.0.0",
62
+ "eslint-plugin-putout": "^13.0.0",
59
63
  "lerna": "^4.0.0",
60
64
  "madrun": "^8.0.1",
61
65
  "mock-require": "^3.0.3",