|
| 1 | +import { TSESLint } from '@typescript-eslint/experimental-utils'; |
| 2 | +import rule from '../prefer-comparison-matcher'; |
| 3 | +import { espreeParser } from './test-utils'; |
| 4 | + |
| 5 | +const ruleTester = new TSESLint.RuleTester({ |
| 6 | + parser: espreeParser, |
| 7 | + parserOptions: { |
| 8 | + ecmaVersion: 2015, |
| 9 | + }, |
| 10 | +}); |
| 11 | + |
| 12 | +const generateInvalidCases = ( |
| 13 | + operator: string, |
| 14 | + equalityMatcher: string, |
| 15 | + preferredMatcher: string, |
| 16 | + preferredMatcherWhenNegated: string, |
| 17 | +): Array<TSESLint.InvalidTestCase<'useToBeComparison', never>> => { |
| 18 | + return [ |
| 19 | + { |
| 20 | + code: `expect(value ${operator} 1).${equalityMatcher}(true);`, |
| 21 | + output: `expect(value).${preferredMatcher}(1);`, |
| 22 | + errors: [ |
| 23 | + { |
| 24 | + messageId: 'useToBeComparison', |
| 25 | + data: { preferredMatcher }, |
| 26 | + column: 18 + operator.length, |
| 27 | + line: 1, |
| 28 | + }, |
| 29 | + ], |
| 30 | + }, |
| 31 | + { |
| 32 | + code: `expect(value ${operator} 1)['${equalityMatcher}'](true);`, |
| 33 | + output: `expect(value).${preferredMatcher}(1);`, |
| 34 | + errors: [ |
| 35 | + { |
| 36 | + messageId: 'useToBeComparison', |
| 37 | + data: { preferredMatcher }, |
| 38 | + column: 18 + operator.length, |
| 39 | + line: 1, |
| 40 | + }, |
| 41 | + ], |
| 42 | + }, |
| 43 | + { |
| 44 | + code: `expect(value ${operator} 1).${equalityMatcher}(false);`, |
| 45 | + output: `expect(value).${preferredMatcherWhenNegated}(1);`, |
| 46 | + errors: [ |
| 47 | + { |
| 48 | + messageId: 'useToBeComparison', |
| 49 | + data: { preferredMatcher: preferredMatcherWhenNegated }, |
| 50 | + column: 18 + operator.length, |
| 51 | + line: 1, |
| 52 | + }, |
| 53 | + ], |
| 54 | + }, |
| 55 | + { |
| 56 | + code: `expect(value ${operator} 1)['${equalityMatcher}'](false);`, |
| 57 | + output: `expect(value).${preferredMatcherWhenNegated}(1);`, |
| 58 | + errors: [ |
| 59 | + { |
| 60 | + messageId: 'useToBeComparison', |
| 61 | + data: { preferredMatcher: preferredMatcherWhenNegated }, |
| 62 | + column: 18 + operator.length, |
| 63 | + line: 1, |
| 64 | + }, |
| 65 | + ], |
| 66 | + }, |
| 67 | + { |
| 68 | + code: `expect(value ${operator} 1).not.${equalityMatcher}(true);`, |
| 69 | + output: `expect(value).${preferredMatcherWhenNegated}(1);`, |
| 70 | + errors: [ |
| 71 | + { |
| 72 | + messageId: 'useToBeComparison', |
| 73 | + data: { preferredMatcher: preferredMatcherWhenNegated }, |
| 74 | + column: 18 + operator.length, |
| 75 | + line: 1, |
| 76 | + }, |
| 77 | + ], |
| 78 | + }, |
| 79 | + { |
| 80 | + code: `expect(value ${operator} 1)['not'].${equalityMatcher}(true);`, |
| 81 | + output: `expect(value).${preferredMatcherWhenNegated}(1);`, |
| 82 | + errors: [ |
| 83 | + { |
| 84 | + messageId: 'useToBeComparison', |
| 85 | + data: { preferredMatcher: preferredMatcherWhenNegated }, |
| 86 | + column: 18 + operator.length, |
| 87 | + line: 1, |
| 88 | + }, |
| 89 | + ], |
| 90 | + }, |
| 91 | + { |
| 92 | + code: `expect(value ${operator} 1).not.${equalityMatcher}(false);`, |
| 93 | + output: `expect(value).${preferredMatcher}(1);`, |
| 94 | + errors: [ |
| 95 | + { |
| 96 | + messageId: 'useToBeComparison', |
| 97 | + data: { preferredMatcher }, |
| 98 | + column: 18 + operator.length, |
| 99 | + line: 1, |
| 100 | + }, |
| 101 | + ], |
| 102 | + }, |
| 103 | + ]; |
| 104 | +}; |
| 105 | + |
| 106 | +const generateValidStringLiteralCases = (operator: string, matcher: string) => { |
| 107 | + return [ |
| 108 | + ['x', "'y'"], |
| 109 | + ['x', '`y`'], |
| 110 | + ['x', '`y${z}`'], |
| 111 | + ].reduce((cases, [a, b]) => [ |
| 112 | + ...cases, |
| 113 | + ...[ |
| 114 | + `expect(${a} ${operator} ${b}).${matcher}(true)`, |
| 115 | + `expect(${a} ${operator} ${b}).${matcher}(false)`, |
| 116 | + `expect(${a} ${operator} ${b}).not.${matcher}(true)`, |
| 117 | + `expect(${a} ${operator} ${b}).not.${matcher}(false)`, |
| 118 | + `expect(${b} ${operator} ${a}).${matcher}(true)`, |
| 119 | + `expect(${b} ${operator} ${a}).${matcher}(false)`, |
| 120 | + `expect(${b} ${operator} ${a}).not.${matcher}(true)`, |
| 121 | + `expect(${b} ${operator} ${a}).not.${matcher}(false)`, |
| 122 | + `expect(${a} ${operator} ${b}).${matcher}(true)`, |
| 123 | + `expect(${a} ${operator} ${b}).${matcher}(false)`, |
| 124 | + `expect(${a} ${operator} ${b}).not.${matcher}(true)`, |
| 125 | + `expect(${a} ${operator} ${b}).not.${matcher}(false)`, |
| 126 | + `expect(${b} ${operator} ${a}).${matcher}(true)`, |
| 127 | + `expect(${b} ${operator} ${a}).${matcher}(false)`, |
| 128 | + `expect(${b} ${operator} ${a}).not.${matcher}(true)`, |
| 129 | + `expect(${b} ${operator} ${a}).not.${matcher}(false)`, |
| 130 | + `expect(${b} ${operator} ${b}).not.${matcher}(false)`, |
| 131 | + ], |
| 132 | + ]); |
| 133 | +}; |
| 134 | + |
| 135 | +const testComparisonOperator = ( |
| 136 | + operator: string, |
| 137 | + preferredMatcher: string, |
| 138 | + preferredMatcherWhenNegated: string, |
| 139 | +) => { |
| 140 | + ruleTester.run(`prefer-to-be-comparison: ${operator}`, rule, { |
| 141 | + valid: [ |
| 142 | + 'expect()', |
| 143 | + 'expect({}).toStrictEqual({})', |
| 144 | + `expect(value).${preferredMatcher}(1);`, |
| 145 | + `expect(value).${preferredMatcherWhenNegated}(1);`, |
| 146 | + `expect(value).not.${preferredMatcher}(1);`, |
| 147 | + `expect(value).not.${preferredMatcherWhenNegated}(1);`, |
| 148 | + `expect(value).${preferredMatcher}(1);`, |
| 149 | + ...['toBe', 'toEqual', 'toStrictEqual'].reduce<string[]>( |
| 150 | + (cases, equalityMatcher) => [ |
| 151 | + ...cases, |
| 152 | + ...generateValidStringLiteralCases(operator, equalityMatcher), |
| 153 | + ], |
| 154 | + [], |
| 155 | + ), |
| 156 | + ], |
| 157 | + invalid: ['toBe', 'toEqual', 'toStrictEqual'].reduce< |
| 158 | + Array<TSESLint.InvalidTestCase<'useToBeComparison', never>> |
| 159 | + >( |
| 160 | + (cases, equalityMatcher) => [ |
| 161 | + ...cases, |
| 162 | + ...generateInvalidCases( |
| 163 | + operator, |
| 164 | + equalityMatcher, |
| 165 | + preferredMatcher, |
| 166 | + preferredMatcherWhenNegated, |
| 167 | + ), |
| 168 | + ], |
| 169 | + [], |
| 170 | + ), |
| 171 | + }); |
| 172 | +}; |
| 173 | + |
| 174 | +testComparisonOperator('>', 'toBeGreaterThan', 'toBeLessThanOrEqual'); |
| 175 | +testComparisonOperator('<', 'toBeLessThan', 'toBeGreaterThanOrEqual'); |
| 176 | +testComparisonOperator('>=', 'toBeGreaterThanOrEqual', 'toBeLessThan'); |
| 177 | +testComparisonOperator('<=', 'toBeLessThanOrEqual', 'toBeGreaterThan'); |
| 178 | + |
| 179 | +ruleTester.run(`prefer-to-be-comparison`, rule, { |
| 180 | + valid: [ |
| 181 | + 'expect()', |
| 182 | + 'expect({}).toStrictEqual({})', |
| 183 | + 'expect(a === b).toBe(true)', |
| 184 | + 'expect(a !== 2).toStrictEqual(true)', |
| 185 | + 'expect(a === b).not.toEqual(true)', |
| 186 | + 'expect(a !== "string").toStrictEqual(true)', |
| 187 | + 'expect(5 != a).toBe(true)', |
| 188 | + 'expect(a == "string").toBe(true)', |
| 189 | + 'expect(a == "string").not.toBe(true)', |
| 190 | + ], |
| 191 | + invalid: [], |
| 192 | +}); |
0 commit comments