diff --git a/e2e/transform-js/babel-file/babel.config.js b/e2e/transform-js/babel-cjs-file/babel.config.cjs similarity index 100% rename from e2e/transform-js/babel-file/babel.config.js rename to e2e/transform-js/babel-cjs-file/babel.config.cjs diff --git a/e2e/transform-js/babel-cjs-file/jest.config.js b/e2e/transform-js/babel-cjs-file/jest.config.js new file mode 100644 index 0000000000..7995e4026c --- /dev/null +++ b/e2e/transform-js/babel-cjs-file/jest.config.js @@ -0,0 +1,18 @@ +/** @type {import('../../../dist').InitialOptionsTsJest} */ +module.exports = { + displayName: 'babel-cjs-file', + roots: ['', '/../__tests__/for-babel'], + globals: { + 'ts-jest': { + babelConfig: '/babel.config.cjs', + isolatedModules: true, + }, + }, + moduleNameMapper: { + '@babel/core': '/../../../node_modules/@babel/core', + 'babel-jest': '/../../../node_modules/babel-jest', + }, + transform: { + '^.+.[tj]sx?$': '/../../../dist/index.js', + }, +} diff --git a/e2e/transform-js/babel-file/tsconfig.json b/e2e/transform-js/babel-cjs-file/tsconfig.json similarity index 100% rename from e2e/transform-js/babel-file/tsconfig.json rename to e2e/transform-js/babel-cjs-file/tsconfig.json diff --git a/e2e/transform-js/babel-js-file/babel.config.js b/e2e/transform-js/babel-js-file/babel.config.js new file mode 100644 index 0000000000..e6196ef3d3 --- /dev/null +++ b/e2e/transform-js/babel-js-file/babel.config.js @@ -0,0 +1,3 @@ +module.exports = { + presets: ['@babel/preset-env'], +} diff --git a/e2e/transform-js/babel-file/jest.config.js b/e2e/transform-js/babel-js-file/jest.config.js similarity index 93% rename from e2e/transform-js/babel-file/jest.config.js rename to e2e/transform-js/babel-js-file/jest.config.js index 1bbf819fe6..353825c30e 100644 --- a/e2e/transform-js/babel-file/jest.config.js +++ b/e2e/transform-js/babel-js-file/jest.config.js @@ -1,6 +1,6 @@ /** @type {import('../../../dist').InitialOptionsTsJest} */ module.exports = { - displayName: 'babel-file', + displayName: 'babel-js-file', roots: ['', '/../__tests__/for-babel'], globals: { 'ts-jest': { diff --git a/e2e/transform-js/babel-js-file/tsconfig.json b/e2e/transform-js/babel-js-file/tsconfig.json new file mode 100644 index 0000000000..0421216283 --- /dev/null +++ b/e2e/transform-js/babel-js-file/tsconfig.json @@ -0,0 +1,6 @@ +{ + "extends": "../tsconfig.json", + "compilerOptions": { + "module": "ESNext" + } +} diff --git a/e2e/transform-js/jest-babel.config.js b/e2e/transform-js/jest-babel.config.js index d40dbcf2a6..9912d4b5bb 100644 --- a/e2e/transform-js/jest-babel.config.js +++ b/e2e/transform-js/jest-babel.config.js @@ -1,3 +1,3 @@ module.exports = { - projects: ['babel-enabled', 'babel-file'], + projects: ['babel-enabled', 'babel-js-file', 'babel-cjs-file'], } diff --git a/src/__mocks__/babel-foo.config.cjs b/src/__mocks__/babel-foo.config.cjs new file mode 100644 index 0000000000..e7d514d354 --- /dev/null +++ b/src/__mocks__/babel-foo.config.cjs @@ -0,0 +1,3 @@ +module.exports = { + presets: ['@babel/preset-env', '@babel/preset-typescript', '@babel/preset-react'], +} diff --git a/src/config/config-set.spec.ts b/src/config/config-set.spec.ts index e8fe8c20c2..f669bc67b3 100644 --- a/src/config/config-set.spec.ts +++ b/src/config/config-set.spec.ts @@ -275,35 +275,37 @@ describe('babelJestTransformer', () => { expect(typeof babelJest.process).toBe('function') }) - it('should return babelJestTransformer with javascript file path', () => { - const FILE = 'src/__mocks__/babel-foo.config.js' - const cs = createConfigSet({ - jestConfig: { - globals: { - 'ts-jest': { - babelConfig: FILE, + it.each(['src/__mocks__/babel-foo.config.js', 'src/__mocks__/babel-foo.config.cjs'])( + 'should return babelJestTransformer with javascript file path', + (babelFilePath) => { + const cs = createConfigSet({ + jestConfig: { + globals: { + 'ts-jest': { + babelConfig: babelFilePath, + }, }, }, - }, - resolve: null, - }) - const babelJest = cs.babelJestTransformer as Transformer + resolve: null, + }) + const babelJest = cs.babelJestTransformer as Transformer - // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - const babelCfg = cs.babelConfig! - expect(babelCfg.cwd).toEqual(cs.cwd) - expect(babelCfg.presets).toMatchInlineSnapshot(` + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + const babelCfg = cs.babelConfig! + expect(babelCfg.cwd).toEqual(cs.cwd) + expect(babelCfg.presets).toMatchInlineSnapshot(` Array [ "@babel/preset-env", "@babel/preset-typescript", "@babel/preset-react", ] `) - expect(babelJest.canInstrument).toBe(true) - expect(babelJest.createTransformer).toBeUndefined() - expect(typeof babelJest.getCacheKey).toBe('function') - expect(typeof babelJest.process).toBe('function') - }) + expect(babelJest.canInstrument).toBe(true) + expect(babelJest.createTransformer).toBeUndefined() + expect(typeof babelJest.getCacheKey).toBe('function') + expect(typeof babelJest.process).toBe('function') + }, + ) it('should return babelJestTransformer with loaded config object', () => { /* eslint-disable-next-line jest/no-mocks-import */ diff --git a/src/config/config-set.ts b/src/config/config-set.ts index 485736be70..3f030bf98b 100644 --- a/src/config/config-set.ts +++ b/src/config/config-set.ts @@ -242,7 +242,8 @@ export class ConfigSet { const baseBabelCfg = { cwd: this.cwd } if (typeof options.babelConfig === 'string') { const babelCfgPath = this.resolvePath(options.babelConfig) - if (extname(options.babelConfig) === '.js') { + const babelFileExtName = extname(options.babelConfig) + if (babelFileExtName === '.js' || babelFileExtName === '.cjs') { this.babelConfig = { ...baseBabelCfg, ...require(babelCfgPath),