|
| 1 | +import test from 'ava'; |
| 2 | +import AvaRuleTester from 'eslint-ava-rule-tester'; |
| 3 | +import rule from '../rules/no-useless-t-pass.js'; |
| 4 | + |
| 5 | +const ruleTester = new AvaRuleTester(test, { |
| 6 | + languageOptions: { |
| 7 | + ecmaVersion: 'latest', |
| 8 | + }, |
| 9 | +}); |
| 10 | + |
| 11 | +const header = 'const test = require(\'ava\');\n'; |
| 12 | + |
| 13 | +const error = [{messageId: 'no-useless-t-pass'}]; |
| 14 | + |
| 15 | +ruleTester.run('no-useless-t-pass', rule, { |
| 16 | + valid: [ |
| 17 | + // Useful: t.pass() with t.plan() |
| 18 | + `${header} test(t => { t.plan(1); t.pass(); });`, |
| 19 | + // Multiple assertions including t.pass() with t.plan() |
| 20 | + `${header} test(t => { t.plan(2); t.pass(); t.is(1, 1); });`, |
| 21 | + // No t.pass() at all |
| 22 | + `${header} test(t => { t.is(1, 1); });`, |
| 23 | + // Not a test file |
| 24 | + 'test(t => { t.pass(); });', |
| 25 | + // Reversed order: t.pass() before t.plan() |
| 26 | + `${header} test(t => { t.pass(); t.plan(1); });`, |
| 27 | + // Context usage is not an assertion |
| 28 | + `${header} test(t => { t.context.pass(); });`, |
| 29 | + // Test modifiers with t.plan() |
| 30 | + `${header} test.serial(t => { t.plan(1); t.pass(); });`, |
| 31 | + `${header} test.failing(t => { t.plan(1); t.pass(); });`, |
| 32 | + // Skipped pass with t.plan() |
| 33 | + `${header} test(t => { t.plan(1); t.skip.pass(); });`, |
| 34 | + // Not a test object (foo.t.pass) |
| 35 | + `${header} test(t => { ${'foo.t.pass(); '.repeat(2)}});`, |
| 36 | + // ESM import |
| 37 | + 'import test from \'ava\';\n test(t => { t.plan(1); t.pass(); });', |
| 38 | + ], |
| 39 | + invalid: [ |
| 40 | + { |
| 41 | + code: `${header} test(t => { t.pass(); });`, |
| 42 | + errors: error, |
| 43 | + }, |
| 44 | + { |
| 45 | + code: `${header} test(t => { t.pass('message'); });`, |
| 46 | + errors: error, |
| 47 | + }, |
| 48 | + { |
| 49 | + code: `${header} test(t => { t.pass(); t.pass(); });`, |
| 50 | + errors: [...error, ...error], |
| 51 | + }, |
| 52 | + { |
| 53 | + code: `${header} test(t => { t.pass(); t.is(1, 1); });`, |
| 54 | + errors: error, |
| 55 | + }, |
| 56 | + { |
| 57 | + code: `${header} test(t => { t.skip.pass(); });`, |
| 58 | + errors: error, |
| 59 | + }, |
| 60 | + // Two tests: one valid (has t.plan), one invalid (no t.plan) - ensures state resets |
| 61 | + { |
| 62 | + code: `${header} test(t => { t.plan(1); t.pass(); }); test(t => { t.pass(); });`, |
| 63 | + errors: error, |
| 64 | + }, |
| 65 | + // Alternative test object name |
| 66 | + { |
| 67 | + code: `${header} test(t => { tt.pass(); });`, |
| 68 | + errors: error, |
| 69 | + }, |
| 70 | + // Hooks: t.plan() is not available, so t.pass() is always useless |
| 71 | + { |
| 72 | + code: `${header} test.before(t => { t.pass(); });`, |
| 73 | + errors: error, |
| 74 | + }, |
| 75 | + { |
| 76 | + code: `${header} test.beforeEach(t => { t.pass(); });`, |
| 77 | + errors: error, |
| 78 | + }, |
| 79 | + { |
| 80 | + code: `${header} test.after(t => { t.pass(); });`, |
| 81 | + errors: error, |
| 82 | + }, |
| 83 | + { |
| 84 | + code: `${header} test.afterEach(t => { t.pass(); });`, |
| 85 | + errors: error, |
| 86 | + }, |
| 87 | + // Test modifiers without t.plan() |
| 88 | + { |
| 89 | + code: `${header} test.serial(t => { t.pass(); });`, |
| 90 | + errors: error, |
| 91 | + }, |
| 92 | + { |
| 93 | + code: `${header} test.failing(t => { t.pass(); });`, |
| 94 | + errors: error, |
| 95 | + }, |
| 96 | + // State reset: invalid then valid |
| 97 | + { |
| 98 | + code: `${header} test(t => { t.pass(); }); test(t => { t.plan(1); t.pass(); });`, |
| 99 | + errors: error, |
| 100 | + }, |
| 101 | + // Nested callback with t.pass() |
| 102 | + { |
| 103 | + code: `${header} test(t => { setTimeout(() => { t.pass(); }, 0); });`, |
| 104 | + errors: error, |
| 105 | + }, |
| 106 | + // Async test |
| 107 | + { |
| 108 | + code: `${header} test(async t => { t.pass(); });`, |
| 109 | + errors: error, |
| 110 | + }, |
| 111 | + // ESM import |
| 112 | + { |
| 113 | + code: 'import test from \'ava\';\n test(t => { t.pass(); });', |
| 114 | + errors: error, |
| 115 | + }, |
| 116 | + ], |
| 117 | +}); |
0 commit comments