Skip to content

Commit e1c717f

Browse files
[babel-jest] Support Babel 8
1 parent ee688a7 commit e1c717f

File tree

8 files changed

+364
-293
lines changed

8 files changed

+364
-293
lines changed

jest.globalSetup.mjs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ const requireFrom = (from, name) =>
1010

1111
export default async function () {
1212
globalThis.preloadedDependencies = {
13+
'babel-jest': {
14+
'@babel-8/core': requireFrom('babel-jest', '@babel-8/core'),
15+
},
1316
'babel-plugin-jest-hoist': {
1417
'@babel-8/core': requireFrom('babel-plugin-jest-hoist', '@babel-8/core'),
1518
'@babel-8/preset-react': requireFrom(

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

@@ -33,173 +44,179 @@ 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-
});
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';
5154

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

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

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);
7965
});
8066

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

84-
const newCacheKey = (await createTransformer()).getCacheKey!(
85-
sourceText,
86-
sourcePath,
87-
transformOptions,
88-
);
72+
const {createTransformer} =
73+
require('../index') as typeof import('../index');
8974

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

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);
11782
});
11883

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

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

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

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

143-
expect(oldCacheKey).not.toEqual(newCacheKey);
144-
});
126+
expect(oldCacheKey).not.toEqual(newCacheKey);
127+
});
145128

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

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

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

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

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

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

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

182-
const newCacheKey = getCacheKey!(sourceText, sourcePath, transformOptions);
198+
const newCacheKey = getCacheKey!(sourceText, sourcePath, transformOptions);
183199

184-
expect(oldCacheKey).not.toEqual(newCacheKey);
185-
});
200+
expect(oldCacheKey).not.toEqual(newCacheKey);
201+
});
186202

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

190-
const newCacheKey = getCacheKey!(sourceText, sourcePath, transformOptions);
206+
const newCacheKey = getCacheKey!(sourceText, sourcePath, transformOptions);
191207

192-
expect(oldCacheKey).not.toEqual(newCacheKey);
193-
});
208+
expect(oldCacheKey).not.toEqual(newCacheKey);
209+
});
194210

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

201-
const newCacheKey = getCacheKey!(sourceText, sourcePath, transformOptions);
217+
const newCacheKey = getCacheKey!(sourceText, sourcePath, transformOptions);
202218

203-
expect(oldCacheKey).not.toEqual(newCacheKey);
219+
expect(oldCacheKey).not.toEqual(newCacheKey);
220+
});
204221
});
205222
});

0 commit comments

Comments
 (0)