Skip to content

Commit a303981

Browse files
authored
Merge pull request #1370 from ahnpnl/fix/load-babel-config
Let babel-jest handle loading babel config
2 parents 570763e + c71e95a commit a303981

5 files changed

Lines changed: 23 additions & 41 deletions

File tree

e2e/__tests__/__snapshots__/logger.test.ts.snap

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,7 @@ Array [
4949
"[level:20] backporting config",
5050
"[level:20] normalized jest config",
5151
"[level:20] normalized ts-jest config",
52-
"[level:20] loaded module @babel/core",
53-
"[level:20] patching @babel/core",
54-
"[level:20] checking version of @babel/core: OK",
55-
"[level:20] normalized babel config",
52+
"[level:20] normalized babel config via ts-jest option",
5653
"[level:20] loaded module typescript",
5754
"[level:20] patching typescript",
5855
"[level:20] checking version of typescript: OK",
@@ -96,10 +93,7 @@ Array [
9693
"[level:20] normalized jest config",
9794
"[level:20] resolved path from babel.config.js to <cwd>/babel.config.js",
9895
"[level:20] normalized ts-jest config",
99-
"[level:20] loaded module @babel/core",
100-
"[level:20] patching @babel/core",
101-
"[level:20] checking version of @babel/core: OK",
102-
"[level:20] normalized babel config",
96+
"[level:20] normalized babel config via ts-jest option",
10397
"[level:20] loaded module typescript",
10498
"[level:20] patching typescript",
10599
"[level:20] checking version of typescript: OK",

src/__mocks__/.babelrc-foo

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,7 @@
11
{
2+
"presets": [
3+
"@babel/preset-env",
4+
"@babel/preset-typescript",
5+
"@babel/preset-react"
6+
],
27
}

src/__mocks__/babel-foo.config.js

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

src/config/config-set.spec.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { Transformer } from '@jest/transform/build/types'
22
import { Config } from '@jest/types'
33
import { testing } from 'bs-logger'
4+
import { readFileSync } from 'fs'
5+
import json5 = require('json5')
46
import { resolve } from 'path'
57
import { Diagnostic, DiagnosticCategory, ModuleKind, ParsedCommandLine, ScriptTarget } from 'typescript'
68
// tslint:disable-next-line:no-duplicate-imports
@@ -84,6 +86,7 @@ describe('tsJest', () => {
8486
expect(get().tsConfig).toEqual(EXPECTED)
8587
expect(get({ tsConfig: true }).tsConfig).toEqual(EXPECTED)
8688
})
89+
8790
it('should be correct for false', () => {
8891
expect(get({ tsConfig: false }).tsConfig).toBeUndefined()
8992
})
@@ -159,35 +162,32 @@ describe('tsJest', () => {
159162
})
160163
expect(cs.tsJest.babelConfig!.kind).toEqual('file')
161164
expect(cs.tsJest.babelConfig!.value).toContain('.babelrc-foo')
162-
expect(cs.babel?.plugins).toEqual([])
163-
expect(cs.babel?.presets).toEqual([])
165+
expect(cs.babel).toEqual(expect.objectContaining(json5.parse(readFileSync(FILE, 'utf8'))))
164166
})
165167

166168
it('should be correct for given javascript file path', () => {
167-
const FILE = 'src/__mocks__/babel-foo.config.js'
168169
const cs = createConfigSet({
169170
tsJestConfig: {
170-
babelConfig: FILE,
171+
babelConfig: 'src/__mocks__/babel-foo.config.js',
171172
},
172173
resolve: null,
173174
})
174175
expect(cs.tsJest.babelConfig!.kind).toEqual('file')
175176
expect(cs.tsJest.babelConfig!.value).toContain('babel-foo.config.js')
176-
expect(cs.babel?.plugins).toEqual([])
177-
expect(cs.babel?.presets).toEqual([])
177+
expect(cs.babel).toEqual(expect.objectContaining(require('../__mocks__/babel-foo.config')))
178178
})
179179

180180
it('should be correct for imported javascript file', () => {
181+
const babelConfig = require('../__mocks__/babel-foo.config')
181182
const cs = createConfigSet({
182183
jestConfig: { rootDir: 'src', cwd: 'src' } as any,
183184
tsJestConfig: {
184-
babelConfig: require('../__mocks__/babel-foo.config'),
185+
babelConfig,
185186
},
186187
resolve: null,
187188
})
188189
expect(cs.tsJest.babelConfig!.kind).toEqual('inline')
189-
expect(cs.babel?.plugins).toEqual([])
190-
expect(cs.babel?.presets).toEqual([])
190+
expect(cs.babel).toEqual(expect.objectContaining(babelConfig))
191191
})
192192

193193
it('should be correct for inline config', () => {

src/config/config-set.ts

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import { LogContexts, Logger } from 'bs-logger'
1313
import { existsSync, readFileSync, realpathSync } from 'fs'
1414
import json5 = require('json5')
1515
import { dirname, extname, isAbsolute, join, normalize, resolve } from 'path'
16-
import semver = require('semver')
1716
import {
1817
CompilerOptions,
1918
CustomTransformers,
@@ -349,24 +348,6 @@ export class ConfigSet {
349348
}
350349
}
351350

352-
private static loadConfig(base: BabelConfig): BabelConfig {
353-
// loadPartialConfig is from babel 7+, and OptionManager is backward compatible but deprecated 6 API
354-
const { OptionManager, loadPartialConfig, version } = importer.babelCore(ImportReasons.BabelJest)
355-
// cwd is only supported from babel >= 7
356-
if (version && semver.satisfies(version, '>=6 <7')) {
357-
delete base.cwd
358-
}
359-
// call babel to load options
360-
if (typeof loadPartialConfig === 'function') {
361-
const partialConfig = loadPartialConfig(base)
362-
if (partialConfig) {
363-
return partialConfig.options as BabelConfig
364-
}
365-
}
366-
367-
return new OptionManager().init(base) as BabelConfig
368-
}
369-
370351
@Memoize()
371352
get babel(): BabelConfig | undefined {
372353
const {
@@ -394,12 +375,9 @@ export class ConfigSet {
394375
} else if (babelConfig.kind === 'inline') {
395376
base = { ...base, ...babelConfig.value }
396377
}
378+
this.logger.debug({ babelConfig: base }, 'normalized babel config via ts-jest option')
397379

398-
// call babel to load options
399-
const config = ConfigSet.loadConfig(base)
400-
401-
this.logger.debug({ babelConfig: config }, 'normalized babel config')
402-
return config
380+
return base
403381
}
404382

405383
@Memoize()

0 commit comments

Comments
 (0)