Skip to content

Commit 42235df

Browse files
Make tests work
1 parent 0d9a720 commit 42235df

File tree

5 files changed

+38
-84
lines changed

5 files changed

+38
-84
lines changed

jest.config.mjs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ export default {
2323
'!**/vendor/**',
2424
'!e2e/**',
2525
],
26-
globalSetup: './jest.globalSetup.mjs',
2726
modulePathIgnorePatterns: [
2827
'examples/.*',
2928
'packages/.*/build',

jest.globalSetup.mjs

Lines changed: 0 additions & 32 deletions
This file was deleted.

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

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,15 @@ 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-
// We need to use `var` to declare a global variable
17-
// eslint-disable-next-line no-var
18-
var preloadedDependencies: {
19-
'babel-jest': {
20-
'@babel-8/core': null | typeof import('@babel-8/core');
21-
};
22-
};
23-
}
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+
);
2421

2522
const {getCacheKey} =
2623
babelJest.createTransformer() as SyncTransformer<BabelTransformOptions>;
@@ -49,11 +46,11 @@ afterEach(() => {
4946
describe.each(
5047
[
5148
{babel: require('@babel/core'), version: '7'},
52-
{
53-
babel: globalThis.preloadedDependencies['babel-jest']['@babel-8/core'],
49+
parseInt(process.versions.node) >= 20 && {
50+
babel: originalNodeRequire('@babel-8/core'),
5451
version: '8',
5552
},
56-
].filter(v => v.babel !== null),
53+
].filter((x) => x !== false),
5754
)('babel $version', ({babel}) => {
5855
describe('getCacheKey', () => {
5956
const sourceText = 'mock source';

packages/babel-jest/src/__tests__/index.ts

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,15 @@ import {makeProjectConfig} from '@jest/test-utils';
1313
import type {SyncTransformer, TransformOptions} from '@jest/transform';
1414
import babelJest, {createTransformer} from '../index';
1515

16-
// This is loaded by /jest.globalSetup.mjs. We need to pre-load it there
17-
// because this test file is being transpiled, leading to require(esm), which
18-
// we don't support yet.
19-
declare global {
20-
// We need to use `var` to declare a global variable
21-
// eslint-disable-next-line no-var
22-
var preloadedDependencies: {
23-
'babel-jest': {
24-
'@babel-8/core': null | typeof import('@babel-8/core');
25-
};
26-
};
27-
}
16+
// We need to use the Node.js implementation of `require` to load Babel 8
17+
// packages, instead of our sandboxed implementation, because Babel 8 is
18+
// written in ESM and we don't support require(esm) yet.
19+
import Module from 'node:module';
20+
import {pathToFileURL} from 'node:url';
21+
const createOriginalNodeRequire = Object.getPrototypeOf(Module).createRequire;
22+
const originalNodeRequire = createOriginalNodeRequire(
23+
pathToFileURL(__filename),
24+
);
2825

2926
type BabelCore = typeof import('@babel-8/core');
3027

@@ -65,11 +62,11 @@ customMultiply({a: 32, dummy: "test"}, 2);
6562
describe.each(
6663
[
6764
{babel: require('@babel/core'), version: '7'},
68-
{
69-
babel: globalThis.preloadedDependencies['babel-jest']['@babel-8/core'],
65+
parseInt(process.versions.node) >= 20 && {
66+
babel: originalNodeRequire('@babel-8/core'),
7067
version: '8',
7168
},
72-
].filter(v => v.babel !== null),
69+
].filter(x => x !== false),
7370
)('babel $version', ({babel}: {babel: BabelCore}) => {
7471
beforeEach(() => {
7572
jest.clearAllMocks();

packages/babel-plugin-jest-hoist/src/__tests__/hoistPlugin.test.ts

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,15 @@ import pluginTester from 'babel-plugin-tester';
1212
import type {Options} from 'prettier';
1313
import babelPluginJestHoist from '..';
1414

15-
// This is loaded by /jest.globalSetup.mjs. We need to pre-load it there
16-
// because this test file is being transpiled, leading to require(esm), which
17-
// we don't support yet.
18-
declare const preloadedDependencies: {
19-
'babel-plugin-jest-hoist': {
20-
'@babel-8/core': typeof import('@babel-8/core');
21-
'@babel-8/preset-react': typeof import('@babel-8/preset-react');
22-
'@babel-8/preset-typescript': typeof import('@babel-8/preset-typescript');
23-
};
24-
};
15+
// We need to use the Node.js implementation of `require` to load Babel 8
16+
// packages, instead of our sandboxed implementation, because Babel 8 is
17+
// written in ESM and we don't support require(esm) yet.
18+
import Module from 'node:module';
19+
import {pathToFileURL} from 'node:url';
20+
const createOriginalNodeRequire = Object.getPrototypeOf(Module).createRequire;
21+
const originalNodeRequire = createOriginalNodeRequire(
22+
pathToFileURL(__filename),
23+
);
2524

2625
const prettierOptions: Options = {
2726
...resolveConfig(__filename),
@@ -39,19 +38,13 @@ describe.each(
3938
presetTypescript: require('@babel/preset-typescript').default,
4039
version: '7',
4140
},
42-
{
43-
babel: preloadedDependencies['babel-plugin-jest-hoist']['@babel-8/core'],
44-
presetReact:
45-
preloadedDependencies['babel-plugin-jest-hoist'][
46-
'@babel-8/preset-react'
47-
],
48-
presetTypescript:
49-
preloadedDependencies['babel-plugin-jest-hoist'][
50-
'@babel-8/preset-typescript'
51-
],
41+
parseInt(process.versions.node) >= 20 && {
42+
babel: originalNodeRequire('@babel-8/core'),
43+
presetReact: originalNodeRequire('@babel-8/preset-react'),
44+
presetTypescript: originalNodeRequire('@babel-8/preset-typescript'),
5245
version: '8',
5346
},
54-
].filter(v => v.babel !== null),
47+
].filter(x => x !== false),
5548
)('babel $version', ({babel, presetReact, presetTypescript}) => {
5649
pluginTester({
5750
babel,

0 commit comments

Comments
 (0)