Skip to content

Commit 3cfc37b

Browse files
authoredFeb 8, 2021
feat(config): support typed config options for jest config typescript (#2335)
1 parent 1a29b41 commit 3cfc37b

File tree

8 files changed

+95
-40
lines changed

8 files changed

+95
-40
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import type {InitialOptionsTsJest} from 'ts-jest/dist/types'
2+
3+
test('Typings for jest config to support full TypeScript mode', () => {
4+
const config: InitialOptionsTsJest = {
5+
globals: {
6+
'ts-jest': {
7+
isolatedModules: true,
8+
}
9+
},
10+
verbose: true,
11+
};
12+
13+
expect(config).toBeTruthy()
14+
})

‎e2e/__tests__/ts-jest-typings.test.ts

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { PackageSets } from '../__helpers__/templates'
2+
import { configureTestCase } from '../__helpers__/test-case'
3+
4+
describe('ts-jest utils', () => {
5+
const testCase = configureTestCase('typings')
6+
7+
testCase.runWithTemplates([PackageSets.default], 0, (runTest, { testLabel }) => {
8+
it(testLabel, () => {
9+
const result = runTest()
10+
expect(result.status).toBe(0)
11+
})
12+
})
13+
})

‎src/types.ts

+9
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,14 @@ export interface TsJestGlobalOptions {
139139
useESM?: boolean
140140
}
141141

142+
export interface GlobalConfigTsJest extends Config.ConfigGlobals {
143+
'ts-jest': TsJestGlobalOptions
144+
}
145+
146+
export interface InitialOptionsTsJest extends Config.InitialOptions {
147+
globals?: GlobalConfigTsJest
148+
}
149+
142150
interface TsJestConfig$tsConfig$file {
143151
kind: 'file'
144152
value: string | undefined
@@ -189,6 +197,7 @@ export interface TransformOptionsTsJest extends TransformOptions {
189197
}
190198

191199
export type ResolvedModulesMap = Map<string, _ts.ResolvedModuleFull | undefined> | undefined
200+
192201
/**
193202
* @internal
194203
*/

‎website/docs/getting-started/installation.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ Tip: If you get an error with the following `npm` commands such as `npx: command
2929

3030
#### Creating
3131

32-
By default Jest can run without any config files, but it will not compile `.ts` files.
32+
By default, Jest can run without any config files, but it will not compile `.ts` files.
3333
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.
3434

3535
`ts-jest` can create the configuration file for you automatically:

‎website/docs/getting-started/options.md

+35-19
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,11 @@ title: Options
55

66
### Introduction
77

8-
All `ts-jest` specific options are located under the `globals.ts-jest` path of your Jest config:
9-
10-
```js
11-
// jest.config.js
12-
module.exports = {
13-
// [...]
14-
globals: {
15-
'ts-jest': {
16-
// ts-jest configuration goes here
17-
},
18-
},
19-
}
20-
```
8+
All `ts-jest` specific options are located under the `globals` of Jest config object in the `package.json` file of your project,
9+
or through a `jest.config.js`, or `jest.config.ts` file.
2110

2211
```json
23-
// OR package.json
12+
// package.json
2413
{
2514
// [...]
2615
"jest": {
@@ -33,23 +22,50 @@ module.exports = {
3322
}
3423
```
3524

36-
#### IDE `ts-jest` config suggestion
25+
Or through JavaScript:
26+
27+
```js
28+
// jest.config.js
29+
module.exports = {
30+
// [...]
31+
globals: {
32+
'ts-jest': {
33+
// ts-jest configuration goes here
34+
},
35+
},
36+
}
37+
```
38+
39+
:::tip
3740

3841
To utilize IDE suggestions, you can use `JSDoc` comments to provide suggested `ts-jest` configs for your Jest config:
3942

4043
```js
41-
/** @typedef {import('ts-jest/dist/types')} */
42-
/** @type {import('@jest/types').Config.InitialOptions} */
43-
const config = {
44+
/** @type {import('ts-jest/dist/types').InitialOptionsTsJest} */
45+
module.exports = config = {
4446
// [...]
4547
globals: {
4648
'ts-jest': {
4749
// ts-jest configuration goes here and your IDE will suggest which configs when typing
4850
},
4951
},
5052
}
53+
```
54+
55+
:::
56+
57+
Or through TypeScript (if `ts-node` is installed):
5158

52-
module.exports = config
59+
```ts
60+
// jest.config.ts
61+
import type { InitialOptionsTsJest } from 'ts-jest/dist/types'
62+
63+
const config: InitialOptionsTsJests = {
64+
'ts-jest': {
65+
// ts-jest configuration goes here
66+
},
67+
}
68+
export default config
5369
```
5470

5571
### Options

‎website/src/pages/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ const features = [
1919
},
2020
{
2121
title: 'Babel support',
22-
description: <>Working seamlessly with Babel</>,
22+
description: <>Support working in combination with Babel</>,
2323
},
2424
]
2525

‎website/versioned_docs/version-26.5/getting-started/options.md

+21-18
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,11 @@ title: Options
55

66
### Introduction
77

8-
All `ts-jest` specific options are located under the `globals.ts-jest` path of your Jest config:
9-
10-
```js
11-
// jest.config.js
12-
module.exports = {
13-
// [...]
14-
globals: {
15-
'ts-jest': {
16-
// ts-jest configuration goes here
17-
},
18-
},
19-
}
20-
```
8+
All `ts-jest` specific options are located under the `globals` of Jest config object in the `package.json` file of your project,
9+
or through a `jest.config.js`, or `jest.config.ts` file.
2110

2211
```json
23-
// OR package.json
12+
// package.json
2413
{
2514
// [...]
2615
"jest": {
@@ -33,25 +22,39 @@ module.exports = {
3322
}
3423
```
3524

36-
#### IDE `ts-jest` config suggestion
25+
Or through JavaScript:
26+
27+
```js
28+
// jest.config.js
29+
module.exports = {
30+
// [...]
31+
globals: {
32+
'ts-jest': {
33+
// ts-jest configuration goes here
34+
},
35+
},
36+
}
37+
```
38+
39+
:::tip
3740

3841
To utilize IDE suggestions, you can use `JSDoc` comments to provide suggested `ts-jest` configs for your Jest config:
3942

4043
```js
4144
/** @typedef {import('ts-jest/dist/types')} */
4245
/** @type {import('@jest/types').Config.InitialOptions} */
43-
const config = {
46+
module.exports = config = {
4447
// [...]
4548
globals: {
4649
'ts-jest': {
4750
// ts-jest configuration goes here and your IDE will suggest which configs when typing
4851
},
4952
},
5053
}
51-
52-
module.exports = config
5354
```
5455

56+
:::
57+
5558
### Options
5659

5760
All options have default values which should fit most of the projects. Click on the option's name to see details and example(s).

‎website/versioned_docs/version-26.5/guides/esm-support.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@ title: ESM Support
55

66
:::important
77

8-
ESM support is only available in **v27**
8+
ESM support is only available in **v27++**
99

1010
:::

0 commit comments

Comments
 (0)
Please sign in to comment.