1- import { TSESLint } from '@typescript-eslint/utils' ;
1+ import type { TSESLint } from '@typescript-eslint/utils' ;
22import rule from '../no-deprecated-functions' ;
3- import {
4- type JestVersion ,
5- detectJestVersion ,
6- } from '../utils/detectJestVersion' ;
7- import {
8- FlatCompatRuleTester as RuleTester ,
9- usingFlatConfig ,
10- } from './test-utils' ;
11-
12- jest . mock ( '../utils/detectJestVersion' ) ;
13-
14- const detectJestVersionMock = detectJestVersion as jest . MockedFunction <
15- typeof detectJestVersion
3+ import { type JestVersion , getJestVersion } from '../utils/detectJestVersion' ;
4+ import { FlatCompatRuleTester as RuleTester } from './test-utils' ;
5+
6+ jest . mock ( '../utils/detectJestVersion' , ( ) => {
7+ const actual = jest . requireActual ( '../utils/detectJestVersion' ) ;
8+
9+ jest . spyOn ( actual , 'getJestVersion' ) ;
10+
11+ return actual ;
12+ } ) ;
13+
14+ const getJestVersionMock = getJestVersion as jest . MockedFunction <
15+ typeof getJestVersion
1616> ;
1717
1818const ruleTester = new RuleTester ( ) ;
@@ -60,6 +60,31 @@ const generateInvalidCases = (
6060 ] ;
6161} ;
6262
63+ const generateNotDetectedCases = (
64+ deprecation : string ,
65+ ) : Array < TSESLint . InvalidTestCase < 'jestNotDetected' , [ ] > > => {
66+ const [ deprecatedName , deprecatedFunc ] = deprecation . split ( '.' ) ;
67+ const settings = { jest : { version : undefined } } ;
68+ const errors : [ TSESLint . TestCaseError < 'jestNotDetected' > ] = [
69+ { messageId : 'jestNotDetected' } ,
70+ ] ;
71+
72+ return [
73+ {
74+ code : `${ deprecation } ()` ,
75+ output : null ,
76+ settings,
77+ errors,
78+ } ,
79+ {
80+ code : `${ deprecatedName } ['${ deprecatedFunc } ']()` ,
81+ output : null ,
82+ settings,
83+ errors,
84+ } ,
85+ ] ;
86+ } ;
87+
6388// contains the cache-clearing beforeEach so we can test the cache too
6489describe ( 'the rule' , ( ) => {
6590 // a few sanity checks before doing our massive loop
@@ -72,7 +97,7 @@ describe('the rule', () => {
7297 ...generateValidCases ( 25 , 'jest.genMockFromModule' ) ,
7398 ...generateValidCases ( '25.1.1' , 'jest.genMockFromModule' ) ,
7499 ...generateValidCases ( '17.2' , 'require.requireActual' ) ,
75- ] ,
100+ ] . filter ( testCase => testCase . settings ?. jest ?. version ) ,
76101 invalid : [
77102 ...generateInvalidCases (
78103 21 ,
@@ -90,14 +115,14 @@ describe('the rule', () => {
90115 'jest.genMockFromModule' ,
91116 'jest.createMockFromModule' ,
92117 ) ,
93- ] ,
118+ ] . filter ( testCase => testCase . settings ?. jest ?. version ) ,
94119 } ) ;
95120
96121 describe . each < JestVersion > ( [
97122 14 , 15 , 16 , 17 , 18 , 19 , 20 , 21 , 22 , 23 , 24 , 25 , 26 , 27 ,
98123 ] ) ( 'when using jest version %i' , jestVersion => {
99124 beforeEach ( async ( ) => {
100- detectJestVersionMock . mockReturnValue ( jestVersion ) ;
125+ getJestVersionMock . mockReturnValue ( jestVersion ) ;
101126 } ) ;
102127
103128 const allowedFunctions : string [ ] = [ ] ;
@@ -147,37 +172,21 @@ describe('the rule', () => {
147172 } ) ;
148173 } ) ;
149174
150- describe ( 'when there is an error in detecting the jest version' , ( ) => {
151- beforeEach ( ( ) => {
152- detectJestVersionMock . mockImplementation ( ( ) => {
153- throw new Error ( 'oh noes!' ) ;
154- } ) ;
175+ describe ( 'when jest version not detected' , ( ) => {
176+ beforeEach ( async ( ) => {
177+ getJestVersionMock . mockReturnValue ( null ) ;
155178 } ) ;
156179
157- it ( 'bubbles the error up' , ( ) => {
158- expect ( ( ) => {
159- const linter = new TSESLint . Linter ( ) ;
160-
161- /* istanbul ignore if */
162- if ( usingFlatConfig ) {
163- linter . verify ( 'jest.resetModuleRegistry()' , [
164- {
165- plugins : {
166- jest : { rules : { 'no-deprecated-functions' : rule } } ,
167- } ,
168- rules : { 'jest/no-deprecated-functions' : 'error' } ,
169- } ,
170- ] ) ;
171-
172- return ;
173- }
174-
175- linter . defineRule ( 'no-deprecated-functions' , rule ) ;
176-
177- linter . verify ( 'jest.resetModuleRegistry()' , {
178- rules : { 'no-deprecated-functions' : 'error' } ,
179- } ) ;
180- } ) . toThrow ( 'oh noes!' ) ;
180+ ruleTester . run ( 'no jest version' , rule , {
181+ valid : [ 'jest' , 'require("fs")' ] ,
182+ invalid : [
183+ 'jest.resetModuleRegistry' ,
184+ 'jest.addMatchers' ,
185+ 'require.requireMock' ,
186+ 'require.requireActual' ,
187+ 'jest.runTimersToTime' ,
188+ 'jest.genMockFromModule' ,
189+ ] . flatMap ( generateNotDetectedCases ) ,
181190 } ) ;
182191 } ) ;
183192} ) ;
0 commit comments