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(config): resolve .babelrc file before attempting to read it #2071

Merged
merged 1 commit into from Oct 26, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 14 additions & 5 deletions src/config/config-set.spec.ts
Expand Up @@ -199,7 +199,7 @@ describe('customTransformers', () => {
} as any,
logger,
tsJestConfig: {
astTransformers: ['dummy-transformer'],
astTransformers: ['<rootDir>/__mocks__/dummy-transformer'],
},
resolve: null,
}).customTransformers,
Expand Down Expand Up @@ -241,7 +241,7 @@ describe('babelJestTransformer', () => {
expect(babelJest).toBeUndefined()
})

it('should return babelJestTransformer with babalConfig is true', () => {
it('should return babelJestTransformer with babelConfig is true', () => {
const cs = createConfigSet({
jestConfig: {
rootDir: 'src',
Expand All @@ -265,15 +265,24 @@ describe('babelJestTransformer', () => {
expect(typeof babelJest.process).toBe('function')
})

it('should return babelJestTransformer with non javascript file path', () => {
const FILE = 'src/__mocks__/.babelrc-foo'
it.each([
{
path: 'src/__mocks__/.babelrc-foo',
rootDir: './',
},
{
path: '<rootDir>/.babelrc-foo',
rootDir: 'src/__mocks__/',
},
])('should return babelJestTransformer with non javascript file path', (data) => {
const cs = createConfigSet({
jestConfig: {
globals: {
'ts-jest': {
babelConfig: FILE,
babelConfig: data.path,
},
},
rootDir: data.rootDir,
},
resolve: null,
})
Expand Down
5 changes: 3 additions & 2 deletions src/config/config-set.ts
Expand Up @@ -219,15 +219,16 @@ export class ConfigSet {
} else {
const baseBabelCfg = { cwd: this.cwd }
if (typeof options.babelConfig === 'string') {
const babelCfgPath = this.resolvePath(options.babelConfig)
if (extname(options.babelConfig) === '.js') {
this._babelConfig = {
...baseBabelCfg,
...require(this.resolvePath(options.babelConfig, { nodeResolve: true })),
...require(babelCfgPath),
}
} else {
this._babelConfig = {
...baseBabelCfg,
...json5.parse(readFileSync(options.babelConfig, 'utf-8')),
...json5.parse(readFileSync(babelCfgPath, 'utf-8')),
}
}
} else if (typeof options.babelConfig === 'object') {
Expand Down