@putout/plugin-tape 3.7.1 → 3.11.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
@@ -20,6 +20,10 @@ npm i @putout/plugin-tape -D
20
20
  "rules": {
21
21
  "tape/apply-stub-operator": "on",
22
22
  "tape/apply-with-name": "on",
23
+ "tape/add-t-end": "on",
24
+ "tape/add-stop-all": "on",
25
+ "tape/remove-useless-t-end": "on",
26
+ "tape/sync-with-name": "on",
23
27
  "tape/switch-expected-with-result": "on",
24
28
  "tape/convert-tape-to-supertape": "on",
25
29
  "tape/convert-throws-to-try-catch": "on",
@@ -343,6 +347,31 @@ test('should call init before show', (t) => {
343
347
  });
344
348
  ```
345
349
 
350
+ ## sync-with-name
351
+
352
+ ### ❌ Incorrect code example
353
+
354
+ ```js
355
+ test('should call init before show', (t) => {
356
+ const init = stub().withName('show');
357
+ const show = stub().withName('show');
358
+
359
+ t.calledInOrder([init, show]);
360
+ t.end();
361
+ });
362
+ ```
363
+
364
+ ### ✅ Correct code example
365
+
366
+ ```js
367
+ test('should call init before show', (t) => {
368
+ const init = stub().withName('init');
369
+ const show = stub().withName('show');
370
+
371
+ t.calledInOrder([init, show]);
372
+ t.end();
373
+ });
374
+ ```
346
375
 
347
376
  ## declare-stub
348
377
 
@@ -388,6 +417,70 @@ test('xxx', (t) => {
388
417
  });
389
418
  ```
390
419
 
420
+ ## add-t-end
421
+
422
+ ### ❌ Incorrect code example
423
+
424
+ ```js
425
+ test('xxx', () => {
426
+ });
427
+ ```
428
+
429
+ ### ✅ Correct code example
430
+
431
+ ```js
432
+ test('xxx', (t) => {
433
+ t.end();
434
+ });
435
+ ```
436
+
437
+ ## add-stop-all
438
+
439
+ ### ❌ Incorrect code example
440
+
441
+ ```js
442
+ test('stop-all: should be called', () => {
443
+ mockImport('fs/promises', {
444
+ readFile: stub(),
445
+ });
446
+
447
+ t.end();
448
+ });
449
+ ```
450
+
451
+ ### ✅ Correct code example
452
+
453
+ ```js
454
+ test('stop-all: should be called', () => {
455
+ mockImport('fs/promises', {
456
+ readFile: stub(),
457
+ });
458
+
459
+ stopAll();
460
+
461
+ t.end();
462
+ });
463
+ ```
464
+
465
+ ## remove-useless-t-end
466
+
467
+ ### ❌ Incorrect code example
468
+
469
+ ```js
470
+ test('test: remove me', () => {
471
+ t.end();
472
+ t.end();
473
+ });
474
+ ```
475
+
476
+ ### ✅ Correct code example
477
+
478
+ ```js
479
+ test('test: remove me', () => {
480
+ t.end();
481
+ });
482
+ ```
483
+
391
484
  ## convert-ok-to-match
392
485
 
393
486
  ### ❌ Incorrect code example
@@ -0,0 +1,85 @@
1
+ 'use strict';
2
+
3
+ const {
4
+ types,
5
+ operator,
6
+ template,
7
+ } = require('putout');
8
+
9
+ const {
10
+ traverse,
11
+ compare,
12
+ } = operator;
13
+
14
+ const {ExpressionStatement} = types;
15
+
16
+ module.exports.report = () => `Call 'stopAll()' at the end of test when 'mockImport()' used`;
17
+
18
+ module.exports.fix = (path) => {
19
+ const assertionPath = getAssertionsPath(path);
20
+ const stopAllNode = template.ast('stopAll()');
21
+
22
+ assertionPath.insertBefore(ExpressionStatement(stopAllNode));
23
+ };
24
+
25
+ const TEST = 'test("__a", (t) => __body)';
26
+
27
+ module.exports.traverse = ({push}) => ({
28
+ [TEST]: (path) => {
29
+ const {
30
+ hasMockImport,
31
+ hasStopAll,
32
+ hasAssertions,
33
+ } = check(path);
34
+
35
+ if (!hasMockImport)
36
+ return;
37
+
38
+ if (!hasAssertions)
39
+ return;
40
+
41
+ if (hasStopAll)
42
+ return;
43
+
44
+ push(path);
45
+ },
46
+ });
47
+
48
+ function check(path) {
49
+ let hasStopAll = false;
50
+ let hasMockImport = false;
51
+ let hasAssertions = false;
52
+
53
+ traverse(path, {
54
+ 'mockImport(__a, __b)': (path) => {
55
+ hasMockImport = true;
56
+ },
57
+ 'stopAll()': () => {
58
+ hasStopAll = true;
59
+ },
60
+ 't.__(__args)': () => {
61
+ hasAssertions = true;
62
+ },
63
+ });
64
+
65
+ return {
66
+ hasStopAll,
67
+ hasMockImport,
68
+ hasAssertions,
69
+ };
70
+ }
71
+
72
+ function getAssertionsPath(path) {
73
+ const bodyPath = path.get('arguments.1.body.body');
74
+ let resultPath = null;
75
+
76
+ for (const pathEl of bodyPath) {
77
+ if (compare(pathEl, 't.__(__args)')) {
78
+ resultPath = pathEl;
79
+ break;
80
+ }
81
+ }
82
+
83
+ return resultPath;
84
+ }
85
+
@@ -0,0 +1,30 @@
1
+ 'use strict';
2
+
3
+ const {
4
+ types,
5
+ template,
6
+ operator,
7
+ } = require('putout');
8
+ const {compare} = operator;
9
+ const {ExpressionStatement} = types;
10
+
11
+ module.exports.report = () => `'t.end()' is missing at the end of the test`;
12
+
13
+ module.exports.match = () => ({
14
+ 'test(__a, (t) => __body)': ({__body}) => {
15
+ const {body} = __body;
16
+ const {length} = body;
17
+
18
+ if (!length)
19
+ return true;
20
+
21
+ return !compare(body[length - 1], 't.end()');
22
+ },
23
+ });
24
+
25
+ module.exports.replace = () => ({
26
+ 'test(__a, (t) => __body)': ({__body}, path) => {
27
+ __body.body.push(ExpressionStatement(template.ast('t.end()')));
28
+ return path;
29
+ },
30
+ });
package/lib/index.js CHANGED
@@ -6,6 +6,7 @@ const getRule = (a) => ({
6
6
 
7
7
  module.exports.rules = {
8
8
  ...getRule('apply-with-name'),
9
+ ...getRule('sync-with-name'),
9
10
  ...getRule('convert-tape-to-supertape'),
10
11
  ...getRule('convert-throws-to-try-catch'),
11
12
  ...getRule('convert-does-not-throw-to-try-catch'),
@@ -25,5 +26,8 @@ module.exports.rules = {
25
26
  ...getRule('remove-useless-not-called-args'),
26
27
  ...getRule('switch-expected-with-result'),
27
28
  ...getRule('apply-stub-operator'),
29
+ ...getRule('add-t-end'),
30
+ ...getRule('add-stop-all'),
31
+ ...getRule('remove-useless-t-end'),
28
32
  };
29
33
 
@@ -0,0 +1,29 @@
1
+ 'use strict';
2
+
3
+ const {operator} = require('putout');
4
+
5
+ const {traverse} = operator;
6
+
7
+ module.exports.report = () => `Avoid useless 't.end()'`;
8
+
9
+ module.exports.fix = (path) => {
10
+ path.remove();
11
+ };
12
+
13
+ const TEST = 'test("__a", (t) => __body)';
14
+
15
+ module.exports.traverse = ({push}) => ({
16
+ [TEST]: (path) => {
17
+ const paths = [];
18
+
19
+ traverse(path, {
20
+ 't.end()': (path) => {
21
+ paths.push(path);
22
+ },
23
+ });
24
+
25
+ if (paths.length > 1)
26
+ push(paths.pop());
27
+ },
28
+ });
29
+
@@ -0,0 +1,14 @@
1
+ 'use strict';
2
+
3
+ module.exports.report = () => `'stub().withName(name)' should synced with variable name`;
4
+
5
+ module.exports.match = () => ({
6
+ 'const __a = stub().withName(__b)': ({__a, __b}) => __b.value !== __a.name,
7
+ });
8
+
9
+ module.exports.replace = () => ({
10
+ 'const __a = stub().withName(__b)': ({__a, __b}, path) => {
11
+ __b.value = __a.name;
12
+ return path;
13
+ },
14
+ });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@putout/plugin-tape",
3
- "version": "3.7.1",
3
+ "version": "3.11.0",
4
4
  "author": "coderaiser <mnemonic.enemy@gmail.com> (https://github.com/coderaiser)",
5
5
  "description": "putout plugin helps with tests",
6
6
  "homepage": "http://github.com/coderaiser/putout",
@@ -39,6 +39,7 @@
39
39
  "eslint-plugin-putout": "^10.0.0",
40
40
  "lerna": "^4.0.0",
41
41
  "madrun": "^8.0.1",
42
+ "montag": "^1.2.1",
42
43
  "nodemon": "^2.0.1"
43
44
  },
44
45
  "peerDependencies": {