• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

kulshekhar / ts-jest / 10066013409

23 Jul 2024 08:35PM UTC coverage: 95.254%. Remained the same
10066013409

push

github

ahnpnl
build(deps): Update dependency @testing-library/jest-dom to ^6.4.8

777 of 875 branches covered (88.8%)

Branch coverage included in aggregate %.

4763 of 4941 relevant lines covered (96.4%)

1242.48 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

84.38
/src/cli/config/init.ts
1
/**
6✔
2
 * This has been written quickly. While trying to improve I realised it'd be better to have it in Jest...
6✔
3
 * ...and I saw a merged PR with `jest --init` tool!
6✔
4
 * TODO: see what's the best path for this
6✔
5
 */
6✔
6

6✔
7
import { existsSync, readFileSync, writeFileSync } from 'fs'
6✔
8
import { basename, join } from 'path'
6✔
9

6✔
10
import ejs from 'ejs'
6✔
11
import { stringify as stringifyJson5 } from 'json5'
6✔
12

6✔
13
import type { CliCommand, CliCommandArgs } from '..'
6✔
14
import { JEST_CONFIG_EJS_TEMPLATE } from '../../constants'
6✔
15
import { createDefaultPreset, createJsWithTsPreset, createJsWithBabelPreset } from '../../presets/create-jest-preset'
6✔
16
import type { DefaultPreset, JsWithBabelPreset, JsWithTsPreset, TsJestTransformerOptions } from '../../types'
6✔
17

6✔
18
const ensureOnlyUsingDoubleQuotes = (str: string): string => {
6✔
19
  return str
54✔
20
    .replace(/"'(.*?)'"/g, '"$1"')
54✔
21
    .replace(/'ts-jest'/g, '"ts-jest"')
54✔
22
    .replace(/'babel-jest'/g, '"babel-jest"')
54✔
23
}
54✔
24

6✔
25
/**
6✔
26
 * @internal
6✔
27
 */
6✔
28
export const run: CliCommand = async (args: CliCommandArgs /* , logger: Logger */) => {
6✔
29
  const { tsconfig: askedTsconfig, force, jsdom, js: jsFilesProcessor, babel: shouldPostProcessWithBabel } = args
54✔
30
  const file = args._[0]?.toString() ?? 'jest.config.js'
54✔
31
  const filePath = join(process.cwd(), file)
54✔
32
  const name = basename(file)
54✔
33
  const isPackageJsonConfig = name === 'package.json'
54✔
34
  const isJestConfigFileExisted = existsSync(filePath)
54✔
35
  const pkgFile = isPackageJsonConfig ? filePath : join(process.cwd(), 'package.json')
54✔
36
  const isPackageJsonExisted = isPackageJsonConfig || existsSync(pkgFile)
54✔
37
  const tsconfig =
54✔
38
    askedTsconfig === 'tsconfig.json' ? undefined : (askedTsconfig as TsJestTransformerOptions['tsconfig'])
54!
39
  const pkgJsonContent = isPackageJsonExisted ? JSON.parse(readFileSync(pkgFile, 'utf8')) : {}
54!
40
  if (shouldPostProcessWithBabel) {
54!
41
    console.warn(
×
42
      `The option --babel is deprecated and will be removed in the next major version.` +
×
43
        ` Please specify 'js' option value (see more with npx ts-jest help) if you wish 'ts-jest' to process 'js' with TypeScript API or Babel.`,
×
44
    )
×
45
  }
×
46

54✔
47
  if (isPackageJsonConfig && !isJestConfigFileExisted) {
54!
48
    throw new Error(`File ${file} does not exists.`)
×
49
  } else if (!isPackageJsonConfig && isJestConfigFileExisted && !force) {
54!
50
    throw new Error(`Configuration file ${file} already exists.`)
×
51
  }
×
52
  if (!isPackageJsonConfig && !name.endsWith('.js')) {
54!
53
    throw new TypeError(`Configuration file ${file} must be a .js file or the package.json.`)
×
54
  }
×
55
  if (isPackageJsonExisted && pkgJsonContent.jest) {
54!
56
    if (force && !isPackageJsonConfig) {
×
57
      delete pkgJsonContent.jest
×
58
      writeFileSync(pkgFile, JSON.stringify(pkgJsonContent, undefined, '  '))
×
59
    } else if (!force) {
×
60
      throw new Error(`A Jest configuration is already set in ${pkgFile}.`)
×
61
    }
×
62
  }
×
63

54✔
64
  let body: string
54✔
65
  const resolvedTsconfigOption = tsconfig ? { tsconfig: `${stringifyJson5(tsconfig)}` } : undefined
54✔
66
  let transformConfig: DefaultPreset | JsWithTsPreset | JsWithBabelPreset
54✔
67
  if (jsFilesProcessor === 'babel' || shouldPostProcessWithBabel) {
54✔
68
    transformConfig = createJsWithBabelPreset(resolvedTsconfigOption)
18✔
69
  } else if (jsFilesProcessor === 'ts') {
54✔
70
    transformConfig = createJsWithTsPreset(resolvedTsconfigOption)
18✔
71
  } else {
18✔
72
    transformConfig = createDefaultPreset(resolvedTsconfigOption)
18✔
73
  }
18✔
74
  if (isPackageJsonConfig) {
54✔
75
    body = ensureOnlyUsingDoubleQuotes(
18✔
76
      JSON.stringify(
18✔
77
        {
18✔
78
          ...pkgJsonContent,
18✔
79
          jest: transformConfig,
18✔
80
        },
18✔
81
        undefined,
18✔
82
        '  ',
18✔
83
      ),
18✔
84
    )
18✔
85
  } else {
54✔
86
    const [transformPattern, transformValue] = Object.entries(transformConfig.transform)[0]
36✔
87
    body = ejs.render(JEST_CONFIG_EJS_TEMPLATE, {
36✔
88
      exportKind: pkgJsonContent.type === 'module' ? 'export default' : 'module.exports =',
36✔
89
      testEnvironment: jsdom ? 'jsdom' : 'node',
36✔
90
      transformPattern,
36✔
91
      transformValue: ensureOnlyUsingDoubleQuotes(stringifyJson5(transformValue)),
36✔
92
    })
36✔
93
  }
36✔
94

54✔
95
  writeFileSync(filePath, body)
54✔
96

54✔
97
  process.stderr.write(`
54✔
98
Jest configuration written to "${filePath}".
54✔
99
`)
54✔
100
}
54✔
101

6✔
102
/**
6✔
103
 * @internal
6✔
104
 */
6✔
105
export const help: CliCommand = async () => {
6✔
106
  process.stdout.write(`
6✔
107
Usage:
6✔
108
  ts-jest config:init [options] [<config-file>]
6✔
109

6✔
110
Arguments:
6✔
111
  <config-file>         Can be a js or json Jest config file. If it is a
6✔
112
                        package.json file, the configuration will be read from
6✔
113
                        the "jest" property.
6✔
114
                        Default: jest.config.js
6✔
115

6✔
116
Options:
6✔
117
  --force               Discard any existing Jest config
6✔
118
  --js ts|babel         Process '.js' files with ts-jest if 'ts' or with
6✔
119
                        babel-jest if 'babel'
6✔
120
  --no-jest-preset      Disable the use of Jest presets
6✔
121
  --tsconfig <file>     Path to the tsconfig.json file
6✔
122
  --babel               Enable using Babel to process 'js' resulted content from 'ts-jest' processing
6✔
123
  --jsdom               Use 'jsdom' as test environment instead of 'node'
6✔
124
`)
6✔
125
}
6✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc