Skip to content

Commit 5e5ac4a

Browse files
ahnpnlanh.pham
authored and
anh.pham
committed
fix: support Babel config file with .cjs extension (#3361)
Closes #3335
1 parent 2b7dffe commit 5e5ac4a

File tree

10 files changed

+56
-23
lines changed

10 files changed

+56
-23
lines changed
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+
}
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+
}

e2e/transform-js/jest-babel.config.js

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
@@ -275,35 +275,37 @@ describe('babelJestTransformer', () => {
275275
expect(typeof babelJest.process).toBe('function')
276276
})
277277

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

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(`
293+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
294+
const babelCfg = cs.babelConfig!
295+
expect(babelCfg.cwd).toEqual(cs.cwd)
296+
expect(babelCfg.presets).toMatchInlineSnapshot(`
296297
Array [
297298
"@babel/preset-env",
298299
"@babel/preset-typescript",
299300
"@babel/preset-react",
300301
]
301302
`)
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-
})
303+
expect(babelJest.canInstrument).toBe(true)
304+
expect(babelJest.createTransformer).toBeUndefined()
305+
expect(typeof babelJest.getCacheKey).toBe('function')
306+
expect(typeof babelJest.process).toBe('function')
307+
},
308+
)
307309

308310
it('should return babelJestTransformer with loaded config object', () => {
309311
/* 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)