@@ -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
+ // We need to use the Node.js implementation of `require` to load Babel 8
13
+ // packages, instead of our sandboxed implementation, because Babel 8 is
14
+ // written in ESM and we don't support require(esm) yet.
15
+ import Module from 'node:module' ;
16
+ import { pathToFileURL } from 'node:url' ;
17
+ import { onNodeVersions } from '@jest/test-utils' ;
18
+ const createOriginalNodeRequire = Object . getPrototypeOf ( Module ) . createRequire ;
19
+ const originalNodeRequire = createOriginalNodeRequire (
20
+ pathToFileURL ( __filename ) ,
21
+ ) ;
22
+
12
23
const { getCacheKey} =
13
24
babelJest . createTransformer ( ) as SyncTransformer < BabelTransformOptions > ;
14
25
@@ -33,173 +44,211 @@ 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
- } ) ;
51
-
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
- ) ;
47
+ describe ( 'babel 7' , ( ) => {
48
+ defineTests ( { getBabel : ( ) => require ( '@babel/core' ) } ) ;
49
+ } ) ;
65
50
66
- expect ( oldCacheKey ) . not . toEqual ( newCacheKey ) ;
51
+ describe ( 'babel 8' , ( ) => {
52
+ onNodeVersions ( '>=20' , ( ) => {
53
+ defineTests ( {
54
+ getBabel : ( ) => originalNodeRequire ( '@babel-8/core' ) ,
55
+ } ) ;
67
56
} ) ;
57
+ } ) ;
68
58
69
- test ( 'if `babelOptions.options` value is changing' , async ( ) => {
70
- jest . doMock ( '../loadBabelConfig' , ( ) => {
71
- const babel = require ( '@babel/core' ) as typeof import ( '@babel/core' ) ;
72
-
73
- return {
74
- loadPartialConfig : ( options : BabelTransformOptions ) => ( {
75
- ...babel . loadPartialConfig ( options ) ,
76
- options : 'new-options' ,
77
- } ) ,
78
- } ;
59
+ function defineTests ( {
60
+ getBabel,
61
+ } : {
62
+ getBabel : ( ) => typeof import ( '@babel-8/core' ) ;
63
+ } ) {
64
+ describe ( 'getCacheKey' , ( ) => {
65
+ let babel : typeof import ( '@babel-8/core' ) ;
66
+ beforeAll ( ( ) => {
67
+ babel = getBabel ( ) ;
79
68
} ) ;
80
69
81
- const { createTransformer } =
82
- require ( '../index' ) as typeof import ( '../index' ) ;
70
+ const sourceText = 'mock source' ;
71
+ const sourcePath = 'mock-source-path.js' ;
83
72
84
- const newCacheKey = ( await createTransformer ( ) ) . getCacheKey ! (
85
- sourceText ,
86
- sourcePath ,
87
- transformOptions ,
88
- ) ;
73
+ const transformOptions = {
74
+ config : { rootDir : 'mock-root-dir' } ,
75
+ configString : 'mock-config-string' ,
76
+ instrument : true ,
77
+ } as TransformOptions < BabelTransformOptions > ;
89
78
90
- expect ( oldCacheKey ) . not . toEqual ( newCacheKey ) ;
91
- } ) ;
79
+ const oldCacheKey = getCacheKey ! ( sourceText , sourcePath , transformOptions ) ;
92
80
93
- test ( 'if `sourceText` value is changing' , ( ) => {
94
- const newCacheKey = getCacheKey ! (
95
- 'new source text' ,
96
- sourcePath ,
97
- transformOptions ,
98
- ) ;
81
+ test ( 'returns cache key hash' , ( ) => {
82
+ expect ( oldCacheKey ) . toHaveLength ( 32 ) ;
83
+ } ) ;
99
84
100
- expect ( oldCacheKey ) . not . toEqual ( newCacheKey ) ;
101
- } ) ;
85
+ test ( 'if `THIS_FILE` value is changing' , async ( ) => {
86
+ jest . doMock ( 'graceful-fs' , ( ) => ( {
87
+ readFileSync : ( ) => 'new this file' ,
88
+ } ) ) ;
102
89
103
- test ( 'if `sourcePath` value is changing' , ( ) => {
104
- const newCacheKey = getCacheKey ! (
105
- sourceText ,
106
- 'new-source-path.js' ,
107
- transformOptions ,
108
- ) ;
90
+ const { createTransformer} =
91
+ require ( '../index' ) as typeof import ( '../index' ) ;
109
92
110
- expect ( oldCacheKey ) . not . toEqual ( newCacheKey ) ;
111
- } ) ;
93
+ const newCacheKey = ( await createTransformer ( ) ) . getCacheKey ! (
94
+ sourceText ,
95
+ sourcePath ,
96
+ transformOptions ,
97
+ ) ;
112
98
113
- test ( 'if `configString` value is changing' , ( ) => {
114
- const newCacheKey = getCacheKey ! ( sourceText , sourcePath , {
115
- ...transformOptions ,
116
- configString : 'new-config-string' ,
99
+ expect ( oldCacheKey ) . not . toEqual ( newCacheKey ) ;
117
100
} ) ;
118
101
119
- expect ( oldCacheKey ) . not . toEqual ( newCacheKey ) ;
120
- } ) ;
102
+ test ( 'if `babelOptions.options` value is changing' , async ( ) => {
103
+ jest . doMock ( '../babel' , ( ) => {
104
+ return {
105
+ ...babel ,
106
+ loadPartialConfigSync : (
107
+ options : Parameters < typeof babel . loadPartialConfigSync > [ 0 ] ,
108
+ ) => ( {
109
+ ...babel . loadPartialConfigSync ( options ) ,
110
+ options : 'new-options' ,
111
+ } ) ,
112
+ } ;
113
+ } ) ;
114
+
115
+ const { createTransformer} =
116
+ require ( '../index' ) as typeof import ( '../index' ) ;
117
+
118
+ const newCacheKey = ( await createTransformer ( ) ) . getCacheKey ! (
119
+ sourceText ,
120
+ sourcePath ,
121
+ transformOptions ,
122
+ ) ;
123
+
124
+ expect ( oldCacheKey ) . not . toEqual ( newCacheKey ) ;
125
+ } ) ;
121
126
122
- test ( 'if `babelOptions.config` value is changing' , async ( ) => {
123
- jest . doMock ( '../loadBabelConfig' , ( ) => {
124
- const babel = require ( '@babel/core' ) as typeof import ( '@babel/core' ) ;
127
+ test ( 'if `sourceText` value is changing' , ( ) => {
128
+ const newCacheKey = getCacheKey ! (
129
+ 'new source text' ,
130
+ sourcePath ,
131
+ transformOptions ,
132
+ ) ;
125
133
126
- return {
127
- loadPartialConfig : ( options : BabelTransformOptions ) => ( {
128
- ...babel . loadPartialConfig ( options ) ,
129
- config : 'new-config' ,
130
- } ) ,
131
- } ;
134
+ expect ( oldCacheKey ) . not . toEqual ( newCacheKey ) ;
132
135
} ) ;
133
136
134
- const { createTransformer} =
135
- require ( '../index' ) as typeof import ( '../index' ) ;
136
-
137
- const newCacheKey = ( await createTransformer ( ) ) . getCacheKey ! (
138
- sourceText ,
139
- sourcePath ,
140
- transformOptions ,
141
- ) ;
137
+ test ( 'if `sourcePath` value is changing' , ( ) => {
138
+ const newCacheKey = getCacheKey ! (
139
+ sourceText ,
140
+ 'new-source-path.js' ,
141
+ transformOptions ,
142
+ ) ;
142
143
143
- expect ( oldCacheKey ) . not . toEqual ( newCacheKey ) ;
144
- } ) ;
144
+ expect ( oldCacheKey ) . not . toEqual ( newCacheKey ) ;
145
+ } ) ;
145
146
146
- test ( 'if `babelOptions.babelrc` value is changing' , async ( ) => {
147
- jest . doMock ( '../loadBabelConfig' , ( ) => {
148
- const babel = require ( '@babel/core' ) as typeof import ( '@babel/core' ) ;
147
+ test ( 'if `configString` value is changing' , ( ) => {
148
+ const newCacheKey = getCacheKey ! ( sourceText , sourcePath , {
149
+ ...transformOptions ,
150
+ configString : 'new-config-string' ,
151
+ } ) ;
149
152
150
- return {
151
- loadPartialConfig : ( options : BabelTransformOptions ) => ( {
152
- ...babel . loadPartialConfig ( options ) ,
153
- babelrc : 'new-babelrc' ,
154
- } ) ,
155
- } ;
153
+ expect ( oldCacheKey ) . not . toEqual ( newCacheKey ) ;
156
154
} ) ;
157
155
158
- const { createTransformer} =
159
- require ( '../index' ) as typeof import ( '../index' ) ;
156
+ test ( 'if `babelOptions.config` value is changing' , async ( ) => {
157
+ jest . doMock ( '../babel' , ( ) => {
158
+ return {
159
+ ...babel ,
160
+ loadPartialConfigSync : (
161
+ options : Parameters < typeof babel . loadPartialConfigSync > [ 0 ] ,
162
+ ) => ( {
163
+ ...babel . loadPartialConfigSync ( options ) ,
164
+ config : 'new-config' ,
165
+ } ) ,
166
+ } ;
167
+ } ) ;
168
+
169
+ const { createTransformer} =
170
+ require ( '../index' ) as typeof import ( '../index' ) ;
171
+
172
+ const newCacheKey = ( await createTransformer ( ) ) . getCacheKey ! (
173
+ sourceText ,
174
+ sourcePath ,
175
+ transformOptions ,
176
+ ) ;
177
+
178
+ expect ( oldCacheKey ) . not . toEqual ( newCacheKey ) ;
179
+ } ) ;
160
180
161
- const newCacheKey = ( await createTransformer ( ) ) . getCacheKey ! (
162
- sourceText ,
163
- sourcePath ,
164
- transformOptions ,
165
- ) ;
181
+ test ( 'if `babelOptions.babelrc` value is changing' , async ( ) => {
182
+ jest . doMock ( '../babel' , ( ) => {
183
+ return {
184
+ ...babel ,
185
+ loadPartialConfig : (
186
+ options : Parameters < typeof babel . loadPartialConfig > [ 0 ] ,
187
+ ) => ( {
188
+ ...babel . loadPartialConfig ( options ) ,
189
+ babelrc : 'new-babelrc' ,
190
+ } ) ,
191
+ } ;
192
+ } ) ;
193
+
194
+ const { createTransformer} =
195
+ require ( '../index' ) as typeof import ( '../index' ) ;
196
+
197
+ const newCacheKey = ( await createTransformer ( ) ) . getCacheKey ! (
198
+ sourceText ,
199
+ sourcePath ,
200
+ transformOptions ,
201
+ ) ;
202
+
203
+ expect ( oldCacheKey ) . not . toEqual ( newCacheKey ) ;
204
+ } ) ;
166
205
167
- expect ( oldCacheKey ) . not . toEqual ( newCacheKey ) ;
168
- } ) ;
206
+ test ( 'if `instrument` value is changing' , ( ) => {
207
+ const newCacheKey = getCacheKey ! ( sourceText , sourcePath , {
208
+ ...transformOptions ,
209
+ instrument : false ,
210
+ } ) ;
169
211
170
- test ( 'if `instrument` value is changing' , ( ) => {
171
- const newCacheKey = getCacheKey ! ( sourceText , sourcePath , {
172
- ...transformOptions ,
173
- instrument : false ,
212
+ expect ( oldCacheKey ) . not . toEqual ( newCacheKey ) ;
174
213
} ) ;
175
214
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' ;
215
+ test ( 'if `process.env.NODE_ENV` value is changing' , ( ) => {
216
+ process . env . NODE_ENV = 'NEW_NODE_ENV' ;
181
217
182
- const newCacheKey = getCacheKey ! ( sourceText , sourcePath , transformOptions ) ;
218
+ const newCacheKey = getCacheKey ! (
219
+ sourceText ,
220
+ sourcePath ,
221
+ transformOptions ,
222
+ ) ;
183
223
184
- expect ( oldCacheKey ) . not . toEqual ( newCacheKey ) ;
185
- } ) ;
224
+ expect ( oldCacheKey ) . not . toEqual ( newCacheKey ) ;
225
+ } ) ;
186
226
187
- test ( 'if `process.env.BABEL_ENV` value is changing' , ( ) => {
188
- process . env . BABEL_ENV = 'NEW_BABEL_ENV' ;
227
+ test ( 'if `process.env.BABEL_ENV` value is changing' , ( ) => {
228
+ process . env . BABEL_ENV = 'NEW_BABEL_ENV' ;
189
229
190
- const newCacheKey = getCacheKey ! ( sourceText , sourcePath , transformOptions ) ;
230
+ const newCacheKey = getCacheKey ! (
231
+ sourceText ,
232
+ sourcePath ,
233
+ transformOptions ,
234
+ ) ;
191
235
192
- expect ( oldCacheKey ) . not . toEqual ( newCacheKey ) ;
193
- } ) ;
236
+ expect ( oldCacheKey ) . not . toEqual ( newCacheKey ) ;
237
+ } ) ;
194
238
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' ;
239
+ test ( 'if node version is changing' , ( ) => {
240
+ // @ts -expect-error: Testing purpose
241
+ delete process . version ;
242
+ // @ts -expect-error: Testing purpose
243
+ process . version = 'new-node-version' ;
200
244
201
- const newCacheKey = getCacheKey ! ( sourceText , sourcePath , transformOptions ) ;
245
+ const newCacheKey = getCacheKey ! (
246
+ sourceText ,
247
+ sourcePath ,
248
+ transformOptions ,
249
+ ) ;
202
250
203
- expect ( oldCacheKey ) . not . toEqual ( newCacheKey ) ;
251
+ expect ( oldCacheKey ) . not . toEqual ( newCacheKey ) ;
252
+ } ) ;
204
253
} ) ;
205
- } ) ;
254
+ }
0 commit comments