Skip to content

Commit d2a2491

Browse files
Support Babel 8 in plugins and presets (#15750)
Co-authored-by: Christoph Nakazawa <[email protected]>
1 parent 401ec5a commit d2a2491

File tree

18 files changed

+1705
-560
lines changed

18 files changed

+1705
-560
lines changed

e2e/__tests__/__snapshots__/transform.test.ts.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ exports[`babel-jest ignored tells user to match ignored files 1`] = `
66
77
babel-jest: Babel ignores __tests__/ignoredFile.test.js - make sure to include the file in Jest's transformIgnorePatterns as well.
88
9-
at assertLoadedBabelConfig (../../../packages/babel-jest/build/index.js:137:11)"
9+
at assertLoadedBabelConfig (../../../packages/babel-jest/build/index.js:148:11)"
1010
`;
1111

1212
exports[`babel-jest instruments only specific files and collects coverage 1`] = `

jest.config.mjs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,9 @@ export default {
6868
'/packages/jest-snapshot/src/__tests__/fixtures/',
6969
'/e2e/__tests__/iterator-to-null-test.ts',
7070
'/e2e/__tests__/tsIntegration.test.ts', // this test needs types to be build, it runs in a separate CI job through `jest.config.ts.mjs`
71+
Number.parseInt(process.versions.node, 10) >= 20
72+
? '/.*\\.nodejs18\\..*'
73+
: '/.*\\.nodejs20plus\\..*',
7174
],
7275
testTimeout: 70_000,
7376
transform: {

packages/babel-jest/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,13 @@
2929
"slash": "^3.0.0"
3030
},
3131
"devDependencies": {
32+
"@babel-8/core": "npm:@babel/[email protected]",
3233
"@babel/core": "^7.27.4",
3334
"@jest/test-utils": "workspace:*",
3435
"@types/graceful-fs": "^4.1.9"
3536
},
3637
"peerDependencies": {
37-
"@babel/core": "^7.11.0"
38+
"@babel/core": "^7.11.0 || ^8.0.0-0"
3839
},
3940
"engines": {
4041
"node": "^18.14.0 || ^20.0.0 || ^22.0.0 || >=24.0.0"

packages/babel-jest/src/__tests__/getCacheKey.test.ts

Lines changed: 182 additions & 133 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,17 @@ import type {TransformOptions as BabelTransformOptions} from '@babel/core';
99
import type {SyncTransformer, TransformOptions} from '@jest/transform';
1010
import babelJest from '../index';
1111

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+
1223
const {getCacheKey} =
1324
babelJest.createTransformer() as SyncTransformer<BabelTransformOptions>;
1425

@@ -33,173 +44,211 @@ afterEach(() => {
3344
}
3445
});
3546

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+
});
6550

66-
expect(oldCacheKey).not.toEqual(newCacheKey);
51+
describe('babel 8', () => {
52+
onNodeVersions('>=20', () => {
53+
defineTests({
54+
getBabel: () => originalNodeRequire('@babel-8/core'),
55+
});
6756
});
57+
});
6858

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();
7968
});
8069

81-
const {createTransformer} =
82-
require('../index') as typeof import('../index');
70+
const sourceText = 'mock source';
71+
const sourcePath = 'mock-source-path.js';
8372

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>;
8978

90-
expect(oldCacheKey).not.toEqual(newCacheKey);
91-
});
79+
const oldCacheKey = getCacheKey!(sourceText, sourcePath, transformOptions);
9280

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+
});
9984

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+
}));
10289

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');
10992

110-
expect(oldCacheKey).not.toEqual(newCacheKey);
111-
});
93+
const newCacheKey = (await createTransformer()).getCacheKey!(
94+
sourceText,
95+
sourcePath,
96+
transformOptions,
97+
);
11298

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);
117100
});
118101

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+
});
121126

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+
);
125133

126-
return {
127-
loadPartialConfig: (options: BabelTransformOptions) => ({
128-
...babel.loadPartialConfig(options),
129-
config: 'new-config',
130-
}),
131-
};
134+
expect(oldCacheKey).not.toEqual(newCacheKey);
132135
});
133136

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+
);
142143

143-
expect(oldCacheKey).not.toEqual(newCacheKey);
144-
});
144+
expect(oldCacheKey).not.toEqual(newCacheKey);
145+
});
145146

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+
});
149152

150-
return {
151-
loadPartialConfig: (options: BabelTransformOptions) => ({
152-
...babel.loadPartialConfig(options),
153-
babelrc: 'new-babelrc',
154-
}),
155-
};
153+
expect(oldCacheKey).not.toEqual(newCacheKey);
156154
});
157155

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+
});
160180

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+
});
166205

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+
});
169211

170-
test('if `instrument` value is changing', () => {
171-
const newCacheKey = getCacheKey!(sourceText, sourcePath, {
172-
...transformOptions,
173-
instrument: false,
212+
expect(oldCacheKey).not.toEqual(newCacheKey);
174213
});
175214

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';
181217

182-
const newCacheKey = getCacheKey!(sourceText, sourcePath, transformOptions);
218+
const newCacheKey = getCacheKey!(
219+
sourceText,
220+
sourcePath,
221+
transformOptions,
222+
);
183223

184-
expect(oldCacheKey).not.toEqual(newCacheKey);
185-
});
224+
expect(oldCacheKey).not.toEqual(newCacheKey);
225+
});
186226

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';
189229

190-
const newCacheKey = getCacheKey!(sourceText, sourcePath, transformOptions);
230+
const newCacheKey = getCacheKey!(
231+
sourceText,
232+
sourcePath,
233+
transformOptions,
234+
);
191235

192-
expect(oldCacheKey).not.toEqual(newCacheKey);
193-
});
236+
expect(oldCacheKey).not.toEqual(newCacheKey);
237+
});
194238

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';
200244

201-
const newCacheKey = getCacheKey!(sourceText, sourcePath, transformOptions);
245+
const newCacheKey = getCacheKey!(
246+
sourceText,
247+
sourcePath,
248+
transformOptions,
249+
);
202250

203-
expect(oldCacheKey).not.toEqual(newCacheKey);
251+
expect(oldCacheKey).not.toEqual(newCacheKey);
252+
});
204253
});
205-
});
254+
}

0 commit comments

Comments
 (0)