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(config): support typed config options for jest config typescript #2335

Merged
merged 1 commit into from
Feb 8, 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
14 changes: 14 additions & 0 deletions e2e/__cases__/typings/jest-config-typings.spec.ts
Original file line number Diff line number Diff line change
@@ -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()
})
13 changes: 13 additions & 0 deletions e2e/__tests__/ts-jest-typings.test.ts
Original file line number Diff line number Diff line change
@@ -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)
})
})
})
9 changes: 9 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -189,6 +197,7 @@ export interface TransformOptionsTsJest extends TransformOptions {
}

export type ResolvedModulesMap = Map<string, _ts.ResolvedModuleFull | undefined> | undefined

/**
* @internal
*/
Expand Down
2 changes: 1 addition & 1 deletion website/docs/getting-started/installation.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
54 changes: 35 additions & 19 deletions website/docs/getting-started/options.md
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand All @@ -33,23 +22,50 @@ 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': {
// ts-jest configuration goes here and your IDE will suggest which configs when typing
},
},
}
```

:::

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
Expand Down
2 changes: 1 addition & 1 deletion website/src/pages/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const features = [
},
{
title: 'Babel support',
description: <>Working seamlessly with Babel</>,
description: <>Support working in combination with Babel</>,
},
]

Expand Down
39 changes: 21 additions & 18 deletions website/versioned_docs/version-26.5/getting-started/options.md
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand All @@ -33,25 +22,39 @@ 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': {
// ts-jest configuration goes here and your IDE will suggest which configs when typing
},
},
}

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).
Expand Down
2 changes: 1 addition & 1 deletion website/versioned_docs/version-26.5/guides/esm-support.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ title: ESM Support

:::important

ESM support is only available in **v27**
ESM support is only available in **v27++**

:::