Skip to content

Commit

Permalink
feat: adds no jest import rule (#95)
Browse files Browse the repository at this point in the history
  • Loading branch information
brianlmacdonald authored and SimenB committed Mar 5, 2018
1 parent 6e79636 commit ee218ad
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 0 deletions.
20 changes: 20 additions & 0 deletions docs/rules/no-jest-import.md
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)
2 changes: 2 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const noDisabledTests = require('./rules/no-disabled-tests');
const noFocusedTests = require('./rules/no-focused-tests');
const noHooks = require('./rules/no-hooks');
const noIdenticalTitle = require('./rules/no-identical-title');
const noJestImport = require('./rules/no-jest-import');
const noLargeSnapshots = require('./rules/no-large-snapshots');
const noTestPrefixes = require('./rules/no-test-prefixes');
const preferToBeNull = require('./rules/prefer-to-be-null');
Expand Down Expand Up @@ -67,6 +68,7 @@ module.exports = {
'no-focused-tests': noFocusedTests,
'no-hooks': noHooks,
'no-identical-title': noIdenticalTitle,
'no-jest-import': noJestImport,
'no-large-snapshots': noLargeSnapshots,
'no-test-prefixes': noTestPrefixes,
'prefer-to-be-null': preferToBeNull,
Expand Down
72 changes: 72 additions & 0 deletions rules/__tests__/no-jest-import.test.js
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,
},
],
},
],
});
40 changes: 40 additions & 0 deletions rules/no-jest-import.js
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,
});
}
},
};
},
};

0 comments on commit ee218ad

Please sign in to comment.