Skip to content

Commit ee218ad

Browse files
brianlmacdonaldSimenB
authored andcommittedMar 5, 2018
feat: adds no jest import rule (#95)
1 parent 6e79636 commit ee218ad

File tree

4 files changed

+134
-0
lines changed

4 files changed

+134
-0
lines changed
 

Diff for: ‎docs/rules/no-jest-import.md

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Disallow importing Jest(no-jest-import)
2+
3+
The `jest` object is automatically in scope within every test file. The methods
4+
in the `jest` object help create mocks and let you control Jest's overall
5+
behavior. It is therefore completely unnecessary to import in `jest`, as Jest
6+
doesn't export anything in the first place.
7+
8+
### Rule details
9+
10+
This rule reports on any importing of Jest.
11+
12+
To name a few: `var jest = require('jest');` `const jest = require('jest');`
13+
`import jest from 'jest';` `import {jest as test} from 'jest';`
14+
15+
There is no correct usage of this code, other than to not import `jest` in the
16+
first place.
17+
18+
## Further Reading
19+
20+
\*[The Jest Object](https://facebook.github.io/jest/docs/en/jest-object.html)

Diff for: ‎index.js

+2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const noDisabledTests = require('./rules/no-disabled-tests');
66
const noFocusedTests = require('./rules/no-focused-tests');
77
const noHooks = require('./rules/no-hooks');
88
const noIdenticalTitle = require('./rules/no-identical-title');
9+
const noJestImport = require('./rules/no-jest-import');
910
const noLargeSnapshots = require('./rules/no-large-snapshots');
1011
const noTestPrefixes = require('./rules/no-test-prefixes');
1112
const preferToBeNull = require('./rules/prefer-to-be-null');
@@ -67,6 +68,7 @@ module.exports = {
6768
'no-focused-tests': noFocusedTests,
6869
'no-hooks': noHooks,
6970
'no-identical-title': noIdenticalTitle,
71+
'no-jest-import': noJestImport,
7072
'no-large-snapshots': noLargeSnapshots,
7173
'no-test-prefixes': noTestPrefixes,
7274
'prefer-to-be-null': preferToBeNull,

Diff for: ‎rules/__tests__/no-jest-import.test.js

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
'use strict';
2+
3+
const rule = require('../no-jest-import.js');
4+
const RuleTester = require('eslint').RuleTester;
5+
const ruleTester = new RuleTester();
6+
const message = `Jest is automatically in scope. Do not import "jest", as Jest doesn't export anything.`;
7+
8+
ruleTester.run('no-jest-import', rule, {
9+
valid: [
10+
{
11+
code: 'import something from "something"',
12+
parserOptions: { sourceType: 'module' },
13+
},
14+
'require("somethingElse")',
15+
'entirelyDifferent(fn)',
16+
],
17+
invalid: [
18+
{
19+
code: 'require("jest")',
20+
errors: [
21+
{
22+
endColumn: 15,
23+
column: 9,
24+
message,
25+
},
26+
],
27+
},
28+
{
29+
code: 'import jest from "jest"',
30+
parserOptions: { sourceType: 'module' },
31+
errors: [
32+
{
33+
endColumn: 24,
34+
column: 1,
35+
message,
36+
},
37+
],
38+
},
39+
{
40+
code: 'var jest = require("jest")',
41+
errors: [
42+
{
43+
endColumn: 26,
44+
column: 20,
45+
message,
46+
},
47+
],
48+
},
49+
{
50+
code: 'import {jest as test} from "jest"',
51+
parserOptions: { sourceType: 'module' },
52+
errors: [
53+
{
54+
endColumn: 34,
55+
column: 1,
56+
message,
57+
},
58+
],
59+
},
60+
{
61+
code: 'const jest = require("jest")',
62+
parserOptions: { sourceType: 'module' },
63+
errors: [
64+
{
65+
endColumn: 28,
66+
column: 22,
67+
message,
68+
},
69+
],
70+
},
71+
],
72+
});

Diff for: ‎rules/no-jest-import.js

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
'use strict';
2+
3+
const getDocsUrl = require('./util').getDocsUrl;
4+
const getNodeName = require('./util').getNodeName;
5+
const message = `Jest is automatically in scope. Do not import "jest", as Jest doesn't export anything.`;
6+
7+
module.exports = {
8+
meta: {
9+
docs: {
10+
url: getDocsUrl(__filename),
11+
},
12+
},
13+
create(context) {
14+
return {
15+
ImportDeclaration(node) {
16+
if (node.source.value === 'jest') {
17+
context.report({
18+
node,
19+
message,
20+
});
21+
}
22+
},
23+
CallExpression(node) {
24+
const calleeName = getNodeName(node.callee);
25+
if (calleeName === 'require' && node.arguments[0].value === 'jest') {
26+
context.report({
27+
loc: {
28+
end: {
29+
column: node.arguments[0].loc.end.column,
30+
line: node.arguments[0].loc.end.line,
31+
},
32+
start: node.arguments[0].loc.start,
33+
},
34+
message,
35+
});
36+
}
37+
},
38+
};
39+
},
40+
};

0 commit comments

Comments
 (0)
Please sign in to comment.