1
- import * as fs from 'fs' ;
2
- import * as os from 'os' ;
3
- import * as path from 'path' ;
4
- import { JSONSchemaForNPMPackageJsonFiles } from '@schemastore/package' ;
5
1
import { TSESLint } from '@typescript-eslint/experimental-utils' ;
6
- import rule , {
7
- JestVersion ,
8
- _clearCachedJestVersion ,
9
- } from '../no-deprecated-functions' ;
2
+ import { JestVersion , detectJestVersion } from '../detectJestVersion' ;
3
+ import rule from '../no-deprecated-functions' ;
10
4
11
- const ruleTester = new TSESLint . RuleTester ( ) ;
12
-
13
- // pin the original cwd so that we can restore it after each test
14
- const projectDir = process . cwd ( ) ;
15
-
16
- afterEach ( ( ) => process . chdir ( projectDir ) ) ;
17
-
18
- /**
19
- * Makes a new temp directory, prefixed with `eslint-plugin-jest-`
20
- *
21
- * @return {Promise<string> }
22
- */
23
- const makeTempDir = async ( ) =>
24
- fs . mkdtempSync ( path . join ( os . tmpdir ( ) , 'eslint-plugin-jest-' ) ) ;
25
-
26
- /**
27
- * Sets up a fake project with a `package.json` file located in
28
- * `node_modules/jest` whose version is set to the given `jestVersion`.
29
- *
30
- * @param {JestVersion } jestVersion
31
- *
32
- * @return {Promise<string> }
33
- */
34
- const setupFakeProjectDirectory = async (
35
- jestVersion : JestVersion ,
36
- ) : Promise < string > => {
37
- const jestPackageJson : JSONSchemaForNPMPackageJsonFiles = {
38
- name : 'jest' ,
39
- version : `${ jestVersion } .0.0` ,
40
- } ;
5
+ jest . mock ( '../detectJestVersion' ) ;
41
6
42
- const tempDir = await makeTempDir ( ) ;
43
- const jestPackagePath = path . join ( tempDir , 'node_modules' , 'jest' ) ;
7
+ const detectJestVersionMock = detectJestVersion as jest . MockedFunction <
8
+ typeof detectJestVersion
9
+ > ;
44
10
45
- // todo: remove in node@10 & replace with { recursive: true }
46
- fs . mkdirSync ( path . join ( tempDir , 'node_modules' ) ) ;
47
-
48
- fs . mkdirSync ( jestPackagePath ) ;
49
- await fs . writeFileSync (
50
- path . join ( jestPackagePath , 'package.json' ) ,
51
- JSON . stringify ( jestPackageJson ) ,
52
- ) ;
53
-
54
- return tempDir ;
55
- } ;
11
+ const ruleTester = new TSESLint . RuleTester ( ) ;
56
12
57
13
const generateValidCases = (
58
- jestVersion : JestVersion | undefined ,
14
+ jestVersion : JestVersion | string | undefined ,
59
15
functionCall : string ,
60
16
) : Array < TSESLint . ValidTestCase < never > > => {
61
17
const [ name , func ] = functionCall . split ( '.' ) ;
@@ -70,7 +26,7 @@ const generateValidCases = (
70
26
} ;
71
27
72
28
const generateInvalidCases = (
73
- jestVersion : JestVersion | undefined ,
29
+ jestVersion : JestVersion | string | undefined ,
74
30
deprecation : string ,
75
31
replacement : string ,
76
32
) : Array < TSESLint . InvalidTestCase < 'deprecatedFunction' , never > > => {
@@ -97,45 +53,18 @@ const generateInvalidCases = (
97
53
] ;
98
54
} ;
99
55
100
- describe ( 'the jest version cache' , ( ) => {
101
- beforeEach ( async ( ) => process . chdir ( await setupFakeProjectDirectory ( 17 ) ) ) ;
102
-
103
- // change the jest version *after* each test case
104
- afterEach ( async ( ) => {
105
- const jestPackageJson : JSONSchemaForNPMPackageJsonFiles = {
106
- name : 'jest' ,
107
- version : '24.0.0' ,
108
- } ;
109
-
110
- const tempDir = process . cwd ( ) ;
111
-
112
- await fs . writeFileSync (
113
- path . join ( tempDir , 'node_modules' , 'jest' , 'package.json' ) ,
114
- JSON . stringify ( jestPackageJson ) ,
115
- ) ;
116
- } ) ;
117
-
118
- ruleTester . run ( 'no-deprecated-functions' , rule , {
119
- valid : [
120
- 'require("fs")' , // this will cause jest version to be read & cached
121
- 'jest.requireActual()' , // deprecated after jest 17
122
- ] ,
123
- invalid : [ ] ,
124
- } ) ;
125
- } ) ;
126
-
127
56
// contains the cache-clearing beforeEach so we can test the cache too
128
57
describe ( 'the rule' , ( ) => {
129
- beforeEach ( ( ) => _clearCachedJestVersion ( ) ) ;
130
-
131
58
// a few sanity checks before doing our massive loop
132
59
ruleTester . run ( 'no-deprecated-functions' , rule , {
133
60
valid : [
134
- 'jest' ,
135
- 'require("fs")' ,
61
+ { settings : { jest : { version : 14 } } , code : 'jest' } ,
62
+ { settings : { jest : { version : 14 } } , code : 'require("fs")' } ,
136
63
...generateValidCases ( 14 , 'jest.resetModuleRegistry' ) ,
137
64
...generateValidCases ( 17 , 'require.requireActual' ) ,
138
65
...generateValidCases ( 25 , 'jest.genMockFromModule' ) ,
66
+ ...generateValidCases ( '25.1.1' , 'jest.genMockFromModule' ) ,
67
+ ...generateValidCases ( '17.2' , 'require.requireActual' ) ,
139
68
] ,
140
69
invalid : [
141
70
...generateInvalidCases (
@@ -149,15 +78,20 @@ describe('the rule', () => {
149
78
'jest.genMockFromModule' ,
150
79
'jest.createMockFromModule' ,
151
80
) ,
81
+ ...generateInvalidCases (
82
+ '26.0.0-next.11' ,
83
+ 'jest.genMockFromModule' ,
84
+ 'jest.createMockFromModule' ,
85
+ ) ,
152
86
] ,
153
87
} ) ;
154
88
155
89
describe . each < JestVersion > ( [
156
90
14 , 15 , 16 , 17 , 18 , 19 , 20 , 21 , 22 , 23 , 24 , 25 , 26 , 27 ,
157
91
] ) ( 'when using jest version %i' , jestVersion => {
158
- beforeEach ( async ( ) =>
159
- process . chdir ( await setupFakeProjectDirectory ( jestVersion ) ) ,
160
- ) ;
92
+ beforeEach ( async ( ) => {
93
+ detectJestVersionMock . mockReturnValue ( jestVersion ) ;
94
+ } ) ;
161
95
162
96
const allowedFunctions : string [ ] = [ ] ;
163
97
const deprecations = (
@@ -210,50 +144,23 @@ describe('the rule', () => {
210
144
} ) ;
211
145
} ) ;
212
146
213
- describe ( 'when no jest version is provided' , ( ) => {
214
- describe ( 'when the jest package.json is missing the version property' , ( ) => {
215
- beforeEach ( async ( ) => {
216
- const tempDir = await setupFakeProjectDirectory ( 1 ) ;
217
-
218
- await fs . writeFileSync (
219
- path . join ( tempDir , 'node_modules' , 'jest' , 'package.json' ) ,
220
- JSON . stringify ( { } ) ,
221
- ) ;
222
-
223
- process . chdir ( tempDir ) ;
224
- } ) ;
225
-
226
- it ( 'requires the version to be set explicitly' , ( ) => {
227
- expect ( ( ) => {
228
- const linter = new TSESLint . Linter ( ) ;
229
-
230
- linter . defineRule ( 'no-deprecated-functions' , rule ) ;
231
-
232
- linter . verify ( 'jest.resetModuleRegistry()' , {
233
- rules : { 'no-deprecated-functions' : 'error' } ,
234
- } ) ;
235
- } ) . toThrow (
236
- 'Unable to detect Jest version - please ensure jest package is installed, or otherwise set version explicitly' ,
237
- ) ;
147
+ describe ( 'when there is an error in detecting the jest version' , ( ) => {
148
+ beforeEach ( ( ) => {
149
+ detectJestVersionMock . mockImplementation ( ( ) => {
150
+ throw new Error ( 'oh noes!' ) ;
238
151
} ) ;
239
152
} ) ;
240
153
241
- describe ( 'when the jest package.json is not found' , ( ) => {
242
- beforeEach ( async ( ) => process . chdir ( await makeTempDir ( ) ) ) ;
154
+ it ( 'bubbles the error up' , ( ) => {
155
+ expect ( ( ) => {
156
+ const linter = new TSESLint . Linter ( ) ;
243
157
244
- it ( 'requires the version to be set explicitly' , ( ) => {
245
- expect ( ( ) => {
246
- const linter = new TSESLint . Linter ( ) ;
158
+ linter . defineRule ( 'no-deprecated-functions' , rule ) ;
247
159
248
- linter . defineRule ( 'no-deprecated-functions' , rule ) ;
249
-
250
- linter . verify ( 'jest.resetModuleRegistry()' , {
251
- rules : { 'no-deprecated-functions' : 'error' } ,
252
- } ) ;
253
- } ) . toThrow (
254
- 'Unable to detect Jest version - please ensure jest package is installed, or otherwise set version explicitly' ,
255
- ) ;
256
- } ) ;
160
+ linter . verify ( 'jest.resetModuleRegistry()' , {
161
+ rules : { 'no-deprecated-functions' : 'error' } ,
162
+ } ) ;
163
+ } ) . toThrow ( 'oh noes!' ) ;
257
164
} ) ;
258
165
} ) ;
259
166
} ) ;
0 commit comments