-
Notifications
You must be signed in to change notification settings - Fork 237
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: adds no jest import rule (#95)
- Loading branch information
1 parent
6e79636
commit ee218ad
Showing
4 changed files
with
134 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# Disallow importing Jest(no-jest-import) | ||
|
||
The `jest` object is automatically in scope within every test file. The methods | ||
in the `jest` object help create mocks and let you control Jest's overall | ||
behavior. It is therefore completely unnecessary to import in `jest`, as Jest | ||
doesn't export anything in the first place. | ||
|
||
### Rule details | ||
|
||
This rule reports on any importing of Jest. | ||
|
||
To name a few: `var jest = require('jest');` `const jest = require('jest');` | ||
`import jest from 'jest';` `import {jest as test} from 'jest';` | ||
|
||
There is no correct usage of this code, other than to not import `jest` in the | ||
first place. | ||
|
||
## Further Reading | ||
|
||
\*[The Jest Object](https://facebook.github.io/jest/docs/en/jest-object.html) |
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,72 @@ | ||
'use strict'; | ||
|
||
const rule = require('../no-jest-import.js'); | ||
const RuleTester = require('eslint').RuleTester; | ||
const ruleTester = new RuleTester(); | ||
const message = `Jest is automatically in scope. Do not import "jest", as Jest doesn't export anything.`; | ||
|
||
ruleTester.run('no-jest-import', rule, { | ||
valid: [ | ||
{ | ||
code: 'import something from "something"', | ||
parserOptions: { sourceType: 'module' }, | ||
}, | ||
'require("somethingElse")', | ||
'entirelyDifferent(fn)', | ||
], | ||
invalid: [ | ||
{ | ||
code: 'require("jest")', | ||
errors: [ | ||
{ | ||
endColumn: 15, | ||
column: 9, | ||
message, | ||
}, | ||
], | ||
}, | ||
{ | ||
code: 'import jest from "jest"', | ||
parserOptions: { sourceType: 'module' }, | ||
errors: [ | ||
{ | ||
endColumn: 24, | ||
column: 1, | ||
message, | ||
}, | ||
], | ||
}, | ||
{ | ||
code: 'var jest = require("jest")', | ||
errors: [ | ||
{ | ||
endColumn: 26, | ||
column: 20, | ||
message, | ||
}, | ||
], | ||
}, | ||
{ | ||
code: 'import {jest as test} from "jest"', | ||
parserOptions: { sourceType: 'module' }, | ||
errors: [ | ||
{ | ||
endColumn: 34, | ||
column: 1, | ||
message, | ||
}, | ||
], | ||
}, | ||
{ | ||
code: 'const jest = require("jest")', | ||
parserOptions: { sourceType: 'module' }, | ||
errors: [ | ||
{ | ||
endColumn: 28, | ||
column: 22, | ||
message, | ||
}, | ||
], | ||
}, | ||
], | ||
}); |
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 @@ | ||
'use strict'; | ||
|
||
const getDocsUrl = require('./util').getDocsUrl; | ||
const getNodeName = require('./util').getNodeName; | ||
const message = `Jest is automatically in scope. Do not import "jest", as Jest doesn't export anything.`; | ||
|
||
module.exports = { | ||
meta: { | ||
docs: { | ||
url: getDocsUrl(__filename), | ||
}, | ||
}, | ||
create(context) { | ||
return { | ||
ImportDeclaration(node) { | ||
if (node.source.value === 'jest') { | ||
context.report({ | ||
node, | ||
message, | ||
}); | ||
} | ||
}, | ||
CallExpression(node) { | ||
const calleeName = getNodeName(node.callee); | ||
if (calleeName === 'require' && node.arguments[0].value === 'jest') { | ||
context.report({ | ||
loc: { | ||
end: { | ||
column: node.arguments[0].loc.end.column, | ||
line: node.arguments[0].loc.end.line, | ||
}, | ||
start: node.arguments[0].loc.start, | ||
}, | ||
message, | ||
}); | ||
} | ||
}, | ||
}; | ||
}, | ||
}; |