From e0895da602d2dff5ac39134f6e16114e1c7b8baa Mon Sep 17 00:00:00 2001 From: Ahn Date: Sat, 1 Feb 2020 02:41:26 +0400 Subject: [PATCH 1/6] fix(config): pass plain config object to babel-jest transformer --- src/config/config-set.ts | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/src/config/config-set.ts b/src/config/config-set.ts index 0d3144969c..10b4fe8dda 100644 --- a/src/config/config-set.ts +++ b/src/config/config-set.ts @@ -394,12 +394,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() From dff1332667b9520d48d81a58011d80f39032f973 Mon Sep 17 00:00:00 2001 From: Ahn Date: Sat, 1 Feb 2020 02:44:04 +0400 Subject: [PATCH 2/6] fix(config): comment out unused codes --- src/config/config-set.ts | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/src/config/config-set.ts b/src/config/config-set.ts index 10b4fe8dda..0d03f69cf1 100644 --- a/src/config/config-set.ts +++ b/src/config/config-set.ts @@ -349,23 +349,23 @@ 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 - } + // 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 { From 81897e1cf21f583f970200de8018b8e21f551266 Mon Sep 17 00:00:00 2001 From: Ahn Date: Sat, 1 Feb 2020 02:46:37 +0400 Subject: [PATCH 3/6] fix(config): remove unused import --- src/config/config-set.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/src/config/config-set.ts b/src/config/config-set.ts index 0d03f69cf1..cf914480a2 100644 --- a/src/config/config-set.ts +++ b/src/config/config-set.ts @@ -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, From 0e1190d396f290fb69efe1fbd031d9092e0a2a03 Mon Sep 17 00:00:00 2001 From: Ahn Date: Sat, 1 Feb 2020 03:09:57 +0400 Subject: [PATCH 4/6] fix(test): update snapshot --- e2e/__tests__/__snapshots__/logger.test.ts.snap | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/e2e/__tests__/__snapshots__/logger.test.ts.snap b/e2e/__tests__/__snapshots__/logger.test.ts.snap index 2a052c5fa9..82c1d4ab21 100644 --- a/e2e/__tests__/__snapshots__/logger.test.ts.snap +++ b/e2e/__tests__/__snapshots__/logger.test.ts.snap @@ -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", @@ -96,10 +93,7 @@ Array [ "[level:20] normalized jest config", "[level:20] resolved path from babel.config.js to /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", From ebd9380766b4d914fb7acc9b31f91c92659a1ba1 Mon Sep 17 00:00:00 2001 From: Ahn Date: Mon, 3 Feb 2020 10:33:34 +0700 Subject: [PATCH 5/6] fix(test): improve unit tests for babel config --- src/__mocks__/.babelrc-foo | 5 +++++ src/__mocks__/babel-foo.config.js | 5 +++++ src/config/config-set.spec.ts | 18 +++++++++--------- 3 files changed, 19 insertions(+), 9 deletions(-) diff --git a/src/__mocks__/.babelrc-foo b/src/__mocks__/.babelrc-foo index 2c63c08510..2bc7e58738 100644 --- a/src/__mocks__/.babelrc-foo +++ b/src/__mocks__/.babelrc-foo @@ -1,2 +1,7 @@ { + "presets": [ + "@babel/preset-env", + "@babel/preset-typescript", + "@babel/preset-react" + ], } diff --git a/src/__mocks__/babel-foo.config.js b/src/__mocks__/babel-foo.config.js index 631f375658..189fb2552a 100644 --- a/src/__mocks__/babel-foo.config.js +++ b/src/__mocks__/babel-foo.config.js @@ -1,2 +1,7 @@ module.exports = { + presets: [ + '@babel/preset-env', + '@babel/preset-typescript', + '@babel/preset-react', + ], } diff --git a/src/config/config-set.spec.ts b/src/config/config-set.spec.ts index 1fc47ac7fe..42f0be106c 100644 --- a/src/config/config-set.spec.ts +++ b/src/config/config-set.spec.ts @@ -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 @@ -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() }) @@ -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', () => { From 7aa39284a1fab0bdfb55208f29ef1dab1bff5546 Mon Sep 17 00:00:00 2001 From: Ahn Date: Mon, 3 Feb 2020 10:52:11 +0700 Subject: [PATCH 6/6] fix(config): remove unused codes --- src/config/config-set.ts | 18 ------------------ 1 file changed, 18 deletions(-) diff --git a/src/config/config-set.ts b/src/config/config-set.ts index cf914480a2..004a19f953 100644 --- a/src/config/config-set.ts +++ b/src/config/config-set.ts @@ -348,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 {