Skip to content

Commit

Permalink
feat: add tsconfig alias to tsConfig option
Browse files Browse the repository at this point in the history
  • Loading branch information
jednano authored and Jed Mao committed Apr 28, 2020
1 parent 7f85bab commit 8e5091c
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 32 deletions.
8 changes: 4 additions & 4 deletions docs/user/config/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -188,10 +188,10 @@ All options have default values which should fit most of the projects. Click on
| Option | Description | Type | Default |
|---|---|---|---|
| [**`compiler`**][compiler] | [TypeScript module to use as compiler.][compiler] | `string` | `"typescript"` |
| [**`tsConfig`**][tsConfig] | [TypeScript compiler related configuration.][tsConfig] | `string`\|`object`\|`boolean` | _auto_ |
| [**`isolatedModules`**][isolatedModules] | [Disable type-checking][isolatedModules] | `boolean` | `false` |
| [**`diagnostics`**][diagnostics] | [Diagnostics related configuration.][diagnostics] | `boolean`\|`object` | `true` |
| [**`babelConfig`**][babelConfig] | [Babel(Jest) related configuration.][babelConfig] | `boolean`\|`object` | _disabled_ |
| [**`tsConfig` or `tsconfig`**][tsConfig] | [TypeScript compiler related configuration.][tsConfig] | `string`\|`object`\|`boolean` | _auto_ |
| [**`isolatedModules`**][isolatedModules] | [Disable type-checking][isolatedModules] | `true` | _disabled_ |
| [**`diagnostics`**][diagnostics] | [Diagnostics related configuration.][diagnostics] | `boolean`\|`object` | _enabled_ |
| [**`babelConfig`**][babelConfig] | [Babel(Jest) related configuration.][babelConfig] | `true`\|`string`\|`object` | _disabled_ |
| [**`stringifyContentPathRegex`**][stringifyContentPathRegex] | [Files which will become modules returning self content.][stringifyContentPathRegex] | `string`\|`RegExp` | _disabled_ |
| [**`packageJson`**][packageJson] | [Package metadata.][packageJson] | `string`\|`object`\|`boolean` | _auto_ |

Expand Down
10 changes: 6 additions & 4 deletions docs/user/config/tsConfig.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
title: TypeScript Config option
---

The `tsConfig` option allows you to define the which `tsconfig` JSON file to use. An inline compiler options object can also be specified instead of the path to a file.
The `tsConfig` option (alias `tsconfig`) allows you to define which `tsconfig` JSON file to use. An inline [compiler options][] object can also be specified instead of a file path.

By default, it'll use the default TypeScript and use the project's `tsconfig.json` file. If it cannot find one, it'll use defaults TypeScript compiler options (except `es5` is used as target instead of `es3`).
By default `ts-jest` will try to find a `tsconfig.json` in your project. If it cannot find one, it will use the default TypeScript [compiler options][]; except, `ES5` is used as `target` instead of `ES3`.

If you need to use defaults and force `ts-jest` to use the defaults even if there is a `tsconfig.json` in your project, you can set this option to `false`.

Expand Down Expand Up @@ -48,7 +48,7 @@ module.exports = {

#### Inline compiler options

Refer to the [TypeScript compiler options](https://www.typescriptlang.org/docs/handbook/compiler-options.html) to know what can be used.
Refer to the TypeScript [compiler options][] for reference.
It's basically the same object you'd put in your `tsconfig.json`'s `compilerOptions`.

<div class="row"><div class="col-md-6" markdown="block">
Expand Down Expand Up @@ -89,7 +89,7 @@ module.exports = {

#### Disable auto-lookup

By default `ts-jest` will try to find the `tsconfig.json` in your project. But you may want to not use it at all and keep TypeScript default options. You can achieve this by setting `tsConfig` to `false`.
By default `ts-jest` will try to find a `tsconfig.json` in your project. But you may not want to use it at all and keep TypeScript default options. You can achieve this by setting `tsConfig` to `false`.

<div class="row"><div class="col-md-6" markdown="block">

Expand Down Expand Up @@ -122,3 +122,5 @@ module.exports = {
```

</div></div>

[compiler options]: https://www.typescriptlang.org/docs/handbook/compiler-options.html#compiler-options
4 changes: 2 additions & 2 deletions src/config/config-set.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -478,8 +478,8 @@ describe('typescript', () => {
expect(get().fileNames).toContain(normalizeSlashes(__filename))
})

it('should include compiler config from `tsConfig` option key', () => {
expect(get({ tsConfig: { baseUrl: 'src/config' } }).options.baseUrl).toBe(normalizeSlashes(__dirname))
it.each(['tsConfig', 'tsconfig'])('should include compiler config from `%s` option key', (key: string) => {
expect(get({ [key]: { baseUrl: 'src/config' } }).options.baseUrl).toBe(normalizeSlashes(__dirname))
})

it('should include compiler config from base config', () => {
Expand Down
4 changes: 2 additions & 2 deletions src/config/config-set.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,9 +222,9 @@ export class ConfigSet {
const options: TsJestGlobalOptions = { ...globals['ts-jest'] }

// tsconfig
const { tsConfig: tsConfigOpt } = options
const tsConfigOpt = options.tsConfig ?? options.tsconfig ?? true
let tsConfig: TsJestConfig['tsConfig']
if (typeof tsConfigOpt === 'string' || tsConfigOpt == null || tsConfigOpt === true) {
if (typeof tsConfigOpt === 'string' || tsConfigOpt === true) {
tsConfig = {
kind: 'file',
value: typeof tsConfigOpt === 'string' ? this.resolvePath(tsConfigOpt) : undefined,
Expand Down
68 changes: 48 additions & 20 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,28 +30,44 @@ export type BabelConfig = _babel.TransformOptions
export interface TsJestGlobalOptions {
/**
* Compiler options. It can be:
* - `true` (or `undefined`, it's the default): use default tsconfig file
* - `false`: do NOT use default config file
* - `path/to/tsconfig.json`: path to a specific tsconfig file (<rootDir> can be used)
* - `{...}`: an object with inline compiler options
* - `true` (or `undefined`, it's the default): use default tsconfig file
* - `false`: do NOT use default config file
* - `path/to/tsconfig.json`: path to a specific tsconfig file (<rootDir> can be used)
* - `{...}`: an object with inline compiler options
* @default undefined uses the default tsconfig file
* @alias tsconfig
*/
tsConfig?: boolean | string | _ts.CompilerOptions

/**
* Compiler options. It can be:
* - `true` (or `undefined`, it's the default): use default tsconfig file
* - `false`: do NOT use default config file
* - `path/to/tsconfig.json`: path to a specific tsconfig file (<rootDir> can be used)
* - `{...}`: an object with inline compiler options
* @default undefined uses the default tsconfig file
* @alias tsConfig
*/
tsconfig?: boolean | string | _ts.CompilerOptions

/**
* packageJson. It can be:
* - `true` (or `undefined`, it's the default): use default package.json file
* - `path/to/package.json`: path to a specific package.json file (<rootDir> can be used)
* - `{...}`: contents of a package.json
* - `true` (or `undefined`, it's the default): use default package.json file
* - `path/to/package.json`: path to a specific package.json file (<rootDir> can be used)
* - `{...}`: contents of a package.json
* @default undefined uses the default package.json file
*/
packageJson?: boolean | string | object

/**
* Whether to compile files as isolated modules (disables some features and type-checking, default to `false`):
* Compiles files as isolated modules (disables some features and type-checking)
* @default undefined (disabled)
*/
isolatedModules?: boolean

/**
* Compiler to use (default to 'typescript'):
* Compiler to use
* @default 'typescript'
*/
compiler?: string

Expand All @@ -62,33 +78,45 @@ export interface TsJestGlobalOptions {

/**
* TS diagnostics - less to be reported if `isolatedModules` is `true`. It can be:
* - `true` (or `undefined`, it's the default): show all diagnostics
* - `false`: hide diagnostics of all files (kind of useless)
* - `{...}`: an inline object with fine grained settings
* - `true` (or `undefined`, it's the default): show all diagnostics
* - `false`: hide diagnostics of all files (kind of useless)
* - `{...}`: an inline object with fine grained settings
* @default undefined shows all diagnostics
*/
diagnostics?:
| boolean
| {
/**
* Enables colorful and pretty output of errors
* @default undefined (enabled)
*/
pretty?: boolean
/**
* Ignore TypeScript warnings by diagnostic code.
* List of TypeScript diagnostic error codes to ignore
* [here](https://github.com/Microsoft/TypeScript/blob/master/src/compiler/diagnosticMessages.json).
* @see https://github.com/Microsoft/TypeScript/blob/master/src/compiler/diagnosticMessages.json
* @default [6059,18002,18003]
*/
ignoreCodes?: number | string | (number | string)[]
/**
* If specified, diagnostics of source files which path does **not** match
* will be ignored
*/
pathRegex?: RegExp | string
/**
* Logs TypeScript errors to stderr instead of throwing exceptions.
*
* @default false
* Logs TypeScript errors to stderr instead of throwing exceptions
* @default undefined (disabled)
*/
warnOnly?: boolean
}

/**
* Babel config. It can be:
* - `false` (or `undefined`, it's the default): do NOT use babel
* - `true`: use babel using default babelrc file
* - `path/to/.babelrc`: path to a babelrc file (<rootDir> can be used)
* - `{...}`: an object with inline babel options
* - `false` (or `undefined`, it's the default): do NOT use babel
* - `true`: use babel using default babelrc file
* - `path/to/.babelrc`: path to a babelrc file (<rootDir> can be used)
* - `{...}`: an object with inline babel options
* @default undefined does NOT use babel
*/
babelConfig?: boolean | string | BabelConfig

Expand Down

0 comments on commit 8e5091c

Please sign in to comment.