|
| 1 | +'use strict' |
| 2 | + |
| 3 | +const rule = require('../rules/prefer-catch') |
| 4 | +const { RuleTester } = require('./rule-tester') |
| 5 | +const ruleTester = new RuleTester({ |
| 6 | + parserOptions: { |
| 7 | + ecmaVersion: 8, |
| 8 | + }, |
| 9 | +}) |
| 10 | + |
| 11 | +const message = 'Prefer `catch` to `then(a, b)`/`then(null, b)`.' |
| 12 | + |
| 13 | +ruleTester.run('prefer-catch', rule, { |
| 14 | + valid: [ |
| 15 | + 'prom.then()', |
| 16 | + 'prom.then(fn)', |
| 17 | + 'prom.then(fn1).then(fn2)', |
| 18 | + 'prom.then(() => {})', |
| 19 | + 'prom.then(function () {})', |
| 20 | + 'prom.catch()', |
| 21 | + 'prom.catch(handleErr).then(handle)', |
| 22 | + 'prom.catch(handleErr)', |
| 23 | + ], |
| 24 | + |
| 25 | + invalid: [ |
| 26 | + { |
| 27 | + code: 'hey.then(fn1, fn2)', |
| 28 | + errors: [{ message }], |
| 29 | + output: 'hey.catch(fn2).then(fn1)', |
| 30 | + }, |
| 31 | + { |
| 32 | + code: 'hey.then(fn1, (fn2))', |
| 33 | + errors: [{ message }], |
| 34 | + output: 'hey.catch(fn2).then(fn1)', |
| 35 | + }, |
| 36 | + { |
| 37 | + code: 'hey.then(null, fn2)', |
| 38 | + errors: [{ message }], |
| 39 | + output: 'hey.catch(fn2)', |
| 40 | + }, |
| 41 | + { |
| 42 | + code: 'hey.then(undefined, fn2)', |
| 43 | + errors: [{ message }], |
| 44 | + output: 'hey.catch(fn2)', |
| 45 | + }, |
| 46 | + { |
| 47 | + code: 'function foo() { hey.then(x => {}, () => {}) }', |
| 48 | + errors: [{ message }], |
| 49 | + output: 'function foo() { hey.catch(() => {}).then(x => {}) }', |
| 50 | + }, |
| 51 | + { |
| 52 | + code: ` |
| 53 | + function foo() { |
| 54 | + hey.then(function a() { }, function b() {}).then(fn1, fn2) |
| 55 | + } |
| 56 | + `, |
| 57 | + errors: [{ message }, { message }], |
| 58 | + output: ` |
| 59 | + function foo() { |
| 60 | + hey.catch(function b() {}).then(function a() { }).catch(fn2).then(fn1) |
| 61 | + } |
| 62 | + `, |
| 63 | + }, |
| 64 | + ], |
| 65 | +}) |
0 commit comments