From ae56d56d06d3c0468134d96014512923f0b57898 Mon Sep 17 00:00:00 2001 From: Ahn Date: Mon, 8 Feb 2021 12:16:06 +0100 Subject: [PATCH] feat(config): add interface `InitialOptionsTsJest` for jest config in ts --- .../typings/jest-config-typings.spec.ts | 14 +++++ e2e/__tests__/ts-jest-typings.test.ts | 13 +++++ src/types.ts | 9 ++++ website/docs/getting-started/installation.md | 2 +- website/docs/getting-started/options.md | 54 ++++++++++++------- website/src/pages/index.js | 2 +- .../version-26.5/getting-started/options.md | 39 +++++++------- .../version-26.5/guides/esm-support.md | 2 +- 8 files changed, 95 insertions(+), 40 deletions(-) create mode 100644 e2e/__cases__/typings/jest-config-typings.spec.ts create mode 100644 e2e/__tests__/ts-jest-typings.test.ts diff --git a/e2e/__cases__/typings/jest-config-typings.spec.ts b/e2e/__cases__/typings/jest-config-typings.spec.ts new file mode 100644 index 0000000000..da1757e001 --- /dev/null +++ b/e2e/__cases__/typings/jest-config-typings.spec.ts @@ -0,0 +1,14 @@ +import type {InitialOptionsTsJest} from 'ts-jest/dist/types' + +test('Typings for jest config to support full TypeScript mode', () => { + const config: InitialOptionsTsJest = { + globals: { + 'ts-jest': { + isolatedModules: true, + } + }, + verbose: true, + }; + + expect(config).toBeTruthy() +}) diff --git a/e2e/__tests__/ts-jest-typings.test.ts b/e2e/__tests__/ts-jest-typings.test.ts new file mode 100644 index 0000000000..027d01c823 --- /dev/null +++ b/e2e/__tests__/ts-jest-typings.test.ts @@ -0,0 +1,13 @@ +import { PackageSets } from '../__helpers__/templates' +import { configureTestCase } from '../__helpers__/test-case' + +describe('ts-jest utils', () => { + const testCase = configureTestCase('typings') + + testCase.runWithTemplates([PackageSets.default], 0, (runTest, { testLabel }) => { + it(testLabel, () => { + const result = runTest() + expect(result.status).toBe(0) + }) + }) +}) diff --git a/src/types.ts b/src/types.ts index a1ec6d6757..9bd2375369 100644 --- a/src/types.ts +++ b/src/types.ts @@ -139,6 +139,14 @@ export interface TsJestGlobalOptions { useESM?: boolean } +export interface GlobalConfigTsJest extends Config.ConfigGlobals { + 'ts-jest': TsJestGlobalOptions +} + +export interface InitialOptionsTsJest extends Config.InitialOptions { + globals?: GlobalConfigTsJest +} + interface TsJestConfig$tsConfig$file { kind: 'file' value: string | undefined @@ -189,6 +197,7 @@ export interface TransformOptionsTsJest extends TransformOptions { } export type ResolvedModulesMap = Map | undefined + /** * @internal */ diff --git a/website/docs/getting-started/installation.md b/website/docs/getting-started/installation.md index e67daabb08..1c63242684 100644 --- a/website/docs/getting-started/installation.md +++ b/website/docs/getting-started/installation.md @@ -29,7 +29,7 @@ Tip: If you get an error with the following `npm` commands such as `npx: command #### Creating -By default Jest can run without any config files, but it will not compile `.ts` files. +By default, Jest can run without any config files, but it will not compile `.ts` files. To make it transpile TypeScript with `ts-jest`, we will need to create a configuration file that will tell Jest to use a `ts-jest` preset. `ts-jest` can create the configuration file for you automatically: diff --git a/website/docs/getting-started/options.md b/website/docs/getting-started/options.md index 58f1332901..7cca457646 100644 --- a/website/docs/getting-started/options.md +++ b/website/docs/getting-started/options.md @@ -5,22 +5,11 @@ title: Options ### Introduction -All `ts-jest` specific options are located under the `globals.ts-jest` path of your Jest config: - -```js -// jest.config.js -module.exports = { - // [...] - globals: { - 'ts-jest': { - // ts-jest configuration goes here - }, - }, -} -``` +All `ts-jest` specific options are located under the `globals` of Jest config object in the `package.json` file of your project, +or through a `jest.config.js`, or `jest.config.ts` file. ```json -// OR package.json +// package.json { // [...] "jest": { @@ -33,14 +22,27 @@ module.exports = { } ``` -#### IDE `ts-jest` config suggestion +Or through JavaScript: + +```js +// jest.config.js +module.exports = { + // [...] + globals: { + 'ts-jest': { + // ts-jest configuration goes here + }, + }, +} +``` + +:::tip To utilize IDE suggestions, you can use `JSDoc` comments to provide suggested `ts-jest` configs for your Jest config: ```js -/** @typedef {import('ts-jest/dist/types')} */ -/** @type {import('@jest/types').Config.InitialOptions} */ -const config = { +/** @type {import('ts-jest/dist/types').InitialOptionsTsJest} */ +module.exports = config = { // [...] globals: { 'ts-jest': { @@ -48,8 +50,22 @@ const config = { }, }, } +``` + +::: + +Or through TypeScript (if `ts-node` is installed): -module.exports = config +```ts +// jest.config.ts +import type { InitialOptionsTsJest } from 'ts-jest/dist/types' + +const config: InitialOptionsTsJests = { + 'ts-jest': { + // ts-jest configuration goes here + }, +} +export default config ``` ### Options diff --git a/website/src/pages/index.js b/website/src/pages/index.js index 0cbaee1d13..5f00a05048 100644 --- a/website/src/pages/index.js +++ b/website/src/pages/index.js @@ -19,7 +19,7 @@ const features = [ }, { title: 'Babel support', - description: <>Working seamlessly with Babel, + description: <>Support working in combination with Babel, }, ] diff --git a/website/versioned_docs/version-26.5/getting-started/options.md b/website/versioned_docs/version-26.5/getting-started/options.md index 25747302ea..b03c374b28 100644 --- a/website/versioned_docs/version-26.5/getting-started/options.md +++ b/website/versioned_docs/version-26.5/getting-started/options.md @@ -5,22 +5,11 @@ title: Options ### Introduction -All `ts-jest` specific options are located under the `globals.ts-jest` path of your Jest config: - -```js -// jest.config.js -module.exports = { - // [...] - globals: { - 'ts-jest': { - // ts-jest configuration goes here - }, - }, -} -``` +All `ts-jest` specific options are located under the `globals` of Jest config object in the `package.json` file of your project, +or through a `jest.config.js`, or `jest.config.ts` file. ```json -// OR package.json +// package.json { // [...] "jest": { @@ -33,14 +22,28 @@ module.exports = { } ``` -#### IDE `ts-jest` config suggestion +Or through JavaScript: + +```js +// jest.config.js +module.exports = { + // [...] + globals: { + 'ts-jest': { + // ts-jest configuration goes here + }, + }, +} +``` + +:::tip To utilize IDE suggestions, you can use `JSDoc` comments to provide suggested `ts-jest` configs for your Jest config: ```js /** @typedef {import('ts-jest/dist/types')} */ /** @type {import('@jest/types').Config.InitialOptions} */ -const config = { +module.exports = config = { // [...] globals: { 'ts-jest': { @@ -48,10 +51,10 @@ const config = { }, }, } - -module.exports = config ``` +::: + ### Options All options have default values which should fit most of the projects. Click on the option's name to see details and example(s). diff --git a/website/versioned_docs/version-26.5/guides/esm-support.md b/website/versioned_docs/version-26.5/guides/esm-support.md index 9adf87a23d..423a2292f3 100644 --- a/website/versioned_docs/version-26.5/guides/esm-support.md +++ b/website/versioned_docs/version-26.5/guides/esm-support.md @@ -5,6 +5,6 @@ title: ESM Support :::important -ESM support is only available in **v27** +ESM support is only available in **v27++** :::