@putout/test 13.0.1 → 13.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 (3) hide show
  1. package/README.md +23 -0
  2. package/lib/test.js +103 -61
  3. package/package.json +2 -1
package/README.md CHANGED
@@ -58,6 +58,18 @@ const test = createTest(import.meta.url, {
58
58
  });
59
59
  ```
60
60
 
61
+ You can also pass extensions to read from `fixture` directory:
62
+
63
+ ```js
64
+ const test = createTest(import.meta.url, {
65
+ ext: '.wast',
66
+ lint: putout,
67
+ plugins: [
68
+ ['remove-unused-variables', rmVars],
69
+ ],
70
+ });
71
+ ```
72
+
61
73
  ### `report(filename, message: string | string[], plugins?: PutoutPlugin[])`
62
74
 
63
75
  Check error message (or messages) of a plugin:
@@ -200,6 +212,17 @@ test('plugin-putout: check-replace-code: no report: typescript', (t) => {
200
212
  });
201
213
  ```
202
214
 
215
+ ### `noReportCode(filename)`
216
+
217
+ Check error message of a plugin not produces
218
+
219
+ ```js
220
+ test('plugin-putout: check-replace-code: no report: import', (t) => {
221
+ t.noReportCode(`import a from 'a'`);
222
+ t.end();
223
+ });
224
+ ```
225
+
203
226
  ### `noReportAfterTransform(filename)`
204
227
 
205
228
  Check error message of a plugin not produced
package/lib/test.js CHANGED
@@ -19,7 +19,7 @@ const {createProgress} = require('@putout/engine-runner/progress');
19
19
 
20
20
  const {createError} = require('./create-error');
21
21
  const {preTest} = require('./pre-test');
22
-
22
+ const maybeArray = (a) => isArray(a) ? a : [a];
23
23
  const {isArray} = Array;
24
24
  const isString = (a) => typeof a === 'string';
25
25
  const isObject = (a) => typeof a === 'object';
@@ -48,37 +48,51 @@ const TS = {
48
48
  DISABLED: false,
49
49
  };
50
50
 
51
- const readFixture = (name) => {
51
+ const readFixture = (name, ext) => {
52
52
  const {readFileSync} = global.__putout_test_fs;
53
- const [e, data] = tryCatch(readFileSync, `${name}.ts`, 'utf8');
53
+ const [eTS, dataTS] = tryCatch(readFileSync, `${name}.ts`, 'utf8');
54
54
 
55
- if (!e)
55
+ if (!eTS)
56
56
  return [
57
- data,
57
+ dataTS,
58
58
  TS.ENABLED,
59
+ '.ts',
60
+ ];
61
+
62
+ const [eJS, dataJS] = tryCatch(readFileSync, `${name}.js`, 'utf8');
63
+
64
+ if (!eJS)
65
+ return [
66
+ dataJS,
67
+ TS.DISABLED,
68
+ '.js',
59
69
  ];
60
70
 
61
- return [
62
- readFileSync(`${name}.js`, 'utf8'),
63
- TS.DISABLED,
64
- ];
71
+ for (const currentExt of maybeArray(ext)) {
72
+ const [e, data] = tryCatch(readFileSync, `${name}${currentExt}`, 'utf8');
73
+
74
+ if (!e)
75
+ return [
76
+ data,
77
+ TS.DISABLED,
78
+ currentExt,
79
+ ];
80
+ }
81
+
82
+ throw eJS;
65
83
  };
66
84
 
67
- const writeFixture = ({full, code, isTS}) => {
85
+ const writeFixture = ({full, code, currentExt}) => {
68
86
  writeSourceFixture({
69
87
  full: `${full}-fix`,
70
88
  code,
71
- isTS,
89
+ currentExt,
72
90
  });
73
91
  };
74
92
 
75
- const writeSourceFixture = ({full, code, isTS}) => {
93
+ const writeSourceFixture = ({full, code, currentExt}) => {
76
94
  const {writeFileSync} = global.__putout_test_fs;
77
-
78
- if (!isTS)
79
- return writeFileSync(`${full}.js`, code);
80
-
81
- writeFileSync(`${full}.ts`, code);
95
+ writeFileSync(`${full}${currentExt}`, code);
82
96
  };
83
97
 
84
98
  const rmFixture = (name) => {
@@ -104,31 +118,41 @@ const parsePlugin = (plugins) => {
104
118
  function createTest(dir, maybeOptions) {
105
119
  dir = join(dir, 'fixture');
106
120
 
107
- const {lint = putout, ...options} = parseOptions(maybeOptions);
121
+ const {
122
+ ext = [],
123
+ lint = putout,
124
+ ...options
125
+ } = parseOptions(maybeOptions);
108
126
 
109
127
  const plugin = parsePlugin(options.plugins);
110
128
 
129
+ const linterOptions = {
130
+ lint,
131
+ ext,
132
+ };
133
+
111
134
  preTest(test, plugin);
112
135
 
113
136
  return test.extend({
114
- transform: transform(dir, lint, options),
115
- noTransform: noTransform(dir, lint, options),
116
- transformCode: transformCode(lint, options),
117
- noTransformCode: noTransformCode(lint, options),
137
+ transform: transform(dir, linterOptions, options),
138
+ noTransform: noTransform(dir, linterOptions, options),
139
+ transformCode: transformCode(linterOptions, options),
140
+ noTransformCode: noTransformCode(linterOptions, options),
118
141
 
119
142
  progress: progress(dir, options),
120
143
  progressWithOptions: progressWithOptions(dir, options),
121
144
 
122
- transformWithOptions: transformWithOptions(dir, lint, options),
123
- noTransformWithOptions: noTransformWithOptions(dir, lint, options),
145
+ transformWithOptions: transformWithOptions(dir, linterOptions, options),
146
+ noTransformWithOptions: noTransformWithOptions(dir, linterOptions, options),
124
147
 
125
- report: report(dir, lint, options),
126
- noReport: noReport(dir, lint, options),
127
- noReportAfterTransform: noReportAfterTransform(dir, lint, options),
128
- noReportAfterTransformWithOptions: noReportAfterTransformWithOptions(dir, lint, options),
129
- reportWithOptions: reportWithOptions(dir, lint, options),
130
- noReportWithOptions: noReportWithOptions(dir, lint, options),
148
+ report: report(dir, linterOptions, options),
149
+ noReport: noReport(dir, linterOptions, options),
150
+ noReportAfterTransform: noReportAfterTransform(dir, linterOptions, options),
151
+ noReportAfterTransformWithOptions: noReportAfterTransformWithOptions(dir, linterOptions, options),
152
+ reportWithOptions: reportWithOptions(dir, linterOptions, options),
153
+ noReportWithOptions: noReportWithOptions(dir, linterOptions, options),
131
154
  reportCode: reportCode(lint, options),
155
+ noReportCode: noReportCode(lint, options),
132
156
  format: formatSave(dir, options),
133
157
  formatMany: formatManySave(dir, options),
134
158
  noFormat: noFormat(dir, options),
@@ -328,12 +352,12 @@ const progressWithOptions = (dir, options) => (t) => async (name, pluginOptions,
328
352
  return t.deepEqual(result, expected);
329
353
  };
330
354
 
331
- const transform = currify((dir, lint, options, t, name, transformed = null, addons = {}) => {
355
+ const transform = currify((dir, linterOptions, options, t, name, transformed = null, addons = {}) => {
356
+ const {lint, ext} = linterOptions;
332
357
  const {plugins} = options;
333
358
  const full = join(dir, name);
334
359
  const isStr = isString(transformed);
335
-
336
- const [input, isTS] = readFixture(full);
360
+ const [input, isTS, currentExt] = readFixture(full, ext);
337
361
 
338
362
  if (!isStr)
339
363
  addons = transformed;
@@ -356,20 +380,22 @@ const transform = currify((dir, lint, options, t, name, transformed = null, addo
356
380
  writeFixture({
357
381
  full,
358
382
  code,
359
- isTS,
383
+
384
+ currentExt,
360
385
  });
361
386
  return t.pass('fixed fixture updated');
362
387
  }
363
388
 
364
- const [output] = isStr ? [transformed] : readFixture(`${full}-fix`);
389
+ const [output] = isStr ? [transformed] : readFixture(`${full}-fix`, ext);
365
390
 
366
391
  return t.equal(code, output);
367
392
  });
368
393
 
369
- const transformWithOptions = currify((dir, lint, options, t, name, pluginOptions) => {
370
- const {writeFileSync} = global.__putout_test_fs;
394
+ const transformWithOptions = currify((dir, linterOptions, options, t, name, pluginOptions) => {
395
+ const {lint, ext} = linterOptions;
396
+
371
397
  const full = join(dir, name);
372
- const [input, isTS] = readFixture(full);
398
+ const [input, isTS, currentExt] = readFixture(full, ext);
373
399
 
374
400
  const rule = parseRule(options);
375
401
 
@@ -384,11 +410,15 @@ const transformWithOptions = currify((dir, lint, options, t, name, pluginOptions
384
410
  });
385
411
 
386
412
  if (isUpdate()) {
387
- writeFileSync(`${full}-fix.js`, code);
413
+ writeFixture({
414
+ full,
415
+ code,
416
+ currentExt,
417
+ });
388
418
  return t.pass('fixed fixture updated');
389
419
  }
390
420
 
391
- const [output] = readFixture(`${full}-fix`);
421
+ const [output] = readFixture(`${full}-fix`, ext);
392
422
 
393
423
  return t.equal(code, output);
394
424
  });
@@ -399,9 +429,10 @@ const parseRule = ({plugins}) => {
399
429
  return plugin[0] || keys(plugin)[0];
400
430
  };
401
431
 
402
- const noTransformWithOptions = currify((dir, lint, options, t, name, ruleOptions) => {
432
+ const noTransformWithOptions = currify((dir, linterOptions, options, t, name, ruleOptions) => {
433
+ const {lint, ext} = linterOptions;
403
434
  const full = join(dir, name);
404
- const [input, isTS] = readFixture(full);
435
+ const [input, isTS, currentExt] = readFixture(full, ext);
405
436
 
406
437
  rmFixture(`${full}-fix`);
407
438
 
@@ -420,7 +451,8 @@ const noTransformWithOptions = currify((dir, lint, options, t, name, ruleOptions
420
451
  writeSourceFixture({
421
452
  full,
422
453
  code,
423
- isTS,
454
+
455
+ currentExt,
424
456
  });
425
457
 
426
458
  return t.pass('source fixture updated');
@@ -429,14 +461,15 @@ const noTransformWithOptions = currify((dir, lint, options, t, name, ruleOptions
429
461
  return t.equal(code, input);
430
462
  });
431
463
 
432
- const noTransform = currify((dir, lint, options, t, name, addons = {}) => {
464
+ const noTransform = currify((dir, linterOptions, options, t, name, addons = {}) => {
465
+ const {lint, ext} = linterOptions;
433
466
  const full = join(dir, name);
434
- const [fixture] = readFixture(full);
467
+ const [fixture] = readFixture(full, ext);
435
468
 
436
469
  rmFixture(`${full}-fix`);
437
470
 
438
471
  const {plugins} = options;
439
- const [input, isTS] = readFixture(full);
472
+ const [input, isTS, currentExt] = readFixture(full, ext);
440
473
 
441
474
  const {code} = lint(input, {
442
475
  isTS,
@@ -451,7 +484,7 @@ const noTransform = currify((dir, lint, options, t, name, addons = {}) => {
451
484
  writeSourceFixture({
452
485
  full,
453
486
  code,
454
- isTS,
487
+ currentExt,
455
488
  });
456
489
 
457
490
  return t.pass('source fixture updated');
@@ -460,7 +493,8 @@ const noTransform = currify((dir, lint, options, t, name, addons = {}) => {
460
493
  return t.equal(code, fixture);
461
494
  });
462
495
 
463
- const transformCode = currify((lint, options, t, input, output, isTS = false) => {
496
+ const transformCode = currify((linterOptions, options, t, input, output, isTS = false) => {
497
+ const {lint} = linterOptions;
464
498
  const {code} = lint(input, {
465
499
  isTS,
466
500
  ...options,
@@ -469,18 +503,21 @@ const transformCode = currify((lint, options, t, input, output, isTS = false) =>
469
503
  return t.equal(code, output);
470
504
  });
471
505
 
472
- const noTransformCode = currify((lint, options, t, input) => {
506
+ const noTransformCode = currify((linterOptions, options, t, input) => {
507
+ const {lint} = linterOptions;
473
508
  const {code} = lint(input, options);
509
+
474
510
  return t.equal(code, input);
475
511
  });
476
512
 
477
513
  const getMessage = ({message}) => message;
478
514
 
479
- const report = (dir, lint, options) => (t) => (name, message, plugins) => {
515
+ const report = (dir, linterOptions, options) => (t) => (name, message, plugins) => {
516
+ const {lint, ext} = linterOptions;
480
517
  checkReport(name, message);
481
518
 
482
519
  const full = join(dir, name);
483
- const [source, isTS] = readFixture(full);
520
+ const [source, isTS] = readFixture(full, ext);
484
521
 
485
522
  const addT = reportCode(lint, {
486
523
  isTS,
@@ -492,9 +529,10 @@ const report = (dir, lint, options) => (t) => (name, message, plugins) => {
492
529
  return run(source, message, plugins);
493
530
  };
494
531
 
495
- const noReport = currify((dir, lint, options, t, name) => {
532
+ const noReport = currify((dir, linterOptions, options, t, name) => {
533
+ const {lint, ext} = linterOptions;
496
534
  const full = join(dir, name);
497
- const [source, isTS] = readFixture(full);
535
+ const [source, isTS] = readFixture(full, ext);
498
536
 
499
537
  rmFixture(`${full}-fix`);
500
538
 
@@ -506,9 +544,10 @@ const noReport = currify((dir, lint, options, t, name) => {
506
544
 
507
545
  module.exports._createNoReport = noReport;
508
546
 
509
- const noReportAfterTransform = currify((dir, lint, options, t, name, addons = {}) => {
547
+ const noReportAfterTransform = currify((dir, linterOptions, options, t, name, addons = {}) => {
548
+ const {lint, ext} = linterOptions;
510
549
  const full = join(dir, name);
511
- const [source, isTS] = readFixture(full);
550
+ const [source, isTS] = readFixture(full, ext);
512
551
 
513
552
  return noReportCodeAfterTransform(lint, {
514
553
  isTS,
@@ -518,9 +557,10 @@ const noReportAfterTransform = currify((dir, lint, options, t, name, addons = {}
518
557
 
519
558
  module.exports._createNoReportAfterTransform = noReportAfterTransform;
520
559
 
521
- const noReportAfterTransformWithOptions = currify((dir, lint, options, t, name, ruleOptions) => {
560
+ const noReportAfterTransformWithOptions = currify((dir, linterOptions, options, t, name, ruleOptions) => {
561
+ const {lint, ext} = linterOptions;
522
562
  const full = join(dir, name);
523
- const [source, isTS] = readFixture(full);
563
+ const [source, isTS] = readFixture(full, ext);
524
564
  const rule = parseRule(options);
525
565
 
526
566
  const rules = {
@@ -539,9 +579,10 @@ const noReportAfterTransformWithOptions = currify((dir, lint, options, t, name,
539
579
 
540
580
  module.exports._createNoReportAfterTransformWithOptions = noReportAfterTransformWithOptions;
541
581
 
542
- const reportWithOptions = currify((dir, lint, options, t, name, message, ruleOptions) => {
582
+ const reportWithOptions = currify((dir, linterOptions, options, t, name, message, ruleOptions) => {
583
+ const {lint, ext} = linterOptions;
543
584
  const full = join(dir, name);
544
- const [source, isTS] = readFixture(full);
585
+ const [source, isTS] = readFixture(full, ext);
545
586
 
546
587
  const rule = parseRule(options);
547
588
 
@@ -560,9 +601,10 @@ const reportWithOptions = currify((dir, lint, options, t, name, message, ruleOpt
560
601
  return run(source, message);
561
602
  });
562
603
 
563
- const noReportWithOptions = currify((dir, lint, options, t, name, ruleOptions) => {
604
+ const noReportWithOptions = currify((dir, linterOptions, options, t, name, ruleOptions) => {
605
+ const {lint, ext} = linterOptions;
564
606
  const full = join(dir, name);
565
- const [source, isTS] = readFixture(full);
607
+ const [source, isTS] = readFixture(full, ext);
566
608
 
567
609
  rmFixture(`${full}-fix`);
568
610
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@putout/test",
3
- "version": "13.0.1",
3
+ "version": "13.2.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 ",
@@ -66,6 +66,7 @@
66
66
  "@putout/plugin-putout": "*",
67
67
  "@putout/plugin-remove-console": "*",
68
68
  "@putout/plugin-remove-unused-variables": "*",
69
+ "@putout/processor-wasm": "^3.0.0",
69
70
  "c8": "^10.0.0",
70
71
  "eslint": "^9.0.0",
71
72
  "eslint-plugin-n": "^17.0.0",