-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add jest-mocked-grouped and jest-mock-grouped rules
- Loading branch information
Showing
15 changed files
with
278 additions
and
38 deletions.
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
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,40 @@ | ||
# jest-mock-grouped | ||
|
||
## Reasons to use this ESLint rule | ||
|
||
* Having a better overview of mocks | ||
* Have a more readable test file | ||
|
||
## Reasons to not use this ESLint rule | ||
|
||
* using a different way to organize the mocks (like `jest-mock-directly-above-jest-mocked`) | ||
* don't want to have a strict grouping of mocks | ||
|
||
## Correct usages | ||
|
||
|
||
**code:** | ||
``` | ||
jest.mock('../some/path/to/file'); | ||
jest.mock('../some/path/to/other/file'); | ||
``` | ||
|
||
### Incorrect usages | ||
|
||
**code:** | ||
``` | ||
jest.mock('../some/path/to/file'); | ||
// some other code | ||
// ... | ||
jest.mock('../some/path/to/other/file'); | ||
``` | ||
|
||
## Configuration | ||
|
||
This ESLint rule is pretty simple. It currently has no configuration. | ||
|
||
### Example | ||
|
||
```js | ||
'jest-mock-grouped': 'error', | ||
``` |
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
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,44 @@ | ||
/** | ||
* @author Bastian Gebhardt<[email protected]> | ||
*/ | ||
|
||
'use strict'; | ||
|
||
const { getMocks } = require('../utils/utils'); | ||
|
||
module.exports = { | ||
meta: { | ||
type: 'layout', | ||
docs: { | ||
description: 'requires all jest.mock statements to be grouped together', | ||
recommended: false, | ||
url: 'https://github.com/BuZZ-T/eslint-plugin-jest-mock-config/blob/main/docs/rules/jest-mock-grouped.md', | ||
}, | ||
messages: { | ||
jestMockShouldFollow: 'jest.mock should directly follow another jest.mock', | ||
}, | ||
schema: [], | ||
}, | ||
create: function(context) { | ||
|
||
return { | ||
Program: function(program) { | ||
const mocks = getMocks(program); | ||
|
||
const positions = mocks.map((mock) => program.body.indexOf(mock)); | ||
|
||
const notAfterMock = mocks.map((mock, index) => (index > 0 && positions[index] !== positions[index - 1] + 1) ? mock : undefined); | ||
|
||
notAfterMock.forEach((mock) => { | ||
if (mock) { | ||
context.report({ | ||
node: mock, | ||
messageId: 'jestMockShouldFollow', | ||
}); | ||
} | ||
}); | ||
} | ||
|
||
}; | ||
} | ||
}; |
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,44 @@ | ||
/** | ||
* @author Bastian Gebhardt<[email protected]> | ||
*/ | ||
|
||
'use strict'; | ||
|
||
module.exports = { | ||
meta: { | ||
type: 'layout', | ||
docs: { | ||
description: 'requires all jest.mock statements to be grouped together', | ||
recommended: false, | ||
url: 'https://github.com/BuZZ-T/eslint-plugin-jest-mock-config/blob/main/docs/rules/jest-mocked-grouped.md', | ||
}, | ||
messages: { | ||
jestMockedShouldFollow: 'jest.mocked should directly follow another jest.mocked', | ||
}, | ||
schema: [], | ||
}, | ||
create: function(context) { | ||
|
||
return { | ||
Program: function(program) { | ||
const mockedStatements = program.body.filter((node) => node.type === 'VariableDeclaration' | ||
&& node.declarations[0].init?.type === 'CallExpression' | ||
&& node.declarations[0].init.callee?.object?.name === 'jest' | ||
&& node.declarations[0].init.callee?.property?.name === 'mocked'); | ||
|
||
const positions = mockedStatements.map((mock) => program.body.indexOf(mock)); | ||
|
||
const notAfterMock = mockedStatements.map((mocked, index) => (index > 0 && positions[index] !== positions[index - 1] + 1) ? mocked : undefined); | ||
|
||
notAfterMock.forEach((mock) => { | ||
if (mock) { | ||
context.report({ | ||
node: mock, | ||
messageId: 'jestMockedShouldFollow', | ||
}); | ||
} | ||
}); | ||
} | ||
}; | ||
} | ||
}; |
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
Oops, something went wrong.