From c10eb6d6641f62c649ea3c65a34635f379efa21c Mon Sep 17 00:00:00 2001 From: Jed Mao Date: Tue, 28 Apr 2020 11:37:05 -0500 Subject: [PATCH] feat(config): add `tsconfig` alias to `tsConfig` option (#1565) Co-authored-by: Ahn --- docs/user/config/index.md | 8 ++--- docs/user/config/tsConfig.md | 10 +++--- src/config/config-set.spec.ts | 4 +-- src/config/config-set.ts | 4 +-- src/types.ts | 68 ++++++++++++++++++++++++----------- 5 files changed, 62 insertions(+), 32 deletions(-) diff --git a/docs/user/config/index.md b/docs/user/config/index.md index c7ee515358..5e06e4fba9 100644 --- a/docs/user/config/index.md +++ b/docs/user/config/index.md @@ -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] | `boolean` | _disabled_ | +| [**`diagnostics`**][diagnostics] | [Diagnostics related configuration.][diagnostics] | `boolean`\|`object` | _enabled_ | +| [**`babelConfig`**][babelConfig] | [Babel(Jest) related configuration.][babelConfig] | `boolean`\|`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_ | diff --git a/docs/user/config/tsConfig.md b/docs/user/config/tsConfig.md index 9c0a93d034..218ebc4cf5 100644 --- a/docs/user/config/tsConfig.md +++ b/docs/user/config/tsConfig.md @@ -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`. @@ -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`.
@@ -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`.
@@ -122,3 +122,5 @@ module.exports = { ```
+ +[compiler options]: https://www.typescriptlang.org/docs/handbook/compiler-options.html#compiler-options diff --git a/src/config/config-set.spec.ts b/src/config/config-set.spec.ts index 887627d33f..6f4a250141 100644 --- a/src/config/config-set.spec.ts +++ b/src/config/config-set.spec.ts @@ -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', () => { diff --git a/src/config/config-set.ts b/src/config/config-set.ts index 1892cf9413..f0c7e8eca8 100644 --- a/src/config/config-set.ts +++ b/src/config/config-set.ts @@ -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, diff --git a/src/types.ts b/src/types.ts index 1b03b5e870..6061dba0d6 100644 --- a/src/types.ts +++ b/src/types.ts @@ -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 ( 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 ( 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 ( 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 ( 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 ( 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 @@ -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 ( 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 ( can be used) + * - `{...}`: an object with inline babel options + * @default undefined does NOT use babel */ babelConfig?: boolean | string | BabelConfig