Skip to content

Commit 1813d34

Browse files
Support Babel 8 alongside with Babel 7
In: - babel-plugin-jest-hoist - babel-preset-jest - babel-jest
1 parent fe7f28c commit 1813d34

File tree

14 files changed

+1368
-522
lines changed

14 files changed

+1368
-522
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`] = `

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: 178 additions & 134 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,16 @@ 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+
const createOriginalNodeRequire = Object.getPrototypeOf(Module).createRequire;
18+
const originalNodeRequire = createOriginalNodeRequire(
19+
pathToFileURL(__filename),
20+
);
21+
1222
const {getCacheKey} =
1323
babelJest.createTransformer() as SyncTransformer<BabelTransformOptions>;
1424

@@ -33,173 +43,207 @@ afterEach(() => {
3343
}
3444
});
3545

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

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+
});
5055
});
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')});
6760
});
61+
}
6862

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

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

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);
9275

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

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

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

110-
expect(oldCacheKey).not.toEqual(newCacheKey);
111-
});
88+
const newCacheKey = (await createTransformer()).getCacheKey!(
89+
sourceText,
90+
sourcePath,
91+
transformOptions,
92+
);
11293

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);
11795
});
11896

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

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

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

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

143-
expect(oldCacheKey).not.toEqual(newCacheKey);
144-
});
139+
expect(oldCacheKey).not.toEqual(newCacheKey);
140+
});
145141

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

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

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

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

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

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

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

182-
const newCacheKey = getCacheKey!(sourceText, sourcePath, transformOptions);
213+
const newCacheKey = getCacheKey!(
214+
sourceText,
215+
sourcePath,
216+
transformOptions,
217+
);
183218

184-
expect(oldCacheKey).not.toEqual(newCacheKey);
185-
});
219+
expect(oldCacheKey).not.toEqual(newCacheKey);
220+
});
186221

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

190-
const newCacheKey = getCacheKey!(sourceText, sourcePath, transformOptions);
225+
const newCacheKey = getCacheKey!(
226+
sourceText,
227+
sourcePath,
228+
transformOptions,
229+
);
191230

192-
expect(oldCacheKey).not.toEqual(newCacheKey);
193-
});
231+
expect(oldCacheKey).not.toEqual(newCacheKey);
232+
});
194233

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

201-
const newCacheKey = getCacheKey!(sourceText, sourcePath, transformOptions);
240+
const newCacheKey = getCacheKey!(
241+
sourceText,
242+
sourcePath,
243+
transformOptions,
244+
);
202245

203-
expect(oldCacheKey).not.toEqual(newCacheKey);
246+
expect(oldCacheKey).not.toEqual(newCacheKey);
247+
});
204248
});
205-
});
249+
}

0 commit comments

Comments
 (0)