diff --git a/docs/rules/no-jest-import.md b/docs/rules/no-jest-import.md new file mode 100644 index 000000000..16e960630 --- /dev/null +++ b/docs/rules/no-jest-import.md @@ -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) diff --git a/index.js b/index.js index 4f4a5a959..c1a5ee9c0 100644 --- a/index.js +++ b/index.js @@ -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'); @@ -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, diff --git a/rules/__tests__/no-jest-import.test.js b/rules/__tests__/no-jest-import.test.js new file mode 100644 index 000000000..8ebb05d2d --- /dev/null +++ b/rules/__tests__/no-jest-import.test.js @@ -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, + }, + ], + }, + ], +}); diff --git a/rules/no-jest-import.js b/rules/no-jest-import.js new file mode 100644 index 000000000..4b8a7a153 --- /dev/null +++ b/rules/no-jest-import.js @@ -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, + }); + } + }, + }; + }, +};