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 testing utils & cache digest & docs related to presets #787

Merged
merged 9 commits into from
Oct 6, 2018
Prev Previous commit
style(cli): more readable code when setting defaults for init
  • Loading branch information
huafu committed Oct 6, 2018
commit 89ad06a6ed449461eef1962a51c0c8f2c7f0cf6f
24 changes: 14 additions & 10 deletions src/cli/config/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,21 @@ export const run: CliCommand = async (args: Arguments /* , logger: Logger */) =>
const pkgJson = hasPackage ? JSON.parse(readFileSync(pkgFile, 'utf8')) : {}

// auto js/babel
let { js, babel } = args
if (js != null || babel != null) {
if (js == null) js = babel ? 'babel' : undefined
else if (babel == null) babel = js === 'babel'
let { js: jsFilesProcessor, babel: shouldPostProcessWithBabel } = args
// set defaults for missing options
if (jsFilesProcessor == null) {
// set default js files processor depending on whether the user wants to post-process with babel
jsFilesProcessor = shouldPostProcessWithBabel ? 'babel' : undefined
} else if (shouldPostProcessWithBabel == null) {
// auto enables babel post-processing if the user wants babel to process js files
shouldPostProcessWithBabel = jsFilesProcessor === 'babel'
}

// preset
let preset: TsJestPresetDescriptor | undefined
if (js === 'babel') {
if (jsFilesProcessor === 'babel') {
preset = jsWIthBabel
} else if (js === 'ts') {
} else if (jsFilesProcessor === 'ts') {
preset = jsWithTs
} else {
preset = defaults
Expand Down Expand Up @@ -70,11 +74,11 @@ export const run: CliCommand = async (args: Arguments /* , logger: Logger */) =>
// package.json config
const base: any = jestPreset ? { preset: preset.name } : { ...preset.value }
if (!jsdom) base.testEnvironment = 'node'
if (tsconfig || babel) {
if (tsconfig || shouldPostProcessWithBabel) {
const tsJestConf: any = {}
base.globals = { 'ts-jest': tsJestConf }
if (tsconfig) tsJestConf.tsconfig = tsconfig
if (babel) tsJestConf.babelConfig = true
if (shouldPostProcessWithBabel) tsJestConf.babelConfig = true
}
body = JSON.stringify({ ...pkgJson, jest: base }, undefined, ' ')
} else {
Expand All @@ -91,11 +95,11 @@ export const run: CliCommand = async (args: Arguments /* , logger: Logger */) =>
}
if (!jsdom) content.push(` testEnvironment: 'node',`)

if (tsconfig || babel) {
if (tsconfig || shouldPostProcessWithBabel) {
content.push(` globals: {`)
content.push(` 'ts-jest': {`)
if (tsconfig) content.push(` tsconfig: ${stringifyJson5(tsconfig)},`)
if (babel) content.push(` babelConfig: true,`)
if (shouldPostProcessWithBabel) content.push(` babelConfig: true,`)
content.push(` },`)
content.push(` },`)
}
Expand Down