File tree 4 files changed +134
-0
lines changed
4 files changed +134
-0
lines changed Original file line number Diff line number Diff line change
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 )
Original file line number Diff line number Diff line change @@ -6,6 +6,7 @@ const noDisabledTests = require('./rules/no-disabled-tests');
6
6
const noFocusedTests = require ( './rules/no-focused-tests' ) ;
7
7
const noHooks = require ( './rules/no-hooks' ) ;
8
8
const noIdenticalTitle = require ( './rules/no-identical-title' ) ;
9
+ const noJestImport = require ( './rules/no-jest-import' ) ;
9
10
const noLargeSnapshots = require ( './rules/no-large-snapshots' ) ;
10
11
const noTestPrefixes = require ( './rules/no-test-prefixes' ) ;
11
12
const preferToBeNull = require ( './rules/prefer-to-be-null' ) ;
@@ -67,6 +68,7 @@ module.exports = {
67
68
'no-focused-tests' : noFocusedTests ,
68
69
'no-hooks' : noHooks ,
69
70
'no-identical-title' : noIdenticalTitle ,
71
+ 'no-jest-import' : noJestImport ,
70
72
'no-large-snapshots' : noLargeSnapshots ,
71
73
'no-test-prefixes' : noTestPrefixes ,
72
74
'prefer-to-be-null' : preferToBeNull ,
Original file line number Diff line number Diff line change
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
+ } ) ;
Original file line number Diff line number Diff line change
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
+ } ;
You can’t perform that action at this time.
0 commit comments