|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const test = require('ava'); |
| 4 | +const avaRuleTester = require('eslint-ava-rule-tester'); |
| 5 | +const rule = require('../rules/prefer-t-throws'); |
| 6 | + |
| 7 | +const ruleTester = avaRuleTester(test, { |
| 8 | + parserOptions: { |
| 9 | + ecmaVersion: 'latest', |
| 10 | + }, |
| 11 | +}); |
| 12 | + |
| 13 | +const header = 'const test = require(\'ava\');\n'; |
| 14 | + |
| 15 | +ruleTester.run('prefer-t-throws', rule, { |
| 16 | + valid: [ |
| 17 | + `${header}test(async t => { const error = await t.throwsAsync(promise); t.is(error, 'error'); });`, |
| 18 | + `${header}test(t => { const error = t.throws(fn()); t.is(error, 'error'); });`, |
| 19 | + `${header}test(async t => { try { t.fail(); unicorn(); } catch (error) { t.is(error, 'error'); } });`, |
| 20 | + `${header}test(async t => { try { await promise; } catch (error) { t.is(error, 'error'); } });`, |
| 21 | + ], |
| 22 | + invalid: [ |
| 23 | + { |
| 24 | + code: `${header}test(async t => { try { async function unicorn() { throw await Promise.resolve('error') }; unicorn(); t.fail(); } catch (error) { t.is(error, 'error'); } });`, |
| 25 | + errors: [{message: 'Prefer using the `t.throws()` assertion.'}], |
| 26 | + }, |
| 27 | + { |
| 28 | + code: `${header}test(async t => { try { await Promise.reject('error'); t.fail(); } catch (error) { t.is(error, 'error'); } });`, |
| 29 | + errors: [{message: 'Prefer using the `t.throwsAsync()` assertion.'}], |
| 30 | + }, |
| 31 | + { |
| 32 | + code: `${header}test(async t => { try { if (await promise); t.fail(); } catch (error) { t.is(error, 'error'); } });`, |
| 33 | + errors: [{message: 'Prefer using the `t.throwsAsync()` assertion.'}], |
| 34 | + }, |
| 35 | + { |
| 36 | + code: `${header}test(async t => { try { (await 1) > 2; t.fail(); } catch (error) { t.is(error, 'error'); } });`, |
| 37 | + errors: [{message: 'Prefer using the `t.throwsAsync()` assertion.'}], |
| 38 | + }, |
| 39 | + { |
| 40 | + code: `${header}test(async t => { try { (await getArray())[0]; t.fail(); } catch (error) { t.is(error, 'error'); } });`, |
| 41 | + errors: [{message: 'Prefer using the `t.throwsAsync()` assertion.'}], |
| 42 | + }, |
| 43 | + { |
| 44 | + code: `${header}test(async t => { try { getArraySync(await 20)[0]; t.fail(); } catch (error) { t.is(error, 'error'); } });`, |
| 45 | + errors: [{message: 'Prefer using the `t.throwsAsync()` assertion.'}], |
| 46 | + }, |
| 47 | + { |
| 48 | + code: `${header}test(async t => { try { getArraySync()[await 0]; t.fail(); } catch (error) { t.is(error, 'error'); } });`, |
| 49 | + errors: [{message: 'Prefer using the `t.throwsAsync()` assertion.'}], |
| 50 | + }, |
| 51 | + { |
| 52 | + code: `${header}test(async t => { try { new (await cl())(1); t.fail(); } catch (error) { t.is(error, 'error'); } });`, |
| 53 | + errors: [{message: 'Prefer using the `t.throwsAsync()` assertion.'}], |
| 54 | + }, |
| 55 | + { |
| 56 | + code: `${header}test(async t => { try { if (false) { await promise; }; t.fail(); } catch (error) { t.is(error, 'error'); } });`, |
| 57 | + errors: [{message: 'Prefer using the `t.throwsAsync()` assertion.'}], |
| 58 | + }, |
| 59 | + { |
| 60 | + code: `${header}test(t => { try { undefined(); t.fail(); } catch (error) { t.ok(error instanceof TypeError); } });`, |
| 61 | + errors: [{message: 'Prefer using the `t.throws()` assertion.'}], |
| 62 | + }, |
| 63 | + { |
| 64 | + code: `${header}test(async t => { try { undefined(); t.fail(); } catch (error) { t.ok(error instanceof TypeError); } });`, |
| 65 | + errors: [{message: 'Prefer using the `t.throws()` assertion.'}], |
| 66 | + }, |
| 67 | + ], |
| 68 | +}); |
0 commit comments