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