|
| 1 | +import test from 'ava'; |
| 2 | +import AvaRuleTester from 'eslint-ava-rule-tester'; |
| 3 | +import rule from '../rules/no-nested-assertions.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 | +const error = { |
| 13 | + messageId: 'no-nested-assertions', |
| 14 | +}; |
| 15 | + |
| 16 | +ruleTester.run('no-nested-assertions', rule, { |
| 17 | + assertionOptions: { |
| 18 | + requireMessage: true, |
| 19 | + }, |
| 20 | + valid: [ |
| 21 | + // Sequential assertions |
| 22 | + header + 'test(t => { t.is(1, 1); t.true(true); });', |
| 23 | + // Assertions in t.try() callback are fine |
| 24 | + header + 'test(t => { t.try(tt => { tt.is(1, 1); }); });', |
| 25 | + // Multiple assertions in t.try() callback are fine |
| 26 | + header + 'test(t => { t.try(tt => { tt.is(1, 1); tt.true(true); }); });', |
| 27 | + // Non-assertion methods |
| 28 | + header + 'test(t => { t.plan(1); t.is(1, 1); });', |
| 29 | + // Not a test object |
| 30 | + header + 'test(t => { foo.is(t.is(1, 1)); });', |
| 31 | + // Shouldn't be triggered since it's not a test file |
| 32 | + 'test(t => { t.is(t.throws(fn).message, "expected"); });', |
| 33 | + ], |
| 34 | + invalid: [ |
| 35 | + // Assertion as argument to another assertion |
| 36 | + { |
| 37 | + code: header + 'test(t => { t.is(t.throws(fn).message, "expected"); });', |
| 38 | + errors: [error], |
| 39 | + }, |
| 40 | + { |
| 41 | + code: header + 'test(t => { t.true(t.throws(fn).message); });', |
| 42 | + errors: [error], |
| 43 | + }, |
| 44 | + { |
| 45 | + code: header + 'test(t => { t.deepEqual(t.throws(fn), expected); });', |
| 46 | + errors: [error], |
| 47 | + }, |
| 48 | + // Assertion inside t.throws() callback |
| 49 | + { |
| 50 | + code: header + 'test(t => { t.throws(() => { t.is(1, 2); }); });', |
| 51 | + errors: [error], |
| 52 | + }, |
| 53 | + // Assertion inside t.throwsAsync() callback |
| 54 | + { |
| 55 | + code: header + 'test(t => { t.throwsAsync(() => { t.true(false); }); });', |
| 56 | + errors: [error], |
| 57 | + }, |
| 58 | + // Assertion inside t.notThrows() callback |
| 59 | + { |
| 60 | + code: header + 'test(t => { t.notThrows(() => { t.pass(); }); });', |
| 61 | + errors: [error], |
| 62 | + }, |
| 63 | + // Assertion inside t.notThrowsAsync() callback |
| 64 | + { |
| 65 | + code: header + 'test(t => { t.notThrowsAsync(() => { t.true(true); }); });', |
| 66 | + errors: [error], |
| 67 | + }, |
| 68 | + // Multiple assertions inside t.throws() callback |
| 69 | + { |
| 70 | + code: header + 'test(t => { t.throws(() => { t.is(1, 2); t.true(false); }); });', |
| 71 | + errors: [error, error], |
| 72 | + }, |
| 73 | + // Deeply nested |
| 74 | + { |
| 75 | + code: header + 'test(t => { t.is(t.throws(() => { t.pass(); }).message, "x"); });', |
| 76 | + errors: [error, error], |
| 77 | + }, |
| 78 | + // With .skip modifier |
| 79 | + { |
| 80 | + code: header + 'test(t => { t.is(t.throws.skip(fn).message, "expected"); });', |
| 81 | + errors: [error], |
| 82 | + }, |
| 83 | + // Nesting within t.try() callback is still caught |
| 84 | + { |
| 85 | + code: header + 'test(t => { t.try(tt => { tt.is(tt.throws(fn).message, "expected"); }); });', |
| 86 | + errors: [error], |
| 87 | + }, |
| 88 | + // Alternative test object names |
| 89 | + { |
| 90 | + code: header + 'test(t => { tt.is(tt.throws(fn).message, "expected"); });', |
| 91 | + errors: [error], |
| 92 | + }, |
| 93 | + ], |
| 94 | +}); |
0 commit comments