Skip to content

Commit

Permalink
feat(config): support typed config options for jest config typescript (
Browse files Browse the repository at this point in the history
  • Loading branch information
ahnpnl committed Feb 8, 2021
1 parent 1a29b41 commit 3cfc37b
Show file tree
Hide file tree
Showing 8 changed files with 95 additions and 40 deletions.
14 changes: 14 additions & 0 deletions 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()
})
13 changes: 13 additions & 0 deletions 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)
})
})
})
9 changes: 9 additions & 0 deletions src/types.ts
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
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
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
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
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
Expand Up @@ -5,6 +5,6 @@ title: ESM Support

:::important

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

:::

0 comments on commit 3cfc37b

Please sign in to comment.