From 1a3058f3b7f8a2e377cb6a7e829cea310a06d4fa Mon Sep 17 00:00:00 2001 From: Ahn Date: Tue, 9 Feb 2021 12:19:56 +0100 Subject: [PATCH] feat(presets): add typing for `presets` entry point (#2337) --- .eslintignore | 1 + e2e/__tests__/module-kinds/helpers.ts | 5 ++-- presets/index.d.ts | 11 ++++++++ src/cli/helpers/presets.ts | 2 +- src/presets/create-jest-preset.ts | 6 +---- src/types.ts | 9 ++++--- website/docs/getting-started/presets.md | 36 ++++++++++++++++++++----- 7 files changed, 52 insertions(+), 18 deletions(-) create mode 100644 presets/index.d.ts diff --git a/.eslintignore b/.eslintignore index b5fe5cd847..fba047090e 100644 --- a/.eslintignore +++ b/.eslintignore @@ -5,5 +5,6 @@ e2e/__workdir_synlink__/ e2e/__external-repos__/ coverage/ website/src/**/*.js +presets/index.d.ts *.config.js .eslintrc.js diff --git a/e2e/__tests__/module-kinds/helpers.ts b/e2e/__tests__/module-kinds/helpers.ts index 76e4b33b65..c5e4d5dc14 100644 --- a/e2e/__tests__/module-kinds/helpers.ts +++ b/e2e/__tests__/module-kinds/helpers.ts @@ -1,4 +1,3 @@ -// @ts-expect-error testing purpose import tsJestPresets from '../../../presets' import { PackageSets } from '../../__helpers__/templates' import { configureTestCase } from '../../__helpers__/test-case' @@ -33,7 +32,7 @@ const runTestForOptions = (options: TestOptions, preset: AllPresetType = AllPres let tsJestPresetToUse switch (preset) { case AllPreset.DEFAULT_ESM: - tsJestPresetToUse = tsJestPresets.defaultEsm + tsJestPresetToUse = tsJestPresets.defaultsESM break case AllPreset.JS_WITH_TS_ESM: tsJestPresetToUse = tsJestPresets.jsWithTsESM @@ -42,7 +41,7 @@ const runTestForOptions = (options: TestOptions, preset: AllPresetType = AllPres tsJestPresetToUse = tsJestPresets.jsWithBabelESM break default: - tsJestPresetToUse = tsJestPresets.default + tsJestPresetToUse = tsJestPresets.defaults } const testCase = configureTestCase('module-kinds', { diff --git a/presets/index.d.ts b/presets/index.d.ts new file mode 100644 index 0000000000..ddaaddc521 --- /dev/null +++ b/presets/index.d.ts @@ -0,0 +1,11 @@ +import type { TsJestPresets } from '../dist/types' + +declare const _default: { + defaults: TsJestPresets; + defaultsESM: TsJestPresets; + jsWithTs: TsJestPresets; + jsWithTsESM: TsJestPresets; + jsWithBabel: TsJestPresets; + jsWithBabelESM: TsJestPresets; +}; +export = _default; diff --git a/src/cli/helpers/presets.ts b/src/cli/helpers/presets.ts index 00490a5ff1..3055b37790 100644 --- a/src/cli/helpers/presets.ts +++ b/src/cli/helpers/presets.ts @@ -1,4 +1,4 @@ -import type { TsJestPresets } from '../../presets/create-jest-preset' +import type { TsJestPresets } from '../../types' /** @internal */ export const enum JestPresetNames { diff --git a/src/presets/create-jest-preset.ts b/src/presets/create-jest-preset.ts index 69ff37fa3a..ed952193e5 100644 --- a/src/presets/create-jest-preset.ts +++ b/src/presets/create-jest-preset.ts @@ -1,14 +1,10 @@ import type { Config } from '@jest/types' +import type { TsJestPresets } from '../types' import { rootLogger } from '../utils/logger' const logger = rootLogger.child({ namespace: 'jest-preset' }) -export type TsJestPresets = Pick< - Config.InitialOptions, - 'extensionsToTreatAsEsm' | 'moduleFileExtensions' | 'transform' | 'testMatch' -> - export function createJestPreset(allowJs = false, extraOptions: Config.InitialOptions = {}): TsJestPresets { logger.debug({ allowJs }, 'creating jest presets', allowJs ? 'handling' : 'not handling', 'JavaScript files') diff --git a/src/types.ts b/src/types.ts index 9bd2375369..3673ac44f4 100644 --- a/src/types.ts +++ b/src/types.ts @@ -34,6 +34,11 @@ export type BabelJestTransformer = { */ export type BabelConfig = _babel.TransformOptions +export type TsJestPresets = Pick< + Config.InitialOptions, + 'extensionsToTreatAsEsm' | 'moduleFileExtensions' | 'transform' | 'testMatch' +> + export interface AstTransformer> { path: string options?: T @@ -211,9 +216,7 @@ export interface TsCompilerInstance extends CompilerInstance { configSet: ConfigSet program: _ts.Program | undefined } -/** - * @internal - */ + export interface AstTransformerDesc> { name: string version: number diff --git a/website/docs/getting-started/presets.md b/website/docs/getting-started/presets.md index dfb14743b9..e62732dcf9 100644 --- a/website/docs/getting-started/presets.md +++ b/website/docs/getting-started/presets.md @@ -18,7 +18,8 @@ title: Presets ### Basic usage -In most cases, simply setting the `preset` key to the desired preset name in your Jest config should be enough to start using TypeScript with Jest (assuming you added `ts-jest` to your `devDependencies` of course): +In most cases, simply setting the `preset` key to the desired preset name in your Jest config should be enough to start +using TypeScript with Jest (assuming you added `ts-jest` to your `devDependencies` of course): ```js // jest.config.js @@ -53,11 +54,11 @@ In this case you'll need to use the JavaScript version of Jest config (comment/u ```js // jest.config.js const { defaults: tsjPreset } = require('ts-jest/presets') -// const { defaultsESM: tsjPreset } = require('ts-jest/presets'); -// const { jsWithTs: tsjPreset } = require('ts-jest/presets'); -// const { jsWithTsESM: tsjPreset } = require('ts-jest/presets'); -// const { jsWithBabel: tsjPreset } = require('ts-jest/presets'); -// const { jsWithBabelESM: tsjPreset } = require('ts-jest/presets'); +// const { defaultsESM: tsjPreset } = require('ts-jest/presets') +// const { jsWithTs: tsjPreset } = require('ts-jest/presets') +// const { jsWithTsESM: tsjPreset } = require('ts-jest/presets') +// const { jsWithBabel: tsjPreset } = require('ts-jest/presets') +// const { jsWithBabelESM: tsjPreset } = require('ts-jest/presets') module.exports = { // [...] @@ -67,3 +68,26 @@ module.exports = { }, } ``` + +Or through TypeScript (if `ts-node` is installed): + +```ts +// jest.config.ts +import type { InitialOptionsTsJest } from 'ts-jest/dist/types' +import { defaults as tsjPreset } from 'ts-jest/preset' +// import { defaultsESM as tsjPreset } from 'ts-jest/preset' +// import { jsWithTs as tsjPreset } from 'ts-jest/preset' +// import { jsWithTsESM as tsjPreset } from 'ts-jest/preset' +// import { jsWithBabel as tsjPreset } from 'ts-jest/preset' +// import { jsWithBabelESM as tsjPreset } from 'ts-jest/preset' + +const config: InitialOptionsTsJest = { + // [...] + transform: { + ...tsjPreset.transform, + // [...] + }, +} + +export default config +```