|
| 1 | +import { TSESLint } from '@typescript-eslint/experimental-utils'; |
| 2 | +import resolveFrom from 'resolve-from'; |
| 3 | +import rule from '../no-banned-matchers'; |
| 4 | + |
| 5 | +const ruleTester = new TSESLint.RuleTester({ |
| 6 | + parser: resolveFrom(require.resolve('eslint'), 'espree'), |
| 7 | + parserOptions: { |
| 8 | + ecmaVersion: 2017, |
| 9 | + }, |
| 10 | +}); |
| 11 | + |
| 12 | +ruleTester.run('no-banned-matchers', rule, { |
| 13 | + valid: [ |
| 14 | + 'expect(a).toHaveBeenCalled()', |
| 15 | + 'expect(a).not.toHaveBeenCalled()', |
| 16 | + 'expect(a).toHaveBeenCalledTimes()', |
| 17 | + 'expect(a).toHaveBeenCalledWith()', |
| 18 | + 'expect(a).toHaveBeenLastCalledWith()', |
| 19 | + 'expect(a).toHaveBeenNthCalledWith()', |
| 20 | + 'expect(a).toHaveReturned()', |
| 21 | + 'expect(a).toHaveReturnedTimes()', |
| 22 | + 'expect(a).toHaveReturnedWith()', |
| 23 | + 'expect(a).toHaveLastReturnedWith()', |
| 24 | + 'expect(a).toHaveNthReturnedWith()', |
| 25 | + 'expect(a).toThrow()', |
| 26 | + 'expect(a).rejects;', |
| 27 | + 'expect(a);', |
| 28 | + { |
| 29 | + code: 'expect(a).resolves', |
| 30 | + options: [{ not: null }], |
| 31 | + }, |
| 32 | + { |
| 33 | + code: 'expect(a).toBe(b)', |
| 34 | + options: [{ 'not.toBe': null }], |
| 35 | + }, |
| 36 | + { |
| 37 | + code: 'expect(a)["toBe"](b)', |
| 38 | + options: [{ 'not.toBe': null }], |
| 39 | + }, |
| 40 | + ], |
| 41 | + invalid: [ |
| 42 | + { |
| 43 | + code: 'expect(a).toBe(b)', |
| 44 | + options: [{ toBe: null }], |
| 45 | + errors: [ |
| 46 | + { |
| 47 | + messageId: 'bannedChain', |
| 48 | + data: { |
| 49 | + message: null, |
| 50 | + chain: 'toBe', |
| 51 | + }, |
| 52 | + column: 11, |
| 53 | + line: 1, |
| 54 | + }, |
| 55 | + ], |
| 56 | + }, |
| 57 | + { |
| 58 | + code: 'expect(a)["toBe"](b)', |
| 59 | + options: [{ toBe: null }], |
| 60 | + errors: [ |
| 61 | + { |
| 62 | + messageId: 'bannedChain', |
| 63 | + data: { |
| 64 | + message: null, |
| 65 | + chain: 'toBe', |
| 66 | + }, |
| 67 | + column: 11, |
| 68 | + line: 1, |
| 69 | + }, |
| 70 | + ], |
| 71 | + }, |
| 72 | + { |
| 73 | + code: 'expect(a).not', |
| 74 | + options: [{ not: null }], |
| 75 | + errors: [ |
| 76 | + { |
| 77 | + messageId: 'bannedChain', |
| 78 | + data: { |
| 79 | + message: null, |
| 80 | + chain: 'not', |
| 81 | + }, |
| 82 | + column: 11, |
| 83 | + line: 1, |
| 84 | + }, |
| 85 | + ], |
| 86 | + }, |
| 87 | + { |
| 88 | + code: 'expect(a).not.toBe(b)', |
| 89 | + options: [{ not: null }], |
| 90 | + errors: [ |
| 91 | + { |
| 92 | + messageId: 'bannedChain', |
| 93 | + data: { |
| 94 | + message: null, |
| 95 | + chain: 'not', |
| 96 | + }, |
| 97 | + column: 11, |
| 98 | + line: 1, |
| 99 | + }, |
| 100 | + ], |
| 101 | + }, |
| 102 | + { |
| 103 | + code: 'expect(a).not.toBe(b)', |
| 104 | + options: [{ 'not.toBe': null }], |
| 105 | + errors: [ |
| 106 | + { |
| 107 | + messageId: 'bannedChain', |
| 108 | + data: { |
| 109 | + message: null, |
| 110 | + chain: 'not.toBe', |
| 111 | + }, |
| 112 | + endColumn: 19, |
| 113 | + column: 11, |
| 114 | + line: 1, |
| 115 | + }, |
| 116 | + ], |
| 117 | + }, |
| 118 | + { |
| 119 | + code: 'expect(a).toBe(b)', |
| 120 | + options: [{ toBe: 'Prefer `toStrictEqual` instead' }], |
| 121 | + errors: [ |
| 122 | + { |
| 123 | + messageId: 'bannedChainWithMessage', |
| 124 | + data: { |
| 125 | + message: 'Prefer `toStrictEqual` instead', |
| 126 | + chain: 'toBe', |
| 127 | + }, |
| 128 | + column: 11, |
| 129 | + line: 1, |
| 130 | + }, |
| 131 | + ], |
| 132 | + }, |
| 133 | + { |
| 134 | + code: ` |
| 135 | + test('some test', async () => { |
| 136 | + await expect(Promise.resolve(1)).resolves.toBe(1); |
| 137 | + }); |
| 138 | + `, |
| 139 | + options: [{ resolves: 'Use `expect(await promise)` instead.' }], |
| 140 | + errors: [ |
| 141 | + { |
| 142 | + messageId: 'bannedChainWithMessage', |
| 143 | + data: { |
| 144 | + message: 'Use `expect(await promise)` instead.', |
| 145 | + chain: 'resolves', |
| 146 | + }, |
| 147 | + endColumn: 52, |
| 148 | + column: 44, |
| 149 | + }, |
| 150 | + ], |
| 151 | + }, |
| 152 | + { |
| 153 | + code: 'expect(Promise.resolve({})).rejects.toBeFalsy()', |
| 154 | + options: [{ toBeFalsy: null }], |
| 155 | + errors: [ |
| 156 | + { |
| 157 | + messageId: 'bannedChain', |
| 158 | + data: { |
| 159 | + message: null, |
| 160 | + chain: 'toBeFalsy', |
| 161 | + }, |
| 162 | + endColumn: 46, |
| 163 | + column: 37, |
| 164 | + }, |
| 165 | + ], |
| 166 | + }, |
| 167 | + { |
| 168 | + code: "expect(uploadFileMock).not.toHaveBeenCalledWith('file.name')", |
| 169 | + options: [ |
| 170 | + { 'not.toHaveBeenCalledWith': 'Use not.toHaveBeenCalled instead' }, |
| 171 | + ], |
| 172 | + errors: [ |
| 173 | + { |
| 174 | + messageId: 'bannedChainWithMessage', |
| 175 | + data: { |
| 176 | + message: 'Use not.toHaveBeenCalled instead', |
| 177 | + chain: 'not.toHaveBeenCalledWith', |
| 178 | + }, |
| 179 | + endColumn: 48, |
| 180 | + column: 24, |
| 181 | + }, |
| 182 | + ], |
| 183 | + }, |
| 184 | + ], |
| 185 | +}); |
0 commit comments