Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion e2e/__tests__/__snapshots__/transform.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ exports[`babel-jest ignored tells user to match ignored files 1`] = `

babel-jest: Babel ignores __tests__/ignoredFile.test.js - make sure to include the file in Jest's transformIgnorePatterns as well.

at assertLoadedBabelConfig (../../../packages/babel-jest/build/index.js:137:11)"
at assertLoadedBabelConfig (../../../packages/babel-jest/build/index.js:148:11)"
`;

exports[`babel-jest instruments only specific files and collects coverage 1`] = `
Expand Down
3 changes: 3 additions & 0 deletions jest.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ export default {
'/packages/jest-snapshot/src/__tests__/fixtures/',
'/e2e/__tests__/iterator-to-null-test.ts',
'/e2e/__tests__/tsIntegration.test.ts', // this test needs types to be build, it runs in a separate CI job through `jest.config.ts.mjs`
Number.parseInt(process.versions.node, 10) >= 20
? '/.*\\.nodejs18\\..*'
: '/.*\\.nodejs20plus\\..*',
],
testTimeout: 70_000,
transform: {
Expand Down
3 changes: 2 additions & 1 deletion packages/babel-jest/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,13 @@
"slash": "^3.0.0"
},
"devDependencies": {
"@babel-8/core": "npm:@babel/[email protected]",
"@babel/core": "^7.27.4",
"@jest/test-utils": "workspace:*",
"@types/graceful-fs": "^4.1.9"
},
"peerDependencies": {
"@babel/core": "^7.11.0"
"@babel/core": "^7.11.0 || ^8.0.0-0"
},
"engines": {
"node": "^18.14.0 || ^20.0.0 || ^22.0.0 || >=24.0.0"
Expand Down
315 changes: 182 additions & 133 deletions packages/babel-jest/src/__tests__/getCacheKey.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,17 @@ import type {TransformOptions as BabelTransformOptions} from '@babel/core';
import type {SyncTransformer, TransformOptions} from '@jest/transform';
import babelJest from '../index';

// We need to use the Node.js implementation of `require` to load Babel 8
// packages, instead of our sandboxed implementation, because Babel 8 is
// written in ESM and we don't support require(esm) yet.
import Module from 'node:module';
import {pathToFileURL} from 'node:url';
import {onNodeVersions} from '@jest/test-utils';
const createOriginalNodeRequire = Object.getPrototypeOf(Module).createRequire;
const originalNodeRequire = createOriginalNodeRequire(
pathToFileURL(__filename),
);

const {getCacheKey} =
babelJest.createTransformer() as SyncTransformer<BabelTransformOptions>;

Expand All @@ -33,173 +44,211 @@ afterEach(() => {
}
});

describe('getCacheKey', () => {
const sourceText = 'mock source';
const sourcePath = 'mock-source-path.js';

const transformOptions = {
config: {rootDir: 'mock-root-dir'},
configString: 'mock-config-string',
instrument: true,
} as TransformOptions<BabelTransformOptions>;

const oldCacheKey = getCacheKey!(sourceText, sourcePath, transformOptions);

test('returns cache key hash', () => {
expect(oldCacheKey).toHaveLength(32);
});

test('if `THIS_FILE` value is changing', async () => {
jest.doMock('graceful-fs', () => ({
readFileSync: () => 'new this file',
}));

const {createTransformer} =
require('../index') as typeof import('../index');

const newCacheKey = (await createTransformer()).getCacheKey!(
sourceText,
sourcePath,
transformOptions,
);
describe('babel 7', () => {
defineTests({getBabel: () => require('@babel/core')});
});

expect(oldCacheKey).not.toEqual(newCacheKey);
describe('babel 8', () => {
onNodeVersions('>=20', () => {
defineTests({
getBabel: () => originalNodeRequire('@babel-8/core'),
});
});
});

test('if `babelOptions.options` value is changing', async () => {
jest.doMock('../loadBabelConfig', () => {
const babel = require('@babel/core') as typeof import('@babel/core');

return {
loadPartialConfig: (options: BabelTransformOptions) => ({
...babel.loadPartialConfig(options),
options: 'new-options',
}),
};
function defineTests({
getBabel,
}: {
getBabel: () => typeof import('@babel-8/core');
}) {
describe('getCacheKey', () => {
let babel: typeof import('@babel-8/core');
beforeAll(() => {
babel = getBabel();
});

const {createTransformer} =
require('../index') as typeof import('../index');
const sourceText = 'mock source';
const sourcePath = 'mock-source-path.js';

const newCacheKey = (await createTransformer()).getCacheKey!(
sourceText,
sourcePath,
transformOptions,
);
const transformOptions = {
config: {rootDir: 'mock-root-dir'},
configString: 'mock-config-string',
instrument: true,
} as TransformOptions<BabelTransformOptions>;

expect(oldCacheKey).not.toEqual(newCacheKey);
});
const oldCacheKey = getCacheKey!(sourceText, sourcePath, transformOptions);

test('if `sourceText` value is changing', () => {
const newCacheKey = getCacheKey!(
'new source text',
sourcePath,
transformOptions,
);
test('returns cache key hash', () => {
expect(oldCacheKey).toHaveLength(32);
});

expect(oldCacheKey).not.toEqual(newCacheKey);
});
test('if `THIS_FILE` value is changing', async () => {
jest.doMock('graceful-fs', () => ({
readFileSync: () => 'new this file',
}));

test('if `sourcePath` value is changing', () => {
const newCacheKey = getCacheKey!(
sourceText,
'new-source-path.js',
transformOptions,
);
const {createTransformer} =
require('../index') as typeof import('../index');

expect(oldCacheKey).not.toEqual(newCacheKey);
});
const newCacheKey = (await createTransformer()).getCacheKey!(
sourceText,
sourcePath,
transformOptions,
);

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

expect(oldCacheKey).not.toEqual(newCacheKey);
});
test('if `babelOptions.options` value is changing', async () => {
jest.doMock('../babel', () => {
return {
...babel,
loadPartialConfigSync: (
options: Parameters<typeof babel.loadPartialConfigSync>[0],
) => ({
...babel.loadPartialConfigSync(options),
options: 'new-options',
}),
};
});

const {createTransformer} =
require('../index') as typeof import('../index');

const newCacheKey = (await createTransformer()).getCacheKey!(
sourceText,
sourcePath,
transformOptions,
);

expect(oldCacheKey).not.toEqual(newCacheKey);
});

test('if `babelOptions.config` value is changing', async () => {
jest.doMock('../loadBabelConfig', () => {
const babel = require('@babel/core') as typeof import('@babel/core');
test('if `sourceText` value is changing', () => {
const newCacheKey = getCacheKey!(
'new source text',
sourcePath,
transformOptions,
);

return {
loadPartialConfig: (options: BabelTransformOptions) => ({
...babel.loadPartialConfig(options),
config: 'new-config',
}),
};
expect(oldCacheKey).not.toEqual(newCacheKey);
});

const {createTransformer} =
require('../index') as typeof import('../index');

const newCacheKey = (await createTransformer()).getCacheKey!(
sourceText,
sourcePath,
transformOptions,
);
test('if `sourcePath` value is changing', () => {
const newCacheKey = getCacheKey!(
sourceText,
'new-source-path.js',
transformOptions,
);

expect(oldCacheKey).not.toEqual(newCacheKey);
});
expect(oldCacheKey).not.toEqual(newCacheKey);
});

test('if `babelOptions.babelrc` value is changing', async () => {
jest.doMock('../loadBabelConfig', () => {
const babel = require('@babel/core') as typeof import('@babel/core');
test('if `configString` value is changing', () => {
const newCacheKey = getCacheKey!(sourceText, sourcePath, {
...transformOptions,
configString: 'new-config-string',
});

return {
loadPartialConfig: (options: BabelTransformOptions) => ({
...babel.loadPartialConfig(options),
babelrc: 'new-babelrc',
}),
};
expect(oldCacheKey).not.toEqual(newCacheKey);
});

const {createTransformer} =
require('../index') as typeof import('../index');
test('if `babelOptions.config` value is changing', async () => {
jest.doMock('../babel', () => {
return {
...babel,
loadPartialConfigSync: (
options: Parameters<typeof babel.loadPartialConfigSync>[0],
) => ({
...babel.loadPartialConfigSync(options),
config: 'new-config',
}),
};
});

const {createTransformer} =
require('../index') as typeof import('../index');

const newCacheKey = (await createTransformer()).getCacheKey!(
sourceText,
sourcePath,
transformOptions,
);

expect(oldCacheKey).not.toEqual(newCacheKey);
});

const newCacheKey = (await createTransformer()).getCacheKey!(
sourceText,
sourcePath,
transformOptions,
);
test('if `babelOptions.babelrc` value is changing', async () => {
jest.doMock('../babel', () => {
return {
...babel,
loadPartialConfig: (
options: Parameters<typeof babel.loadPartialConfig>[0],
) => ({
...babel.loadPartialConfig(options),
babelrc: 'new-babelrc',
}),
};
});

const {createTransformer} =
require('../index') as typeof import('../index');

const newCacheKey = (await createTransformer()).getCacheKey!(
sourceText,
sourcePath,
transformOptions,
);

expect(oldCacheKey).not.toEqual(newCacheKey);
});

expect(oldCacheKey).not.toEqual(newCacheKey);
});
test('if `instrument` value is changing', () => {
const newCacheKey = getCacheKey!(sourceText, sourcePath, {
...transformOptions,
instrument: false,
});

test('if `instrument` value is changing', () => {
const newCacheKey = getCacheKey!(sourceText, sourcePath, {
...transformOptions,
instrument: false,
expect(oldCacheKey).not.toEqual(newCacheKey);
});

expect(oldCacheKey).not.toEqual(newCacheKey);
});

test('if `process.env.NODE_ENV` value is changing', () => {
process.env.NODE_ENV = 'NEW_NODE_ENV';
test('if `process.env.NODE_ENV` value is changing', () => {
process.env.NODE_ENV = 'NEW_NODE_ENV';

const newCacheKey = getCacheKey!(sourceText, sourcePath, transformOptions);
const newCacheKey = getCacheKey!(
sourceText,
sourcePath,
transformOptions,
);

expect(oldCacheKey).not.toEqual(newCacheKey);
});
expect(oldCacheKey).not.toEqual(newCacheKey);
});

test('if `process.env.BABEL_ENV` value is changing', () => {
process.env.BABEL_ENV = 'NEW_BABEL_ENV';
test('if `process.env.BABEL_ENV` value is changing', () => {
process.env.BABEL_ENV = 'NEW_BABEL_ENV';

const newCacheKey = getCacheKey!(sourceText, sourcePath, transformOptions);
const newCacheKey = getCacheKey!(
sourceText,
sourcePath,
transformOptions,
);

expect(oldCacheKey).not.toEqual(newCacheKey);
});
expect(oldCacheKey).not.toEqual(newCacheKey);
});

test('if node version is changing', () => {
// @ts-expect-error: Testing purpose
delete process.version;
// @ts-expect-error: Testing purpose
process.version = 'new-node-version';
test('if node version is changing', () => {
// @ts-expect-error: Testing purpose
delete process.version;
// @ts-expect-error: Testing purpose
process.version = 'new-node-version';

const newCacheKey = getCacheKey!(sourceText, sourcePath, transformOptions);
const newCacheKey = getCacheKey!(
sourceText,
sourcePath,
transformOptions,
);

expect(oldCacheKey).not.toEqual(newCacheKey);
expect(oldCacheKey).not.toEqual(newCacheKey);
});
});
});
}
Loading
Loading