Skip to content

Commit

Permalink
Merge pull request #1370 from ahnpnl/fix/load-babel-config
Browse files Browse the repository at this point in the history
Let babel-jest handle loading babel config
  • Loading branch information
kulshekhar authored Feb 3, 2020
2 parents 570763e + c71e95a commit a303981
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 41 deletions.
10 changes: 2 additions & 8 deletions e2e/__tests__/__snapshots__/logger.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,7 @@ Array [
"[level:20] backporting config",
"[level:20] normalized jest config",
"[level:20] normalized ts-jest config",
"[level:20] loaded module @babel/core",
"[level:20] patching @babel/core",
"[level:20] checking version of @babel/core: OK",
"[level:20] normalized babel config",
"[level:20] normalized babel config via ts-jest option",
"[level:20] loaded module typescript",
"[level:20] patching typescript",
"[level:20] checking version of typescript: OK",
Expand Down Expand Up @@ -96,10 +93,7 @@ Array [
"[level:20] normalized jest config",
"[level:20] resolved path from babel.config.js to <cwd>/babel.config.js",
"[level:20] normalized ts-jest config",
"[level:20] loaded module @babel/core",
"[level:20] patching @babel/core",
"[level:20] checking version of @babel/core: OK",
"[level:20] normalized babel config",
"[level:20] normalized babel config via ts-jest option",
"[level:20] loaded module typescript",
"[level:20] patching typescript",
"[level:20] checking version of typescript: OK",
Expand Down
5 changes: 5 additions & 0 deletions src/__mocks__/.babelrc-foo
Original file line number Diff line number Diff line change
@@ -1,2 +1,7 @@
{
"presets": [
"@babel/preset-env",
"@babel/preset-typescript",
"@babel/preset-react"
],
}
5 changes: 5 additions & 0 deletions src/__mocks__/babel-foo.config.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,7 @@
module.exports = {
presets: [
'@babel/preset-env',
'@babel/preset-typescript',
'@babel/preset-react',
],
}
18 changes: 9 additions & 9 deletions src/config/config-set.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { Transformer } from '@jest/transform/build/types'
import { Config } from '@jest/types'
import { testing } from 'bs-logger'
import { readFileSync } from 'fs'
import json5 = require('json5')
import { resolve } from 'path'
import { Diagnostic, DiagnosticCategory, ModuleKind, ParsedCommandLine, ScriptTarget } from 'typescript'
// tslint:disable-next-line:no-duplicate-imports
Expand Down Expand Up @@ -84,6 +86,7 @@ describe('tsJest', () => {
expect(get().tsConfig).toEqual(EXPECTED)
expect(get({ tsConfig: true }).tsConfig).toEqual(EXPECTED)
})

it('should be correct for false', () => {
expect(get({ tsConfig: false }).tsConfig).toBeUndefined()
})
Expand Down Expand Up @@ -159,35 +162,32 @@ describe('tsJest', () => {
})
expect(cs.tsJest.babelConfig!.kind).toEqual('file')
expect(cs.tsJest.babelConfig!.value).toContain('.babelrc-foo')
expect(cs.babel?.plugins).toEqual([])
expect(cs.babel?.presets).toEqual([])
expect(cs.babel).toEqual(expect.objectContaining(json5.parse(readFileSync(FILE, 'utf8'))))
})

it('should be correct for given javascript file path', () => {
const FILE = 'src/__mocks__/babel-foo.config.js'
const cs = createConfigSet({
tsJestConfig: {
babelConfig: FILE,
babelConfig: 'src/__mocks__/babel-foo.config.js',
},
resolve: null,
})
expect(cs.tsJest.babelConfig!.kind).toEqual('file')
expect(cs.tsJest.babelConfig!.value).toContain('babel-foo.config.js')
expect(cs.babel?.plugins).toEqual([])
expect(cs.babel?.presets).toEqual([])
expect(cs.babel).toEqual(expect.objectContaining(require('../__mocks__/babel-foo.config')))
})

it('should be correct for imported javascript file', () => {
const babelConfig = require('../__mocks__/babel-foo.config')
const cs = createConfigSet({
jestConfig: { rootDir: 'src', cwd: 'src' } as any,
tsJestConfig: {
babelConfig: require('../__mocks__/babel-foo.config'),
babelConfig,
},
resolve: null,
})
expect(cs.tsJest.babelConfig!.kind).toEqual('inline')
expect(cs.babel?.plugins).toEqual([])
expect(cs.babel?.presets).toEqual([])
expect(cs.babel).toEqual(expect.objectContaining(babelConfig))
})

it('should be correct for inline config', () => {
Expand Down
26 changes: 2 additions & 24 deletions src/config/config-set.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import { LogContexts, Logger } from 'bs-logger'
import { existsSync, readFileSync, realpathSync } from 'fs'
import json5 = require('json5')
import { dirname, extname, isAbsolute, join, normalize, resolve } from 'path'
import semver = require('semver')
import {
CompilerOptions,
CustomTransformers,
Expand Down Expand Up @@ -349,24 +348,6 @@ export class ConfigSet {
}
}

private static loadConfig(base: BabelConfig): BabelConfig {
// loadPartialConfig is from babel 7+, and OptionManager is backward compatible but deprecated 6 API
const { OptionManager, loadPartialConfig, version } = importer.babelCore(ImportReasons.BabelJest)
// cwd is only supported from babel >= 7
if (version && semver.satisfies(version, '>=6 <7')) {
delete base.cwd
}
// call babel to load options
if (typeof loadPartialConfig === 'function') {
const partialConfig = loadPartialConfig(base)
if (partialConfig) {
return partialConfig.options as BabelConfig
}
}

return new OptionManager().init(base) as BabelConfig
}

@Memoize()
get babel(): BabelConfig | undefined {
const {
Expand Down Expand Up @@ -394,12 +375,9 @@ export class ConfigSet {
} else if (babelConfig.kind === 'inline') {
base = { ...base, ...babelConfig.value }
}
this.logger.debug({ babelConfig: base }, 'normalized babel config via ts-jest option')

// call babel to load options
const config = ConfigSet.loadConfig(base)

this.logger.debug({ babelConfig: config }, 'normalized babel config')
return config
return base
}

@Memoize()
Expand Down

0 comments on commit a303981

Please sign in to comment.