Skip to content

Commit acfae6b

Browse files
Try avoiding mismatched snapshot errors on Node.js 18
1 parent 4bb646b commit acfae6b

File tree

3 files changed

+447
-410
lines changed

3 files changed

+447
-410
lines changed

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

Lines changed: 173 additions & 160 deletions
Original file line numberDiff line numberDiff line change
@@ -43,198 +43,211 @@ afterEach(() => {
4343
}
4444
});
4545

46-
describe.each(
47-
[
48-
{babel: require('@babel/core'), version: '7'},
49-
Number.parseInt(process.versions.node) >= 20 && {
50-
babel: originalNodeRequire('@babel-8/core'),
51-
version: '8',
52-
},
53-
].filter(x => x !== false),
54-
)('babel $version', ({babel}) => {
55-
describe('getCacheKey', () => {
56-
const sourceText = 'mock source';
57-
const sourcePath = 'mock-source-path.js';
58-
59-
const transformOptions = {
60-
config: {rootDir: 'mock-root-dir'},
61-
configString: 'mock-config-string',
62-
instrument: true,
63-
} as TransformOptions<BabelTransformOptions>;
64-
65-
const oldCacheKey = getCacheKey!(sourceText, sourcePath, transformOptions);
66-
67-
test('returns cache key hash', () => {
68-
expect(oldCacheKey).toHaveLength(32);
69-
});
70-
71-
test('if `THIS_FILE` value is changing', async () => {
72-
jest.doMock('graceful-fs', () => ({
73-
readFileSync: () => 'new this file',
74-
}));
75-
76-
const {createTransformer} =
77-
require('../index') as typeof import('../index');
78-
79-
const newCacheKey = (await createTransformer()).getCacheKey!(
46+
const babelVersions = [
47+
{babel: require('@babel/core'), version: '7'},
48+
{
49+
babel:
50+
Number.parseInt(process.versions.node, 10) >= 20 &&
51+
originalNodeRequire('@babel-8/core'),
52+
version: '8',
53+
},
54+
];
55+
56+
describe.skip.each(babelVersions.filter(x => !x.babel))(
57+
'babel $version',
58+
() => {},
59+
);
60+
describe.each(babelVersions.filter(x => x.babel))(
61+
'babel $version',
62+
({babel}) => {
63+
describe('getCacheKey', () => {
64+
const sourceText = 'mock source';
65+
const sourcePath = 'mock-source-path.js';
66+
67+
const transformOptions = {
68+
config: {rootDir: 'mock-root-dir'},
69+
configString: 'mock-config-string',
70+
instrument: true,
71+
} as TransformOptions<BabelTransformOptions>;
72+
73+
const oldCacheKey = getCacheKey!(
8074
sourceText,
8175
sourcePath,
8276
transformOptions,
8377
);
8478

85-
expect(oldCacheKey).not.toEqual(newCacheKey);
86-
});
87-
88-
test('if `babelOptions.options` value is changing', async () => {
89-
jest.doMock('../babel', () => {
90-
return {
91-
...babel,
92-
loadPartialConfigSync: (
93-
options: Parameters<typeof babel.loadPartialConfigSync>[0],
94-
) => ({
95-
...babel.loadPartialConfigSync(options),
96-
options: 'new-options',
97-
}),
98-
};
79+
test('returns cache key hash', () => {
80+
expect(oldCacheKey).toHaveLength(32);
9981
});
10082

101-
const {createTransformer} =
102-
require('../index') as typeof import('../index');
83+
test('if `THIS_FILE` value is changing', async () => {
84+
jest.doMock('graceful-fs', () => ({
85+
readFileSync: () => 'new this file',
86+
}));
10387

104-
const newCacheKey = (await createTransformer()).getCacheKey!(
105-
sourceText,
106-
sourcePath,
107-
transformOptions,
108-
);
88+
const {createTransformer} =
89+
require('../index') as typeof import('../index');
10990

110-
expect(oldCacheKey).not.toEqual(newCacheKey);
111-
});
112-
113-
test('if `sourceText` value is changing', () => {
114-
const newCacheKey = getCacheKey!(
115-
'new source text',
116-
sourcePath,
117-
transformOptions,
118-
);
91+
const newCacheKey = (await createTransformer()).getCacheKey!(
92+
sourceText,
93+
sourcePath,
94+
transformOptions,
95+
);
11996

120-
expect(oldCacheKey).not.toEqual(newCacheKey);
121-
});
97+
expect(oldCacheKey).not.toEqual(newCacheKey);
98+
});
12299

123-
test('if `sourcePath` value is changing', () => {
124-
const newCacheKey = getCacheKey!(
125-
sourceText,
126-
'new-source-path.js',
127-
transformOptions,
128-
);
100+
test('if `babelOptions.options` value is changing', async () => {
101+
jest.doMock('../babel', () => {
102+
return {
103+
...babel,
104+
loadPartialConfigSync: (
105+
options: Parameters<typeof babel.loadPartialConfigSync>[0],
106+
) => ({
107+
...babel.loadPartialConfigSync(options),
108+
options: 'new-options',
109+
}),
110+
};
111+
});
112+
113+
const {createTransformer} =
114+
require('../index') as typeof import('../index');
115+
116+
const newCacheKey = (await createTransformer()).getCacheKey!(
117+
sourceText,
118+
sourcePath,
119+
transformOptions,
120+
);
121+
122+
expect(oldCacheKey).not.toEqual(newCacheKey);
123+
});
129124

130-
expect(oldCacheKey).not.toEqual(newCacheKey);
131-
});
125+
test('if `sourceText` value is changing', () => {
126+
const newCacheKey = getCacheKey!(
127+
'new source text',
128+
sourcePath,
129+
transformOptions,
130+
);
132131

133-
test('if `configString` value is changing', () => {
134-
const newCacheKey = getCacheKey!(sourceText, sourcePath, {
135-
...transformOptions,
136-
configString: 'new-config-string',
132+
expect(oldCacheKey).not.toEqual(newCacheKey);
137133
});
138134

139-
expect(oldCacheKey).not.toEqual(newCacheKey);
140-
});
135+
test('if `sourcePath` value is changing', () => {
136+
const newCacheKey = getCacheKey!(
137+
sourceText,
138+
'new-source-path.js',
139+
transformOptions,
140+
);
141141

142-
test('if `babelOptions.config` value is changing', async () => {
143-
jest.doMock('../babel', () => {
144-
return {
145-
...babel,
146-
loadPartialConfigSync: (
147-
options: Parameters<typeof babel.loadPartialConfigSync>[0],
148-
) => ({
149-
...babel.loadPartialConfigSync(options),
150-
config: 'new-config',
151-
}),
152-
};
142+
expect(oldCacheKey).not.toEqual(newCacheKey);
153143
});
154144

155-
const {createTransformer} =
156-
require('../index') as typeof import('../index');
145+
test('if `configString` value is changing', () => {
146+
const newCacheKey = getCacheKey!(sourceText, sourcePath, {
147+
...transformOptions,
148+
configString: 'new-config-string',
149+
});
157150

158-
const newCacheKey = (await createTransformer()).getCacheKey!(
159-
sourceText,
160-
sourcePath,
161-
transformOptions,
162-
);
163-
164-
expect(oldCacheKey).not.toEqual(newCacheKey);
165-
});
166-
167-
test('if `babelOptions.babelrc` value is changing', async () => {
168-
jest.doMock('../babel', () => {
169-
return {
170-
...babel,
171-
loadPartialConfig: (
172-
options: Parameters<typeof babel.loadPartialConfig>[0],
173-
) => ({
174-
...babel.loadPartialConfig(options),
175-
babelrc: 'new-babelrc',
176-
}),
177-
};
151+
expect(oldCacheKey).not.toEqual(newCacheKey);
178152
});
179153

180-
const {createTransformer} =
181-
require('../index') as typeof import('../index');
154+
test('if `babelOptions.config` value is changing', async () => {
155+
jest.doMock('../babel', () => {
156+
return {
157+
...babel,
158+
loadPartialConfigSync: (
159+
options: Parameters<typeof babel.loadPartialConfigSync>[0],
160+
) => ({
161+
...babel.loadPartialConfigSync(options),
162+
config: 'new-config',
163+
}),
164+
};
165+
});
166+
167+
const {createTransformer} =
168+
require('../index') as typeof import('../index');
169+
170+
const newCacheKey = (await createTransformer()).getCacheKey!(
171+
sourceText,
172+
sourcePath,
173+
transformOptions,
174+
);
175+
176+
expect(oldCacheKey).not.toEqual(newCacheKey);
177+
});
182178

183-
const newCacheKey = (await createTransformer()).getCacheKey!(
184-
sourceText,
185-
sourcePath,
186-
transformOptions,
187-
);
179+
test('if `babelOptions.babelrc` value is changing', async () => {
180+
jest.doMock('../babel', () => {
181+
return {
182+
...babel,
183+
loadPartialConfig: (
184+
options: Parameters<typeof babel.loadPartialConfig>[0],
185+
) => ({
186+
...babel.loadPartialConfig(options),
187+
babelrc: 'new-babelrc',
188+
}),
189+
};
190+
});
191+
192+
const {createTransformer} =
193+
require('../index') as typeof import('../index');
194+
195+
const newCacheKey = (await createTransformer()).getCacheKey!(
196+
sourceText,
197+
sourcePath,
198+
transformOptions,
199+
);
200+
201+
expect(oldCacheKey).not.toEqual(newCacheKey);
202+
});
188203

189-
expect(oldCacheKey).not.toEqual(newCacheKey);
190-
});
204+
test('if `instrument` value is changing', () => {
205+
const newCacheKey = getCacheKey!(sourceText, sourcePath, {
206+
...transformOptions,
207+
instrument: false,
208+
});
191209

192-
test('if `instrument` value is changing', () => {
193-
const newCacheKey = getCacheKey!(sourceText, sourcePath, {
194-
...transformOptions,
195-
instrument: false,
210+
expect(oldCacheKey).not.toEqual(newCacheKey);
196211
});
197212

198-
expect(oldCacheKey).not.toEqual(newCacheKey);
199-
});
213+
test('if `process.env.NODE_ENV` value is changing', () => {
214+
process.env.NODE_ENV = 'NEW_NODE_ENV';
200215

201-
test('if `process.env.NODE_ENV` value is changing', () => {
202-
process.env.NODE_ENV = 'NEW_NODE_ENV';
216+
const newCacheKey = getCacheKey!(
217+
sourceText,
218+
sourcePath,
219+
transformOptions,
220+
);
203221

204-
const newCacheKey = getCacheKey!(
205-
sourceText,
206-
sourcePath,
207-
transformOptions,
208-
);
209-
210-
expect(oldCacheKey).not.toEqual(newCacheKey);
211-
});
222+
expect(oldCacheKey).not.toEqual(newCacheKey);
223+
});
212224

213-
test('if `process.env.BABEL_ENV` value is changing', () => {
214-
process.env.BABEL_ENV = 'NEW_BABEL_ENV';
225+
test('if `process.env.BABEL_ENV` value is changing', () => {
226+
process.env.BABEL_ENV = 'NEW_BABEL_ENV';
215227

216-
const newCacheKey = getCacheKey!(
217-
sourceText,
218-
sourcePath,
219-
transformOptions,
220-
);
228+
const newCacheKey = getCacheKey!(
229+
sourceText,
230+
sourcePath,
231+
transformOptions,
232+
);
221233

222-
expect(oldCacheKey).not.toEqual(newCacheKey);
223-
});
234+
expect(oldCacheKey).not.toEqual(newCacheKey);
235+
});
224236

225-
test('if node version is changing', () => {
226-
// @ts-expect-error: Testing purpose
227-
delete process.version;
228-
// @ts-expect-error: Testing purpose
229-
process.version = 'new-node-version';
237+
test('if node version is changing', () => {
238+
// @ts-expect-error: Testing purpose
239+
delete process.version;
240+
// @ts-expect-error: Testing purpose
241+
process.version = 'new-node-version';
230242

231-
const newCacheKey = getCacheKey!(
232-
sourceText,
233-
sourcePath,
234-
transformOptions,
235-
);
243+
const newCacheKey = getCacheKey!(
244+
sourceText,
245+
sourcePath,
246+
transformOptions,
247+
);
236248

237-
expect(oldCacheKey).not.toEqual(newCacheKey);
249+
expect(oldCacheKey).not.toEqual(newCacheKey);
250+
});
238251
});
239-
});
240-
});
252+
},
253+
);

0 commit comments

Comments
 (0)