|
| 1 | +import test from 'ava'; |
| 2 | +import avaRuleTester from 'eslint-ava-rule-tester'; |
| 3 | +import rule from '../rules/use-t-throws-async-well'; |
| 4 | + |
| 5 | +const ruleTester = avaRuleTester(test, { |
| 6 | + parserOptions: { |
| 7 | + ecmaVersion: 2020 |
| 8 | + } |
| 9 | +}); |
| 10 | + |
| 11 | +const header = 'const test = require(\'ava\');\n'; |
| 12 | + |
| 13 | +function asyncTestCase(contents, prependHeader) { |
| 14 | + const content = `test(async t => { ${contents} });`; |
| 15 | + |
| 16 | + if (prependHeader !== false) { |
| 17 | + return header + content; |
| 18 | + } |
| 19 | + |
| 20 | + return content; |
| 21 | +} |
| 22 | + |
| 23 | +function syncTestCase(contents, prependHeader) { |
| 24 | + const content = `test(t => { ${contents} });`; |
| 25 | + |
| 26 | + if (prependHeader !== false) { |
| 27 | + return header + content; |
| 28 | + } |
| 29 | + |
| 30 | + return content; |
| 31 | +} |
| 32 | + |
| 33 | +ruleTester.run('use-t-throws-async-well', rule, { |
| 34 | + valid: [ |
| 35 | + asyncTestCase('await t.throwsAsync(f)'), |
| 36 | + asyncTestCase('await t.notThrowsAsync(f)'), |
| 37 | + asyncTestCase('t.throws(f)'), |
| 38 | + asyncTestCase('t.notThrows(f)'), |
| 39 | + asyncTestCase('f(t.throwsAsync(f))'), |
| 40 | + asyncTestCase('let p = t.throwsAsync(f)'), |
| 41 | + asyncTestCase('p = t.throwsAsync(f)'), |
| 42 | + asyncTestCase('t.throwsAsync(f)', false), // Shouldn't be triggered since it's not a test file |
| 43 | + syncTestCase('t.throwsAsync(f)', false) // Shouldn't be triggered since it's not a test file |
| 44 | + ], |
| 45 | + invalid: [ |
| 46 | + { |
| 47 | + code: syncTestCase('t.throwsAsync(f)'), |
| 48 | + errors: [{ |
| 49 | + ruleId: 'use-t-throws-async-well', |
| 50 | + message: 'Use `await` with `t.throwsAsync()`.' |
| 51 | + }] |
| 52 | + }, |
| 53 | + { |
| 54 | + code: syncTestCase('t.notThrowsAsync(f)'), |
| 55 | + errors: [{ |
| 56 | + ruleId: 'use-t-throws-async-well', |
| 57 | + message: 'Use `await` with `t.notThrowsAsync()`.' |
| 58 | + }] |
| 59 | + }, |
| 60 | + { |
| 61 | + code: asyncTestCase('t.throwsAsync(f)'), |
| 62 | + output: asyncTestCase('await t.throwsAsync(f)'), |
| 63 | + errors: [{ |
| 64 | + ruleId: 'use-t-throws-async-well', |
| 65 | + message: 'Use `await` with `t.throwsAsync()`.' |
| 66 | + }] |
| 67 | + }, |
| 68 | + { |
| 69 | + code: asyncTestCase('t.notThrowsAsync(f)'), |
| 70 | + output: asyncTestCase('await t.notThrowsAsync(f)'), |
| 71 | + errors: [{ |
| 72 | + ruleId: 'use-t-throws-async-well', |
| 73 | + message: 'Use `await` with `t.notThrowsAsync()`.' |
| 74 | + }] |
| 75 | + } |
| 76 | + ] |
| 77 | +}); |
0 commit comments