-
Notifications
You must be signed in to change notification settings - Fork 234
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(prefer-mocked): add new rule (#1470)
- Loading branch information
s.v.zaytsev
committed
May 30, 2024
1 parent
70c8c5e
commit 9195234
Showing
6 changed files
with
412 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# Prefer jest.mocked() over (fn as jest.Mock) (`prefer-mocked`) | ||
|
||
🔧 This rule is automatically fixable by the | ||
[`--fix` CLI option](https://eslint.org/docs/latest/user-guide/command-line-interface#--fix). | ||
|
||
<!-- end auto-generated rule header --> | ||
|
||
When working with mocks of functions using Jest, it's recommended to use the | ||
jest.mocked helper function to properly type the mocked functions. This rule | ||
enforces the use of jest.mocked for better type safety and readability. | ||
|
||
## Rule details | ||
|
||
The following patterns are warnings: | ||
|
||
```typescript | ||
(foo as jest.Mock).mockReturnValue(1); | ||
const mock = (foo as jest.Mock).mockReturnValue(1); | ||
(foo as unknown as jest.Mock).mockReturnValue(1); | ||
(Obj.foo as jest.Mock).mockReturnValue(1); | ||
([].foo as jest.Mock).mockReturnValue(1); | ||
``` | ||
|
||
The following patterns are not warnings: | ||
|
||
```js | ||
jest.mocked(foo).mockReturnValue(1); | ||
const mock = jest.mocked(foo).mockReturnValue(1); | ||
jest.mocked(Obj.foo).mockReturnValue(1); | ||
jest.mocked([].foo).mockReturnValue(1); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,321 @@ | ||
import path from 'path'; | ||
import dedent from 'dedent'; | ||
import rule from '../prefer-mocked'; | ||
import { FlatCompatRuleTester as RuleTester } from './test-utils'; | ||
|
||
function getFixturesRootDir(): string { | ||
return path.join(__dirname, 'fixtures'); | ||
} | ||
|
||
const rootPath = getFixturesRootDir(); | ||
|
||
const ruleTester = new RuleTester({ | ||
parser: require.resolve('@typescript-eslint/parser'), | ||
parserOptions: { | ||
sourceType: 'module', | ||
tsconfigRootDir: rootPath, | ||
project: './tsconfig.json', | ||
}, | ||
}); | ||
|
||
ruleTester.run('prefer-mocked', rule, { | ||
valid: [ | ||
dedent` | ||
import { foo } from './foo'; | ||
foo(); | ||
`, | ||
|
||
dedent` | ||
import { foo } from './foo'; | ||
jest.mocked(foo).mockReturnValue(1); | ||
`, | ||
|
||
dedent` | ||
import { bar } from './bar'; | ||
bar.mockReturnValue(1); | ||
`, | ||
|
||
dedent` | ||
import { foo } from './foo'; | ||
sinon.stub(foo).returns(1); | ||
`, | ||
|
||
dedent` | ||
import { foo } from './foo'; | ||
foo.mockImplementation(() => 1); | ||
`, | ||
|
||
dedent` | ||
const obj = { foo() {} }; | ||
obj.foo(); | ||
`, | ||
|
||
dedent` | ||
const mockFn = jest.fn(); | ||
mockFn.mockReturnValue(1); | ||
`, | ||
|
||
dedent` | ||
const arr = [() => {}]; | ||
arr[0](); | ||
`, | ||
|
||
dedent` | ||
const obj = { foo() {} }; | ||
obj.foo.mockReturnValue(1); | ||
`, | ||
|
||
dedent` | ||
const obj = { foo() {} }; | ||
jest.spyOn(obj, 'foo').mockReturnValue(1); | ||
`, | ||
|
||
dedent` | ||
type MockType = jest.Mock; | ||
const mockFn = jest.fn(); | ||
(mockFn as MockType).mockReturnValue(1); | ||
`, | ||
], | ||
invalid: [ | ||
{ | ||
code: dedent` | ||
import { foo } from './foo'; | ||
(foo as jest.Mock).mockReturnValue(1); | ||
`, | ||
output: dedent` | ||
import { foo } from './foo'; | ||
(jest.mocked(foo)).mockReturnValue(1); | ||
`, | ||
options: [], | ||
errors: [ | ||
{ | ||
messageId: 'useJestMocked', | ||
column: 2, | ||
line: 3, | ||
}, | ||
], | ||
}, | ||
{ | ||
code: dedent` | ||
import { foo } from './foo'; | ||
(foo as jest.Mock).mockImplementation(1); | ||
`, | ||
output: dedent` | ||
import { foo } from './foo'; | ||
(jest.mocked(foo)).mockImplementation(1); | ||
`, | ||
options: [], | ||
errors: [ | ||
{ | ||
messageId: 'useJestMocked', | ||
column: 2, | ||
line: 3, | ||
}, | ||
], | ||
}, | ||
{ | ||
code: dedent` | ||
import { foo } from './foo'; | ||
(foo as unknown as jest.Mock).mockReturnValue(1); | ||
`, | ||
output: dedent` | ||
import { foo } from './foo'; | ||
(jest.mocked(foo)).mockReturnValue(1); | ||
`, | ||
options: [], | ||
errors: [ | ||
{ | ||
messageId: 'useJestMocked', | ||
column: 2, | ||
line: 3, | ||
}, | ||
], | ||
}, | ||
{ | ||
code: dedent` | ||
import { Obj } from './foo'; | ||
(Obj.foo as jest.Mock).mockReturnValue(1); | ||
`, | ||
output: dedent` | ||
import { Obj } from './foo'; | ||
(jest.mocked(Obj.foo)).mockReturnValue(1); | ||
`, | ||
options: [], | ||
errors: [ | ||
{ | ||
messageId: 'useJestMocked', | ||
column: 2, | ||
line: 3, | ||
}, | ||
], | ||
}, | ||
{ | ||
code: dedent` | ||
([].foo as jest.Mock).mockReturnValue(1); | ||
`, | ||
output: dedent` | ||
(jest.mocked([].foo)).mockReturnValue(1); | ||
`, | ||
options: [], | ||
errors: [ | ||
{ | ||
messageId: 'useJestMocked', | ||
column: 2, | ||
line: 1, | ||
}, | ||
], | ||
}, | ||
{ | ||
code: dedent` | ||
import { foo } from './foo'; | ||
(foo as jest.MockedFunction).mockReturnValue(1); | ||
`, | ||
output: dedent` | ||
import { foo } from './foo'; | ||
(jest.mocked(foo)).mockReturnValue(1); | ||
`, | ||
options: [], | ||
errors: [ | ||
{ | ||
messageId: 'useJestMocked', | ||
column: 2, | ||
line: 3, | ||
}, | ||
], | ||
}, | ||
{ | ||
code: dedent` | ||
import { foo } from './foo'; | ||
(foo as jest.MockedFunction).mockImplementation(1); | ||
`, | ||
output: dedent` | ||
import { foo } from './foo'; | ||
(jest.mocked(foo)).mockImplementation(1); | ||
`, | ||
options: [], | ||
errors: [ | ||
{ | ||
messageId: 'useJestMocked', | ||
column: 2, | ||
line: 3, | ||
}, | ||
], | ||
}, | ||
{ | ||
code: dedent` | ||
import { foo } from './foo'; | ||
(foo as unknown as jest.MockedFunction).mockReturnValue(1); | ||
`, | ||
output: dedent` | ||
import { foo } from './foo'; | ||
(jest.mocked(foo)).mockReturnValue(1); | ||
`, | ||
options: [], | ||
errors: [ | ||
{ | ||
messageId: 'useJestMocked', | ||
column: 2, | ||
line: 3, | ||
}, | ||
], | ||
}, | ||
{ | ||
code: dedent` | ||
import { Obj } from './foo'; | ||
(Obj.foo as jest.MockedFunction).mockReturnValue(1); | ||
`, | ||
output: dedent` | ||
import { Obj } from './foo'; | ||
(jest.mocked(Obj.foo)).mockReturnValue(1); | ||
`, | ||
options: [], | ||
errors: [ | ||
{ | ||
messageId: 'useJestMocked', | ||
column: 2, | ||
line: 3, | ||
}, | ||
], | ||
}, | ||
{ | ||
code: dedent` | ||
(new Array(0).fill(null).foo as jest.MockedFunction).mockReturnValue(1); | ||
`, | ||
output: dedent` | ||
(jest.mocked(new Array(0).fill(null).foo)).mockReturnValue(1); | ||
`, | ||
options: [], | ||
errors: [ | ||
{ | ||
messageId: 'useJestMocked', | ||
column: 2, | ||
line: 1, | ||
}, | ||
], | ||
}, | ||
{ | ||
code: dedent` | ||
(jest.fn(() => foo) as jest.MockedFunction).mockReturnValue(1); | ||
`, | ||
output: dedent` | ||
(jest.mocked(jest.fn(() => foo))).mockReturnValue(1); | ||
`, | ||
options: [], | ||
errors: [ | ||
{ | ||
messageId: 'useJestMocked', | ||
column: 2, | ||
line: 1, | ||
}, | ||
], | ||
}, | ||
{ | ||
code: dedent` | ||
const mockedUseFocused = useFocused as jest.MockedFunction<typeof useFocused>; | ||
`, | ||
output: dedent` | ||
const mockedUseFocused = jest.mocked(useFocused); | ||
`, | ||
options: [], | ||
errors: [ | ||
{ | ||
messageId: 'useJestMocked', | ||
column: 26, | ||
line: 1, | ||
}, | ||
], | ||
}, | ||
{ | ||
code: dedent` | ||
const filter = (MessageService.getMessage as jest.Mock).mock.calls[0][0]; | ||
`, | ||
output: dedent` | ||
const filter = (jest.mocked(MessageService.getMessage)).mock.calls[0][0]; | ||
`, | ||
options: [], | ||
errors: [ | ||
{ | ||
messageId: 'useJestMocked', | ||
column: 17, | ||
line: 1, | ||
}, | ||
], | ||
}, | ||
], | ||
}); |
Oops, something went wrong.