Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(presets): add typing for presets entry point #2337

Merged
merged 1 commit into from
Feb 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ e2e/__workdir_synlink__/
e2e/__external-repos__/
coverage/
website/src/**/*.js
presets/index.d.ts
*.config.js
.eslintrc.js
5 changes: 2 additions & 3 deletions e2e/__tests__/module-kinds/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// @ts-expect-error testing purpose
import tsJestPresets from '../../../presets'
import { PackageSets } from '../../__helpers__/templates'
import { configureTestCase } from '../../__helpers__/test-case'
Expand Down Expand Up @@ -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
Expand All @@ -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',
{
Expand Down
11 changes: 11 additions & 0 deletions presets/index.d.ts
Original file line number Diff line number Diff line change
@@ -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;
2 changes: 1 addition & 1 deletion src/cli/helpers/presets.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { TsJestPresets } from '../../presets/create-jest-preset'
import type { TsJestPresets } from '../../types'

/** @internal */
export const enum JestPresetNames {
Expand Down
6 changes: 1 addition & 5 deletions src/presets/create-jest-preset.ts
Original file line number Diff line number Diff line change
@@ -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')

Expand Down
9 changes: 6 additions & 3 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<T = Record<string, unknown>> {
path: string
options?: T
Expand Down Expand Up @@ -211,9 +216,7 @@ export interface TsCompilerInstance extends CompilerInstance {
configSet: ConfigSet
program: _ts.Program | undefined
}
/**
* @internal
*/

export interface AstTransformerDesc<T = Record<string, unknown>> {
name: string
version: number
Expand Down
36 changes: 30 additions & 6 deletions website/docs/getting-started/presets.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 = {
// [...]
Expand All @@ -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
```