Skip to content

Commit

Permalink
fix: support Babel config file with .cjs extension (#3361)
Browse files Browse the repository at this point in the history
Closes #3335
  • Loading branch information
ahnpnl authored and anh.pham committed Mar 24, 2022
1 parent 2b7dffe commit 5e5ac4a
Show file tree
Hide file tree
Showing 10 changed files with 56 additions and 23 deletions.
File renamed without changes.
18 changes: 18 additions & 0 deletions e2e/transform-js/babel-cjs-file/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/** @type {import('../../../dist').InitialOptionsTsJest} */
module.exports = {
displayName: 'babel-cjs-file',
roots: ['<rootDir>', '<rootDir>/../__tests__/for-babel'],
globals: {
'ts-jest': {
babelConfig: '<rootDir>/babel.config.cjs',
isolatedModules: true,
},
},
moduleNameMapper: {
'@babel/core': '<rootDir>/../../../node_modules/@babel/core',
'babel-jest': '<rootDir>/../../../node_modules/babel-jest',
},
transform: {
'^.+.[tj]sx?$': '<rootDir>/../../../dist/index.js',
},
}
File renamed without changes.
3 changes: 3 additions & 0 deletions e2e/transform-js/babel-js-file/babel.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
presets: ['@babel/preset-env'],
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/** @type {import('../../../dist').InitialOptionsTsJest} */
module.exports = {
displayName: 'babel-file',
displayName: 'babel-js-file',
roots: ['<rootDir>', '<rootDir>/../__tests__/for-babel'],
globals: {
'ts-jest': {
Expand Down
6 changes: 6 additions & 0 deletions e2e/transform-js/babel-js-file/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"extends": "../tsconfig.json",
"compilerOptions": {
"module": "ESNext"
}
}
2 changes: 1 addition & 1 deletion e2e/transform-js/jest-babel.config.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module.exports = {
projects: ['babel-enabled', 'babel-file'],
projects: ['babel-enabled', 'babel-js-file', 'babel-cjs-file'],
}
3 changes: 3 additions & 0 deletions src/__mocks__/babel-foo.config.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
presets: ['@babel/preset-env', '@babel/preset-typescript', '@babel/preset-react'],
}
42 changes: 22 additions & 20 deletions src/config/config-set.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down
3 changes: 2 additions & 1 deletion src/config/config-set.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down

0 comments on commit 5e5ac4a

Please sign in to comment.