|
| 1 | +import RuleTester from './helpers/rule-tester.js'; |
| 2 | +import rule from '../rules/prefer-t-throws.js'; |
| 3 | + |
| 4 | +const ruleTester = new RuleTester(); |
| 5 | + |
| 6 | +const syncError = {messageId: 'prefer-t-throws'}; |
| 7 | +const asyncError = {messageId: 'prefer-t-throws-async'}; |
| 8 | + |
| 9 | +ruleTester.run('prefer-t-throws', rule, { |
| 10 | + valid: [ |
| 11 | + // Try/catch without `t.fail()` |
| 12 | + 'test(t => { try { foo(); } catch (error) { t.is(error.message, "expected"); } });', |
| 13 | + // Try/finally without catch |
| 14 | + 'test(t => { try { foo(); } finally { cleanup(); } });', |
| 15 | + // `t.fail()` inside a nested arrow function, not a direct statement |
| 16 | + 'test(t => { try { foo(() => { t.fail(); }); } catch (error) { t.pass(); } });', |
| 17 | + // `t.fail()` inside a nested function expression, not a direct statement |
| 18 | + 'test(t => { try { foo(function() { t.fail(); }); } catch (error) { t.pass(); } });', |
| 19 | + // `t.fail()` only in catch block, not in try block |
| 20 | + 'test(t => { try { foo(); } catch (error) { t.fail(); } });', |
| 21 | + // Not a test file |
| 22 | + {code: 'test(t => { try { foo(); t.fail(); } catch (error) { t.pass(); } });', noHeader: true}, |
| 23 | + // Empty try block |
| 24 | + 'test(t => { try {} catch (error) { t.pass(); } });', |
| 25 | + // `t.fail()` inside nested if, not a direct statement |
| 26 | + 'test(t => { try { if (true) { t.fail(); } } catch (error) { t.pass(); } });', |
| 27 | + // `t.context.fail()` is not `t.fail()` |
| 28 | + 'test(t => { try { foo(); t.context.fail(); } catch (error) { t.pass(); } });', |
| 29 | + // `t.fail()` is the only statement in try block (no throwing code before it) |
| 30 | + 'test(t => { try { t.fail(); } catch (error) { t.pass(); } });', |
| 31 | + // `t.fail()` is the first statement, code follows it |
| 32 | + 'test(t => { try { t.fail(); foo(); } catch (error) { t.pass(); } });', |
| 33 | + // Not a test object (`foo.fail()`) |
| 34 | + 'test(t => { try { bar(); foo.fail(); } catch (error) { t.pass(); } });', |
| 35 | + // `t.fail()` inside for loop in try block, not a direct statement |
| 36 | + 'test(t => { try { for (const x of items) { t.fail(); } } catch (error) { t.pass(); } });', |
| 37 | + // `t.fail()` inside switch case in try block, not a direct statement |
| 38 | + 'test(t => { try { switch (x) { case 1: t.fail(); } } catch (error) { t.pass(); } });', |
| 39 | + // `t.fail()` inside while loop |
| 40 | + 'test(t => { try { while (true) { t.fail(); } } catch (error) { t.pass(); } });', |
| 41 | + // `t.fail()` inside block statement (bare braces) |
| 42 | + 'test(t => { try { { t.fail(); } } catch (error) { t.pass(); } });', |
| 43 | + // `t.fail()` inside nested function declaration |
| 44 | + 'test(t => { try { function f() { t.fail(); } f(); } catch (error) { t.pass(); } });', |
| 45 | + ], |
| 46 | + invalid: [ |
| 47 | + // Basic sync |
| 48 | + { |
| 49 | + code: 'test(t => { try { foo(); t.fail(); } catch (error) { t.is(error.message, "expected"); } });', |
| 50 | + errors: [syncError], |
| 51 | + }, |
| 52 | + // Basic async with `await` in try block |
| 53 | + { |
| 54 | + code: 'test(async t => { try { await foo(); t.fail(); } catch (error) { t.is(error.message, "expected"); } });', |
| 55 | + errors: [asyncError], |
| 56 | + }, |
| 57 | + // With message argument to `t.fail()` |
| 58 | + { |
| 59 | + code: 'test(t => { try { foo(); t.fail("should have thrown"); } catch (error) { t.true(error instanceof Error); } });', |
| 60 | + errors: [syncError], |
| 61 | + }, |
| 62 | + // In a hook |
| 63 | + { |
| 64 | + code: 'test.beforeEach(t => { try { setup(); t.fail(); } catch (error) { t.pass(); } });', |
| 65 | + errors: [syncError], |
| 66 | + }, |
| 67 | + // With serial modifier |
| 68 | + { |
| 69 | + code: 'test.serial(t => { try { foo(); t.fail(); } catch (error) { t.pass(); } });', |
| 70 | + errors: [syncError], |
| 71 | + }, |
| 72 | + // Alternative test object name (`tt`) |
| 73 | + { |
| 74 | + code: 'test(t => { try { foo(); tt.fail(); } catch (error) { tt.pass(); } });', |
| 75 | + errors: [syncError], |
| 76 | + }, |
| 77 | + // Alternative test object name (`t1`) |
| 78 | + { |
| 79 | + code: 'test(t => { try { foo(); t1.fail(); } catch (error) { t1.pass(); } });', |
| 80 | + errors: [syncError], |
| 81 | + }, |
| 82 | + // `t.skip.fail()` |
| 83 | + { |
| 84 | + code: 'test(t => { try { foo(); t.skip.fail(); } catch (error) { t.pass(); } });', |
| 85 | + errors: [syncError], |
| 86 | + }, |
| 87 | + // With `t.plan()` |
| 88 | + { |
| 89 | + code: 'test(t => { t.plan(1); try { foo(); t.fail(); } catch (error) { t.is(error.code, 1); } });', |
| 90 | + errors: [syncError], |
| 91 | + }, |
| 92 | + // Async hook with `await` in try |
| 93 | + { |
| 94 | + code: 'test.afterEach(async t => { try { await cleanup(); t.fail(); } catch (error) { t.pass(); } });', |
| 95 | + errors: [asyncError], |
| 96 | + }, |
| 97 | + // Try/catch/finally - still flags if try has `t.fail()` |
| 98 | + { |
| 99 | + code: 'test(t => { try { foo(); t.fail(); } catch (error) { t.pass(); } finally { cleanup(); } });', |
| 100 | + errors: [syncError], |
| 101 | + }, |
| 102 | + // `t.fail()` with dead code after it in try block |
| 103 | + { |
| 104 | + code: 'test(t => { try { foo(); t.fail(); bar(); } catch (error) { t.pass(); } });', |
| 105 | + errors: [syncError], |
| 106 | + }, |
| 107 | + // Empty catch body |
| 108 | + { |
| 109 | + code: 'test(t => { try { foo(); t.fail(); } catch (error) {} });', |
| 110 | + errors: [syncError], |
| 111 | + }, |
| 112 | + // Catch without binding |
| 113 | + { |
| 114 | + code: 'test(t => { try { foo(); t.fail(); } catch {} });', |
| 115 | + errors: [syncError], |
| 116 | + }, |
| 117 | + // Async test but sync try block (no `await`) - should suggest `t.throws()`, not `t.throwsAsync()` |
| 118 | + { |
| 119 | + code: 'test(async t => { try { foo(); t.fail(); } catch (error) { t.pass(); } });', |
| 120 | + errors: [syncError], |
| 121 | + }, |
| 122 | + // `await` only inside nested function in try - direct code is sync, so `t.throws()` |
| 123 | + { |
| 124 | + code: 'test(async t => { try { foo(async () => { await bar(); }); t.fail(); } catch (error) { t.pass(); } });', |
| 125 | + errors: [syncError], |
| 126 | + }, |
| 127 | + // Variable declaration with `await` before `t.fail()` |
| 128 | + { |
| 129 | + code: 'test(async t => { try { const result = await fetch(url); t.fail(); } catch (error) { t.is(error.status, 404); } });', |
| 130 | + errors: [asyncError], |
| 131 | + }, |
| 132 | + // Multiple statements before `t.fail()` |
| 133 | + { |
| 134 | + code: 'test(t => { try { setup(); foo(); bar(); t.fail(); } catch (error) { t.pass(); } });', |
| 135 | + errors: [syncError], |
| 136 | + }, |
| 137 | + // Multiple statements with `await` before `t.fail()` |
| 138 | + { |
| 139 | + code: 'test(async t => { try { setup(); await foo(); t.fail(); } catch (error) { t.pass(); } });', |
| 140 | + errors: [asyncError], |
| 141 | + }, |
| 142 | + // Nested try/catch both with `t.fail()` - two errors |
| 143 | + { |
| 144 | + code: 'test(async t => { try { try { await foo(); t.fail(); } catch (e) { t.pass(); } await bar(); t.fail(); } catch (e) { t.pass(); } });', |
| 145 | + errors: [asyncError, asyncError], |
| 146 | + }, |
| 147 | + // `test.after` hook |
| 148 | + { |
| 149 | + code: 'test.after(t => { try { cleanup(); t.fail(); } catch (error) { t.pass(); } });', |
| 150 | + errors: [syncError], |
| 151 | + }, |
| 152 | + // `test.after.always` hook |
| 153 | + { |
| 154 | + code: 'test.after.always(t => { try { cleanup(); t.fail(); } catch (error) { t.pass(); } });', |
| 155 | + errors: [syncError], |
| 156 | + }, |
| 157 | + // `for await...of` in try block should suggest `t.throwsAsync()` |
| 158 | + { |
| 159 | + code: 'test(async t => { try { for await (const x of stream) { process(x); } t.fail(); } catch (error) { t.pass(); } });', |
| 160 | + errors: [asyncError], |
| 161 | + }, |
| 162 | + // `await` inside regular `for...of` body (not `for await...of`) should suggest `t.throwsAsync()` |
| 163 | + { |
| 164 | + code: 'test(async t => { try { for (const x of items) { await process(x); } t.fail(); } catch (error) { t.pass(); } });', |
| 165 | + errors: [asyncError], |
| 166 | + }, |
| 167 | + // Multiple independent try/catch blocks - each flagged separately |
| 168 | + { |
| 169 | + code: 'test(t => { try { foo(); t.fail(); } catch (error) { t.pass(); } try { bar(); t.fail(); } catch (error) { t.pass(); } });', |
| 170 | + errors: [syncError, syncError], |
| 171 | + }, |
| 172 | + ], |
| 173 | +}); |
0 commit comments