Skip to content

Commit d99aa01

Browse files
authored
fix: support Babel config file with .cjs extension (#3361)
Closes #3335
1 parent 02ab4da commit d99aa01

10 files changed

Lines changed: 56 additions & 23 deletions

File tree

File renamed without changes.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/** @type {import('../../../dist').InitialOptionsTsJest} */
2+
module.exports = {
3+
displayName: 'babel-cjs-file',
4+
roots: ['<rootDir>', '<rootDir>/../__tests__/for-babel'],
5+
globals: {
6+
'ts-jest': {
7+
babelConfig: '<rootDir>/babel.config.cjs',
8+
isolatedModules: true,
9+
},
10+
},
11+
moduleNameMapper: {
12+
'@babel/core': '<rootDir>/../../../node_modules/@babel/core',
13+
'babel-jest': '<rootDir>/../../../node_modules/babel-jest',
14+
},
15+
transform: {
16+
'^.+.[tj]sx?$': '<rootDir>/../../../dist/index.js',
17+
},
18+
}
File renamed without changes.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = {
2+
presets: ['@babel/preset-env'],
3+
}

e2e/transform-js/babel-file/jest.config.js renamed to e2e/transform-js/babel-js-file/jest.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/** @type {import('../../../dist').InitialOptionsTsJest} */
22
module.exports = {
3-
displayName: 'babel-file',
3+
displayName: 'babel-js-file',
44
roots: ['<rootDir>', '<rootDir>/../__tests__/for-babel'],
55
globals: {
66
'ts-jest': {
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"extends": "../tsconfig.json",
3+
"compilerOptions": {
4+
"module": "ESNext"
5+
}
6+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
module.exports = {
2-
projects: ['babel-enabled', 'babel-file'],
2+
projects: ['babel-enabled', 'babel-js-file', 'babel-cjs-file'],
33
}

src/__mocks__/babel-foo.config.cjs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = {
2+
presets: ['@babel/preset-env', '@babel/preset-typescript', '@babel/preset-react'],
3+
}

src/config/config-set.spec.ts

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -274,35 +274,37 @@ describe('babelJestTransformer', () => {
274274
expect(typeof babelJest.process).toBe('function')
275275
})
276276

277-
it('should return babelJestTransformer with javascript file path', () => {
278-
const FILE = 'src/__mocks__/babel-foo.config.js'
279-
const cs = createConfigSet({
280-
jestConfig: {
281-
globals: {
282-
'ts-jest': {
283-
babelConfig: FILE,
277+
it.each(['src/__mocks__/babel-foo.config.js', 'src/__mocks__/babel-foo.config.cjs'])(
278+
'should return babelJestTransformer with javascript file path',
279+
(babelFilePath) => {
280+
const cs = createConfigSet({
281+
jestConfig: {
282+
globals: {
283+
'ts-jest': {
284+
babelConfig: babelFilePath,
285+
},
284286
},
285287
},
286-
},
287-
resolve: null,
288-
})
289-
const babelJest = cs.babelJestTransformer as Transformer
288+
resolve: null,
289+
})
290+
const babelJest = cs.babelJestTransformer as Transformer
290291

291-
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
292-
const babelCfg = cs.babelConfig!
293-
expect(babelCfg.cwd).toEqual(cs.cwd)
294-
expect(babelCfg.presets).toMatchInlineSnapshot(`
292+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
293+
const babelCfg = cs.babelConfig!
294+
expect(babelCfg.cwd).toEqual(cs.cwd)
295+
expect(babelCfg.presets).toMatchInlineSnapshot(`
295296
Array [
296297
"@babel/preset-env",
297298
"@babel/preset-typescript",
298299
"@babel/preset-react",
299300
]
300301
`)
301-
expect(babelJest.canInstrument).toBe(true)
302-
expect(babelJest.createTransformer).toBeUndefined()
303-
expect(typeof babelJest.getCacheKey).toBe('function')
304-
expect(typeof babelJest.process).toBe('function')
305-
})
302+
expect(babelJest.canInstrument).toBe(true)
303+
expect(babelJest.createTransformer).toBeUndefined()
304+
expect(typeof babelJest.getCacheKey).toBe('function')
305+
expect(typeof babelJest.process).toBe('function')
306+
},
307+
)
306308

307309
it('should return babelJestTransformer with loaded config object', () => {
308310
/* eslint-disable-next-line jest/no-mocks-import */

src/config/config-set.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,8 @@ export class ConfigSet {
242242
const baseBabelCfg = { cwd: this.cwd }
243243
if (typeof options.babelConfig === 'string') {
244244
const babelCfgPath = this.resolvePath(options.babelConfig)
245-
if (extname(options.babelConfig) === '.js') {
245+
const babelFileExtName = extname(options.babelConfig)
246+
if (babelFileExtName === '.js' || babelFileExtName === '.cjs') {
246247
this.babelConfig = {
247248
...baseBabelCfg,
248249
...require(babelCfgPath),

0 commit comments

Comments
 (0)