@@ -9,6 +9,17 @@ import type {TransformOptions as BabelTransformOptions} from '@babel/core';
9
9
import type { SyncTransformer , TransformOptions } from '@jest/transform' ;
10
10
import babelJest from '../index' ;
11
11
12
+ // This is loaded by /jest.globalSetup.mjs. We need to pre-load it there
13
+ // because this test file is being transpiled, leading to require(esm), which
14
+ // we don't support yet.
15
+ declare global {
16
+ var preloadedDependencies : {
17
+ 'babel-jest' : {
18
+ '@babel-8/core' : typeof import ( '@babel-8/core' ) ;
19
+ } ;
20
+ } ;
21
+ }
22
+
12
23
const { getCacheKey} =
13
24
babelJest . createTransformer ( ) as SyncTransformer < BabelTransformOptions > ;
14
25
@@ -33,173 +44,179 @@ afterEach(() => {
33
44
}
34
45
} ) ;
35
46
36
- describe ( 'getCacheKey' , ( ) => {
37
- const sourceText = 'mock source' ;
38
- const sourcePath = 'mock-source-path.js' ;
39
-
40
- const transformOptions = {
41
- config : { rootDir : 'mock-root-dir' } ,
42
- configString : 'mock-config-string' ,
43
- instrument : true ,
44
- } as TransformOptions < BabelTransformOptions > ;
45
-
46
- const oldCacheKey = getCacheKey ! ( sourceText , sourcePath , transformOptions ) ;
47
-
48
- test ( 'returns cache key hash' , ( ) => {
49
- expect ( oldCacheKey ) . toHaveLength ( 32 ) ;
50
- } ) ;
47
+ describe . each ( [
48
+ { version : '7' , babel : require ( '@babel/core' ) } ,
49
+ { version : '8' , babel : globalThis . preloadedDependencies [ 'babel-jest' ] [ '@babel-8/core' ] , }
50
+ ] ) ( `babel $version` , ( { babel } ) => {
51
+ describe ( 'getCacheKey' , ( ) => {
52
+ const sourceText = 'mock source' ;
53
+ const sourcePath = 'mock-source-path.js' ;
51
54
52
- test ( 'if `THIS_FILE` value is changing' , async ( ) => {
53
- jest . doMock ( 'graceful-fs' , ( ) => ( {
54
- readFileSync : ( ) => 'new this file' ,
55
- } ) ) ;
56
-
57
- const { createTransformer} =
58
- require ( '../index' ) as typeof import ( '../index' ) ;
59
-
60
- const newCacheKey = ( await createTransformer ( ) ) . getCacheKey ! (
61
- sourceText ,
62
- sourcePath ,
63
- transformOptions ,
64
- ) ;
65
-
66
- expect ( oldCacheKey ) . not . toEqual ( newCacheKey ) ;
67
- } ) ;
55
+ const transformOptions = {
56
+ config : { rootDir : 'mock-root-dir' } ,
57
+ configString : 'mock-config-string' ,
58
+ instrument : true ,
59
+ } as TransformOptions < BabelTransformOptions > ;
68
60
69
- test ( 'if `babelOptions.options` value is changing' , async ( ) => {
70
- jest . doMock ( '../loadBabelConfig' , ( ) => {
71
- const babel = require ( '@babel/core' ) as typeof import ( '@babel/core' ) ;
61
+ const oldCacheKey = getCacheKey ! ( sourceText , sourcePath , transformOptions ) ;
72
62
73
- return {
74
- loadPartialConfig : ( options : BabelTransformOptions ) => ( {
75
- ...babel . loadPartialConfig ( options ) ,
76
- options : 'new-options' ,
77
- } ) ,
78
- } ;
63
+ test ( 'returns cache key hash' , ( ) => {
64
+ expect ( oldCacheKey ) . toHaveLength ( 32 ) ;
79
65
} ) ;
80
66
81
- const { createTransformer} =
82
- require ( '../index' ) as typeof import ( '../index' ) ;
67
+ test ( 'if `THIS_FILE` value is changing' , async ( ) => {
68
+ jest . doMock ( 'graceful-fs' , ( ) => ( {
69
+ readFileSync : ( ) => 'new this file' ,
70
+ } ) ) ;
83
71
84
- const newCacheKey = ( await createTransformer ( ) ) . getCacheKey ! (
85
- sourceText ,
86
- sourcePath ,
87
- transformOptions ,
88
- ) ;
72
+ const { createTransformer} =
73
+ require ( '../index' ) as typeof import ( '../index' ) ;
89
74
90
- expect ( oldCacheKey ) . not . toEqual ( newCacheKey ) ;
91
- } ) ;
92
-
93
- test ( 'if `sourceText` value is changing' , ( ) => {
94
- const newCacheKey = getCacheKey ! (
95
- 'new source text' ,
96
- sourcePath ,
97
- transformOptions ,
98
- ) ;
99
-
100
- expect ( oldCacheKey ) . not . toEqual ( newCacheKey ) ;
101
- } ) ;
75
+ const newCacheKey = ( await createTransformer ( ) ) . getCacheKey ! (
76
+ sourceText ,
77
+ sourcePath ,
78
+ transformOptions ,
79
+ ) ;
102
80
103
- test ( 'if `sourcePath` value is changing' , ( ) => {
104
- const newCacheKey = getCacheKey ! (
105
- sourceText ,
106
- 'new-source-path.js' ,
107
- transformOptions ,
108
- ) ;
109
-
110
- expect ( oldCacheKey ) . not . toEqual ( newCacheKey ) ;
111
- } ) ;
112
-
113
- test ( 'if `configString` value is changing' , ( ) => {
114
- const newCacheKey = getCacheKey ! ( sourceText , sourcePath , {
115
- ...transformOptions ,
116
- configString : 'new-config-string' ,
81
+ expect ( oldCacheKey ) . not . toEqual ( newCacheKey ) ;
117
82
} ) ;
118
83
119
- expect ( oldCacheKey ) . not . toEqual ( newCacheKey ) ;
120
- } ) ;
84
+ test ( 'if `babelOptions.options` value is changing' , async ( ) => {
85
+ jest . doMock ( '../babel' , ( ) => {
86
+ return {
87
+ ...babel ,
88
+ loadPartialConfigSync : (
89
+ options : Parameters < typeof babel . loadPartialConfigSync > [ 0 ] ,
90
+ ) => ( {
91
+ ...babel . loadPartialConfigSync ( options ) ,
92
+ options : 'new-options' ,
93
+ } ) ,
94
+ } ;
95
+ } ) ;
96
+
97
+ const { createTransformer} =
98
+ require ( '../index' ) as typeof import ( '../index' ) ;
99
+
100
+ const newCacheKey = ( await createTransformer ( ) ) . getCacheKey ! (
101
+ sourceText ,
102
+ sourcePath ,
103
+ transformOptions ,
104
+ ) ;
105
+
106
+ expect ( oldCacheKey ) . not . toEqual ( newCacheKey ) ;
107
+ } ) ;
121
108
122
- test ( 'if `babelOptions.config` value is changing' , async ( ) => {
123
- jest . doMock ( '../loadBabelConfig' , ( ) => {
124
- const babel = require ( '@babel/core' ) as typeof import ( '@babel/core' ) ;
109
+ test ( 'if `sourceText` value is changing' , ( ) => {
110
+ const newCacheKey = getCacheKey ! (
111
+ 'new source text' ,
112
+ sourcePath ,
113
+ transformOptions ,
114
+ ) ;
125
115
126
- return {
127
- loadPartialConfig : ( options : BabelTransformOptions ) => ( {
128
- ...babel . loadPartialConfig ( options ) ,
129
- config : 'new-config' ,
130
- } ) ,
131
- } ;
116
+ expect ( oldCacheKey ) . not . toEqual ( newCacheKey ) ;
132
117
} ) ;
133
118
134
- const { createTransformer} =
135
- require ( '../index' ) as typeof import ( '../index' ) ;
136
-
137
- const newCacheKey = ( await createTransformer ( ) ) . getCacheKey ! (
138
- sourceText ,
139
- sourcePath ,
140
- transformOptions ,
141
- ) ;
119
+ test ( 'if `sourcePath` value is changing' , ( ) => {
120
+ const newCacheKey = getCacheKey ! (
121
+ sourceText ,
122
+ 'new-source-path.js' ,
123
+ transformOptions ,
124
+ ) ;
142
125
143
- expect ( oldCacheKey ) . not . toEqual ( newCacheKey ) ;
144
- } ) ;
126
+ expect ( oldCacheKey ) . not . toEqual ( newCacheKey ) ;
127
+ } ) ;
145
128
146
- test ( 'if `babelOptions.babelrc` value is changing' , async ( ) => {
147
- jest . doMock ( '../loadBabelConfig' , ( ) => {
148
- const babel = require ( '@babel/core' ) as typeof import ( '@babel/core' ) ;
129
+ test ( 'if `configString` value is changing' , ( ) => {
130
+ const newCacheKey = getCacheKey ! ( sourceText , sourcePath , {
131
+ ...transformOptions ,
132
+ configString : 'new-config-string' ,
133
+ } ) ;
149
134
150
- return {
151
- loadPartialConfig : ( options : BabelTransformOptions ) => ( {
152
- ...babel . loadPartialConfig ( options ) ,
153
- babelrc : 'new-babelrc' ,
154
- } ) ,
155
- } ;
135
+ expect ( oldCacheKey ) . not . toEqual ( newCacheKey ) ;
156
136
} ) ;
157
137
158
- const { createTransformer} =
159
- require ( '../index' ) as typeof import ( '../index' ) ;
138
+ test ( 'if `babelOptions.config` value is changing' , async ( ) => {
139
+ jest . doMock ( '../babel' , ( ) => {
140
+ return {
141
+ ...babel ,
142
+ loadPartialConfigSync : (
143
+ options : Parameters < typeof babel . loadPartialConfigSync > [ 0 ] ,
144
+ ) => ( {
145
+ ...babel . loadPartialConfigSync ( options ) ,
146
+ config : 'new-config' ,
147
+ } ) ,
148
+ } ;
149
+ } ) ;
150
+
151
+ const { createTransformer} =
152
+ require ( '../index' ) as typeof import ( '../index' ) ;
153
+
154
+ const newCacheKey = ( await createTransformer ( ) ) . getCacheKey ! (
155
+ sourceText ,
156
+ sourcePath ,
157
+ transformOptions ,
158
+ ) ;
159
+
160
+ expect ( oldCacheKey ) . not . toEqual ( newCacheKey ) ;
161
+ } ) ;
160
162
161
- const newCacheKey = ( await createTransformer ( ) ) . getCacheKey ! (
162
- sourceText ,
163
- sourcePath ,
164
- transformOptions ,
165
- ) ;
163
+ test ( 'if `babelOptions.babelrc` value is changing' , async ( ) => {
164
+ jest . doMock ( '../babel' , ( ) => {
165
+ return {
166
+ ...babel ,
167
+ loadPartialConfig : ( options : Parameters < typeof babel . loadPartialConfig > [ 0 ] ) => ( {
168
+ ...babel . loadPartialConfig ( options ) ,
169
+ babelrc : 'new-babelrc' ,
170
+ } ) ,
171
+ } ;
172
+ } ) ;
173
+
174
+ const { createTransformer} =
175
+ require ( '../index' ) as typeof import ( '../index' ) ;
176
+
177
+ const newCacheKey = ( await createTransformer ( ) ) . getCacheKey ! (
178
+ sourceText ,
179
+ sourcePath ,
180
+ transformOptions ,
181
+ ) ;
182
+
183
+ expect ( oldCacheKey ) . not . toEqual ( newCacheKey ) ;
184
+ } ) ;
166
185
167
- expect ( oldCacheKey ) . not . toEqual ( newCacheKey ) ;
168
- } ) ;
186
+ test ( 'if `instrument` value is changing' , ( ) => {
187
+ const newCacheKey = getCacheKey ! ( sourceText , sourcePath , {
188
+ ...transformOptions ,
189
+ instrument : false ,
190
+ } ) ;
169
191
170
- test ( 'if `instrument` value is changing' , ( ) => {
171
- const newCacheKey = getCacheKey ! ( sourceText , sourcePath , {
172
- ...transformOptions ,
173
- instrument : false ,
192
+ expect ( oldCacheKey ) . not . toEqual ( newCacheKey ) ;
174
193
} ) ;
175
194
176
- expect ( oldCacheKey ) . not . toEqual ( newCacheKey ) ;
177
- } ) ;
178
-
179
- test ( 'if `process.env.NODE_ENV` value is changing' , ( ) => {
180
- process . env . NODE_ENV = 'NEW_NODE_ENV' ;
195
+ test ( 'if `process.env.NODE_ENV` value is changing' , ( ) => {
196
+ process . env . NODE_ENV = 'NEW_NODE_ENV' ;
181
197
182
- const newCacheKey = getCacheKey ! ( sourceText , sourcePath , transformOptions ) ;
198
+ const newCacheKey = getCacheKey ! ( sourceText , sourcePath , transformOptions ) ;
183
199
184
- expect ( oldCacheKey ) . not . toEqual ( newCacheKey ) ;
185
- } ) ;
200
+ expect ( oldCacheKey ) . not . toEqual ( newCacheKey ) ;
201
+ } ) ;
186
202
187
- test ( 'if `process.env.BABEL_ENV` value is changing' , ( ) => {
188
- process . env . BABEL_ENV = 'NEW_BABEL_ENV' ;
203
+ test ( 'if `process.env.BABEL_ENV` value is changing' , ( ) => {
204
+ process . env . BABEL_ENV = 'NEW_BABEL_ENV' ;
189
205
190
- const newCacheKey = getCacheKey ! ( sourceText , sourcePath , transformOptions ) ;
206
+ const newCacheKey = getCacheKey ! ( sourceText , sourcePath , transformOptions ) ;
191
207
192
- expect ( oldCacheKey ) . not . toEqual ( newCacheKey ) ;
193
- } ) ;
208
+ expect ( oldCacheKey ) . not . toEqual ( newCacheKey ) ;
209
+ } ) ;
194
210
195
- test ( 'if node version is changing' , ( ) => {
196
- // @ts -expect-error: Testing purpose
197
- delete process . version ;
198
- // @ts -expect-error: Testing purpose
199
- process . version = 'new-node-version' ;
211
+ test ( 'if node version is changing' , ( ) => {
212
+ // @ts -expect-error: Testing purpose
213
+ delete process . version ;
214
+ // @ts -expect-error: Testing purpose
215
+ process . version = 'new-node-version' ;
200
216
201
- const newCacheKey = getCacheKey ! ( sourceText , sourcePath , transformOptions ) ;
217
+ const newCacheKey = getCacheKey ! ( sourceText , sourcePath , transformOptions ) ;
202
218
203
- expect ( oldCacheKey ) . not . toEqual ( newCacheKey ) ;
219
+ expect ( oldCacheKey ) . not . toEqual ( newCacheKey ) ;
220
+ } ) ;
204
221
} ) ;
205
222
} ) ;
0 commit comments