Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: support Babel config file with .cjs extension #3361

Merged
merged 1 commit into from
Mar 23, 2022
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
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',
},
}
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 @@ -274,35 +274,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