|
| 1 | +import { TSESLint } from '@typescript-eslint/utils'; |
| 2 | +import dedent from 'dedent'; |
| 3 | +import rule from '../no-restricted-jest-methods'; |
| 4 | +import { espreeParser } from './test-utils'; |
| 5 | + |
| 6 | +const ruleTester = new TSESLint.RuleTester({ |
| 7 | + parser: espreeParser, |
| 8 | + parserOptions: { |
| 9 | + ecmaVersion: 2017, |
| 10 | + }, |
| 11 | +}); |
| 12 | + |
| 13 | +ruleTester.run('no-restricted-jest-methods', rule, { |
| 14 | + valid: [ |
| 15 | + 'jest', |
| 16 | + 'jest.mock()', |
| 17 | + 'expect(a).rejects;', |
| 18 | + 'expect(a);', |
| 19 | + { |
| 20 | + code: dedent` |
| 21 | + import { jest } from '@jest/globals'; |
| 22 | +
|
| 23 | + jest; |
| 24 | + `, |
| 25 | + parserOptions: { sourceType: 'module' }, |
| 26 | + }, |
| 27 | + ], |
| 28 | + invalid: [ |
| 29 | + { |
| 30 | + code: 'jest.fn()', |
| 31 | + options: [{ fn: null }], |
| 32 | + errors: [ |
| 33 | + { |
| 34 | + messageId: 'restrictedJestMethod', |
| 35 | + data: { |
| 36 | + message: null, |
| 37 | + restriction: 'fn', |
| 38 | + }, |
| 39 | + column: 6, |
| 40 | + line: 1, |
| 41 | + }, |
| 42 | + ], |
| 43 | + }, |
| 44 | + { |
| 45 | + code: 'jest["fn"]()', |
| 46 | + options: [{ fn: null }], |
| 47 | + errors: [ |
| 48 | + { |
| 49 | + messageId: 'restrictedJestMethod', |
| 50 | + data: { |
| 51 | + message: null, |
| 52 | + restriction: 'fn', |
| 53 | + }, |
| 54 | + column: 6, |
| 55 | + line: 1, |
| 56 | + }, |
| 57 | + ], |
| 58 | + }, |
| 59 | + { |
| 60 | + code: 'jest.mock()', |
| 61 | + options: [{ mock: 'Do not use mocks' }], |
| 62 | + errors: [ |
| 63 | + { |
| 64 | + messageId: 'restrictedJestMethodWithMessage', |
| 65 | + data: { |
| 66 | + message: 'Do not use mocks', |
| 67 | + restriction: 'mock', |
| 68 | + }, |
| 69 | + column: 6, |
| 70 | + line: 1, |
| 71 | + }, |
| 72 | + ], |
| 73 | + }, |
| 74 | + { |
| 75 | + code: 'jest["mock"]()', |
| 76 | + options: [{ mock: 'Do not use mocks' }], |
| 77 | + errors: [ |
| 78 | + { |
| 79 | + messageId: 'restrictedJestMethodWithMessage', |
| 80 | + data: { |
| 81 | + message: 'Do not use mocks', |
| 82 | + restriction: 'mock', |
| 83 | + }, |
| 84 | + column: 6, |
| 85 | + line: 1, |
| 86 | + }, |
| 87 | + ], |
| 88 | + }, |
| 89 | + { |
| 90 | + code: dedent` |
| 91 | + import { jest } from '@jest/globals'; |
| 92 | +
|
| 93 | + jest.advanceTimersByTime(); |
| 94 | + `, |
| 95 | + options: [{ advanceTimersByTime: null }], |
| 96 | + parserOptions: { sourceType: 'module' }, |
| 97 | + errors: [ |
| 98 | + { |
| 99 | + messageId: 'restrictedJestMethod', |
| 100 | + data: { |
| 101 | + message: null, |
| 102 | + restriction: 'advanceTimersByTime', |
| 103 | + }, |
| 104 | + column: 6, |
| 105 | + line: 3, |
| 106 | + }, |
| 107 | + ], |
| 108 | + }, |
| 109 | + ], |
| 110 | +}); |
0 commit comments