Skip to content

Commit a20cb5c

Browse files
Split Node.js 18 and Node.js 20+ tests in two files
1 parent 1290442 commit a20cb5c

File tree

5 files changed

+320
-30
lines changed

5 files changed

+320
-30
lines changed

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: {
Lines changed: 288 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,288 @@
1+
// Jest Snapshot v1, https://jestjs.io/docs/snapshot-testing
2+
3+
exports[`babel 7 babel-plugin-jest-hoist 1. automatic react runtime: 1. automatic react runtime 1`] = `
4+
5+
jest.mock('./App', () => () => <div>Hello world</div>);
6+
7+
8+
↓ ↓ ↓ ↓ ↓ ↓
9+
10+
var _jsxFileName = '/root/project/src/file.js';
11+
_getJestObj().mock(
12+
'./App',
13+
() => () =>
14+
/*#__PURE__*/ _jsxDEV(
15+
'div',
16+
{
17+
children: 'Hello world',
18+
},
19+
void 0,
20+
false,
21+
{
22+
fileName: _jsxFileName,
23+
lineNumber: 1,
24+
columnNumber: 32,
25+
},
26+
this,
27+
),
28+
);
29+
import {jsxDEV as _jsxDEV} from 'react/jsx-dev-runtime';
30+
function _getJestObj() {
31+
const {jest} = require('@jest/globals');
32+
_getJestObj = () => jest;
33+
return jest;
34+
}
35+
36+
`;
37+
38+
exports[`babel 7 babel-plugin-jest-hoist 2. top level mocking: 2. top level mocking 1`] = `
39+
40+
require('x');
41+
42+
jest.enableAutomock();
43+
jest.disableAutomock();
44+
45+
46+
↓ ↓ ↓ ↓ ↓ ↓
47+
48+
_getJestObj().enableAutomock();
49+
_getJestObj().disableAutomock();
50+
function _getJestObj() {
51+
const {jest} = require('@jest/globals');
52+
_getJestObj = () => jest;
53+
return jest;
54+
}
55+
require('x');
56+
57+
`;
58+
59+
exports[`babel 7 babel-plugin-jest-hoist 3. within a block: 3. within a block 1`] = `
60+
61+
beforeEach(() => {
62+
require('x');
63+
jest.mock('someNode');
64+
});
65+
66+
67+
↓ ↓ ↓ ↓ ↓ ↓
68+
69+
function _getJestObj() {
70+
const {jest} = require('@jest/globals');
71+
_getJestObj = () => jest;
72+
return jest;
73+
}
74+
beforeEach(() => {
75+
_getJestObj().mock('someNode');
76+
require('x');
77+
});
78+
79+
`;
80+
81+
exports[`babel 7 babel-plugin-jest-hoist 4. within a block with no siblings: 4. within a block with no siblings 1`] = `
82+
83+
beforeEach(() => {
84+
jest.mock('someNode');
85+
});
86+
87+
88+
↓ ↓ ↓ ↓ ↓ ↓
89+
90+
function _getJestObj() {
91+
const {jest} = require('@jest/globals');
92+
_getJestObj = () => jest;
93+
return jest;
94+
}
95+
beforeEach(() => {
96+
_getJestObj().mock('someNode');
97+
});
98+
99+
`;
100+
101+
exports[`babel 7 babel-plugin-jest-hoist 5. required \`jest\` within \`jest\`: 5. required \`jest\` within \`jest\` 1`] = `
102+
103+
const {jest} = require('@jest/globals');
104+
105+
jest.mock('some-module', () => {
106+
jest.requireActual('some-module');
107+
});
108+
109+
110+
↓ ↓ ↓ ↓ ↓ ↓
111+
112+
const {jest} = require('@jest/globals');
113+
jest.mock('some-module', () => {
114+
jest.requireActual('some-module');
115+
});
116+
117+
`;
118+
119+
exports[`babel 7 babel-plugin-jest-hoist 6. imported jest.mock within jest.mock: 6. imported jest.mock within jest.mock 1`] = `
120+
121+
import {jest} from '@jest/globals';
122+
123+
jest.mock('some-module', () => {
124+
jest.mock('some-module');
125+
});
126+
127+
128+
↓ ↓ ↓ ↓ ↓ ↓
129+
130+
_getJestObj().mock('some-module', () => {
131+
_getJestObj().mock('some-module');
132+
});
133+
function _getJestObj() {
134+
const {jest} = require('@jest/globals');
135+
_getJestObj = () => jest;
136+
return jest;
137+
}
138+
import {jest} from '@jest/globals';
139+
140+
`;
141+
142+
exports[`babel 7 babel-plugin-jest-hoist 7. global jest.mock within jest.mock: 7. global jest.mock within jest.mock 1`] = `
143+
144+
jest.mock('some-module', () => {
145+
jest.mock('some-module');
146+
});
147+
148+
149+
↓ ↓ ↓ ↓ ↓ ↓
150+
151+
_getJestObj().mock('some-module', () => {
152+
_getJestObj().mock('some-module');
153+
});
154+
function _getJestObj() {
155+
const {jest} = require('@jest/globals');
156+
_getJestObj = () => jest;
157+
return jest;
158+
}
159+
160+
`;
161+
162+
exports[`babel 7 babel-plugin-jest-hoist 8. imported jest.requireActual in jest.mock: 8. imported jest.requireActual in jest.mock 1`] = `
163+
164+
import {jest} from '@jest/globals';
165+
166+
jest.mock('some-module', () => {
167+
jest.requireActual('some-module');
168+
});
169+
170+
jest.requireActual('some-module');
171+
172+
173+
↓ ↓ ↓ ↓ ↓ ↓
174+
175+
_getJestObj().mock('some-module', () => {
176+
_getJestObj().requireActual('some-module');
177+
});
178+
function _getJestObj() {
179+
const {jest} = require('@jest/globals');
180+
_getJestObj = () => jest;
181+
return jest;
182+
}
183+
import {jest} from '@jest/globals';
184+
jest.requireActual('some-module');
185+
186+
`;
187+
188+
exports[`babel 7 babel-plugin-jest-hoist 9. global jest.requireActual in jest.mock: 9. global jest.requireActual in jest.mock 1`] = `
189+
190+
jest.mock('some-module', () => {
191+
jest.requireActual('some-module');
192+
});
193+
194+
jest.requireActual('some-module');
195+
196+
197+
↓ ↓ ↓ ↓ ↓ ↓
198+
199+
_getJestObj().mock('some-module', () => {
200+
_getJestObj().requireActual('some-module');
201+
});
202+
function _getJestObj() {
203+
const {jest} = require('@jest/globals');
204+
_getJestObj = () => jest;
205+
return jest;
206+
}
207+
jest.requireActual('some-module');
208+
209+
`;
210+
211+
exports[`babel 7 babel-plugin-jest-hoist 10. TS typeof usage in jest.mock: 10. TS typeof usage in jest.mock 1`] = `
212+
213+
jest.mock('some-module', () => {
214+
const actual = jest.requireActual('some-module');
215+
216+
return jest.fn<typeof actual.method>();
217+
});
218+
219+
220+
↓ ↓ ↓ ↓ ↓ ↓
221+
222+
_getJestObj().mock('some-module', () => {
223+
const actual = jest.requireActual('some-module');
224+
return jest.fn();
225+
});
226+
function _getJestObj() {
227+
const {jest} = require('@jest/globals');
228+
_getJestObj = () => jest;
229+
return jest;
230+
}
231+
232+
`;
233+
234+
exports[`babel 7 babel-plugin-jest-hoist 11. jest.spyOn call on the imported module: 11. jest.spyOn call on the imported module 1`] = `
235+
236+
jest.mock('some-module', () => {
237+
const module = jest.requireActual('some-module');
238+
jest.spyOn(module, 'add');
239+
return module;
240+
});
241+
242+
243+
↓ ↓ ↓ ↓ ↓ ↓
244+
245+
_getJestObj().mock('some-module', () => {
246+
const module = jest.requireActual('some-module');
247+
_getJestObj().spyOn(module, 'add');
248+
return module;
249+
});
250+
function _getJestObj() {
251+
const {jest} = require('@jest/globals');
252+
_getJestObj = () => jest;
253+
return jest;
254+
}
255+
256+
`;
257+
258+
exports[`babel 7 babel-plugin-jest-hoist 12. jest.spyOn call in class constructor: 12. jest.spyOn call in class constructor 1`] = `
259+
260+
jest.mock('some-module', () => {
261+
const Actual = jest.requireActual('some-module');
262+
return class Mocked extends Actual {
263+
constructor() {
264+
super();
265+
jest.spyOn(this, 'add');
266+
}
267+
};
268+
});
269+
270+
271+
↓ ↓ ↓ ↓ ↓ ↓
272+
273+
_getJestObj().mock('some-module', () => {
274+
const Actual = jest.requireActual('some-module');
275+
return class Mocked extends Actual {
276+
constructor() {
277+
super();
278+
_getJestObj().spyOn(this, 'add');
279+
}
280+
};
281+
});
282+
function _getJestObj() {
283+
const {jest} = require('@jest/globals');
284+
_getJestObj = () => jest;
285+
return jest;
286+
}
287+
288+
`;

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

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

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-
);
24-
2515
const prettierOptions: Options = {
2616
...resolveConfig(__filename),
2717
filepath: __filename,
@@ -38,26 +28,7 @@ describe('babel 7', () => {
3828
});
3929
});
4030

41-
if (Number.parseInt(process.versions.node, 10) >= 20) {
42-
describe('babel 8', () => {
43-
defineTests({
44-
babel: originalNodeRequire('@babel-8/core'),
45-
presetReact: originalNodeRequire('@babel-8/preset-react'),
46-
presetTypescript: originalNodeRequire('@babel-8/preset-typescript'),
47-
});
48-
});
49-
} else {
50-
// eslint-disable-next-line jest/no-identical-title
51-
describe.skip('babel 8', () => {
52-
defineTests({
53-
babel: null as any,
54-
presetReact: null,
55-
presetTypescript: null,
56-
});
57-
});
58-
}
59-
60-
function defineTests({
31+
export function defineTests({
6132
babel,
6233
presetReact,
6334
presetTypescript,
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
*/
8+
9+
// We need to use the Node.js implementation of `require` to load Babel 8
10+
// packages, instead of our sandboxed implementation, because Babel 8 is
11+
// written in ESM and we don't support require(esm) yet.
12+
import Module from 'node:module';
13+
import {pathToFileURL} from 'node:url';
14+
const createOriginalNodeRequire = Object.getPrototypeOf(Module).createRequire;
15+
const originalNodeRequire = createOriginalNodeRequire(
16+
pathToFileURL(__filename),
17+
);
18+
19+
// This import will also `define()` the Babel 7 tests
20+
import {defineTests} from './hoistPlugin.nodejs18.test';
21+
22+
describe('babel 8', () => {
23+
defineTests({
24+
babel: originalNodeRequire('@babel-8/core'),
25+
presetReact: originalNodeRequire('@babel-8/preset-react'),
26+
presetTypescript: originalNodeRequire('@babel-8/preset-typescript'),
27+
});
28+
});

0 commit comments

Comments
 (0)