Skip to content

Commit 5a69bce

Browse files
authored
feat(config): add incremental option (#1418)
1 parent a0ddca7 commit 5a69bce

32 files changed

+732
-102
lines changed

.eslintrc.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ module.exports = {
77
ecmaVersion: 2020,
88
sourceType: 'module',
99
impliedStrict: true,
10+
ecmaFeatures: {
11+
jsx: true,
12+
},
1013
},
1114
rules: {
1215
'no-console': ['error', { allow: ['warn', 'error', 'log'] }],

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<a name="25.2.1"></a>
2-
# [25.2.1](https://github.com/kulshekhar/ts-jest/compare/25.2.1...25.2.0) (2020-02-21)
2+
# [25.2.1](https://github.com/kulshekhar/ts-jest/compare/25.2.0...25.2.1) (2020-02-21)
33

44

55
### Bug Fixes

docs/user/config/compilerHost.md

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
---
2+
title: Compiler Host option
3+
---
4+
5+
By default `ts-jest` uses TypeScript `LanguageService` API in the context of a project (yours), with full type-checking and features.
6+
But TypeScript `Program` can also be used to achieve the same behavior as `LanguageService`.
7+
That's what the `compilerHost` option (which defaults to `false`) does.
8+
9+
There are 2 types of TypeScript `Program`, one is `Incremental Program` which is only available from TypeScript 3.4
10+
and the other one is normal `Program`.
11+
12+
By default `ts-jest` uses `Incremental Program` if `compilerHost` is enabled. The priority of using TypeScript APIs in `ts-jest`
13+
as below:
14+
- Default TypeScript API is `LanguageService`.
15+
- `compilerHost` is enabled:
16+
- `incremental` is enabled (**default**): use TypeScript `Incremental Program`.
17+
- `incremental` is disabled: use TypeScript `Program`.
18+
- `isolatedModules` is enabled, use TypeScript transpile modules.
19+
20+
Here is how to enable `ts-jest` to compile using TypeScript `Program`
21+
22+
### Example
23+
24+
<div class="row"><div class="col-md-6" markdown="block">
25+
26+
```js
27+
// jest.config.js
28+
module.exports = {
29+
// [...]
30+
globals: {
31+
'ts-jest': {
32+
compilerHost: true,
33+
incremental: false,
34+
}
35+
}
36+
};
37+
```
38+
39+
</div><div class="col-md-6" markdown="block">
40+
41+
```js
42+
// OR package.json
43+
{
44+
// [...]
45+
"jest": {
46+
"globals": {
47+
"ts-jest": {
48+
"compilerHost": true,
49+
"incremental": false
50+
}
51+
}
52+
}
53+
}
54+
```
55+
56+
</div></div>
57+
58+
59+
Here is how to enable `ts-jest` to compile using TypeScript `IncrementalProgram`
60+
61+
### Example
62+
63+
<div class="row"><div class="col-md-6" markdown="block">
64+
65+
```js
66+
// jest.config.js
67+
module.exports = {
68+
// [...]
69+
globals: {
70+
'ts-jest': {
71+
compilerHost: true
72+
}
73+
}
74+
};
75+
```
76+
77+
</div><div class="col-md-6" markdown="block">
78+
79+
```js
80+
// OR package.json
81+
{
82+
// [...]
83+
"jest": {
84+
"globals": {
85+
"ts-jest": {
86+
"compilerHost": true
87+
}
88+
}
89+
}
90+
}
91+
```
92+
93+
</div></div>

docs/user/config/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,7 @@ npx ts-jest config:migrate package.json
220220
[compiler]: compiler
221221
[tsConfig]: tsConfig
222222
[isolatedModules]: isolatedModules
223+
[compilerHost]: compilerHost
223224
[diagnostics]: diagnostics
224225
[babelConfig]: babelConfig
225226
[stringifyContentPathRegex]: stringifyContentPathRegex
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { g } from './main'
2+
3+
it('should pass', () => {
4+
const x: string = g(5)
5+
expect(x).toBe(5)
6+
})

e2e/__cases__/compiler-host/main.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const g = (v: number) => v

e2e/__cases__/composite/foo.spec.ts

Lines changed: 0 additions & 3 deletions
This file was deleted.

e2e/__cases__/composite/tsconfig.json

Lines changed: 0 additions & 6 deletions
This file was deleted.

e2e/__helpers__/templates.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,7 @@ export enum PackageSets {
66
// invalid
77
unsupportedVersion = 'with-unsupported-version',
88
}
9-
export const allValidPackageSets = [PackageSets.default, PackageSets.babel7, PackageSets.babel7StringConfig, PackageSets.typescript2_7]
10-
export const allPackageSetsWithPreset = [PackageSets.default, PackageSets.babel7, PackageSets.babel7StringConfig, PackageSets.typescript2_7]
9+
export const allValidPackageSets = [PackageSets.default, PackageSets.babel7, PackageSets.babel7StringConfig, PackageSets.typescript2_7],
10+
allPackageSetsWithPreset = [PackageSets.default, PackageSets.babel7, PackageSets.babel7StringConfig, PackageSets.typescript2_7],
11+
allPackageSetsWithProgram = [PackageSets.default, PackageSets.babel7, PackageSets.babel7StringConfig],
12+
allPackageSetsWithoutProgram = [PackageSets.typescript2_7, PackageSets.unsupportedVersion]

e2e/__monorepos__/simple/with-dependency/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@
3232
"ts-jest": {
3333
"diagnostics": true,
3434
"tsConfig": "<rootDir>/tsconfig.json",
35-
"compilerHost": true
35+
"compilerHost": true,
36+
"incremental": true
3637
}
3738
}
3839
},

e2e/__monorepos__/simple/with-dependency/tsconfig.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@
1313
"downlevelIteration": true,
1414
"strict": true,
1515
"moduleResolution": "node",
16-
"esModuleInterop": true,
17-
"incremental": true
16+
"esModuleInterop": true
1817
},
1918
"include": [
2019
"./src"
Lines changed: 219 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,219 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`With compilerHost enabled and incremental disabled should fail using template "with-typescript-2-7" 1`] = `
4+
× jest --no-cache
5+
↳ exit code: 1
6+
===[ STDOUT ]===================================================================
7+
8+
===[ STDERR ]===================================================================
9+
FAIL ./main.spec.ts
10+
● Test suite failed to run
11+
12+
TypeError: Cannot read property 'maxNodeModuleJsDepth' of undefined
13+
14+
at Object.createProgram (../../__templates__/with-typescript-2-7/node_modules/typescript/lib/typescript.js:73929:51)
15+
16+
Test Suites: 1 failed, 1 total
17+
Tests: 0 total
18+
Snapshots: 0 total
19+
Time: XXs
20+
Ran all test suites.
21+
================================================================================
22+
`;
23+
24+
exports[`With compilerHost enabled and incremental disabled should fail using template "with-unsupported-version" 1`] = `
25+
× jest --no-cache
26+
↳ exit code: 1
27+
===[ STDOUT ]===================================================================
28+
29+
===[ STDERR ]===================================================================
30+
ts-jest[versions] (WARN) Version 2.5.3 of typescript installed has not been tested with ts-jest. If you're experiencing issues, consider using a supported version (>=2.7.0 <4.0.0). Please do not report issues in ts-jest if you are using unsupported versions.
31+
FAIL ./main.spec.ts
32+
● Test suite failed to run
33+
34+
TypeError: Cannot read property 'maxNodeModuleJsDepth' of undefined
35+
36+
at Object.createProgram (../../__templates__/with-unsupported-version/node_modules/typescript/lib/typescript.js:69709:51)
37+
38+
Test Suites: 1 failed, 1 total
39+
Tests: 0 total
40+
Snapshots: 0 total
41+
Time: XXs
42+
Ran all test suites.
43+
================================================================================
44+
`;
45+
46+
exports[`With compilerHost enabled and incremental disabled should pass using template "default" 1`] = `
47+
√ jest --no-cache
48+
↳ exit code: 0
49+
===[ STDOUT ]===================================================================
50+
51+
===[ STDERR ]===================================================================
52+
ts-jest[ts-compiler] (WARN) TypeScript diagnostics (customize using \`[jest-config].globals.ts-jest.diagnostics\` option):
53+
main.spec.ts:4:9 - error TS2322: Type 'number' is not assignable to type 'string'.
54+
55+
4 const x: string = g(5)
56+
~
57+
PASS ./main.spec.ts
58+
√ should pass
59+
60+
Test Suites: 1 passed, 1 total
61+
Tests: 1 passed, 1 total
62+
Snapshots: 0 total
63+
Time: XXs
64+
Ran all test suites.
65+
================================================================================
66+
`;
67+
68+
exports[`With compilerHost enabled and incremental disabled should pass using template "with-babel-7" 1`] = `
69+
√ jest --no-cache
70+
↳ exit code: 0
71+
===[ STDOUT ]===================================================================
72+
73+
===[ STDERR ]===================================================================
74+
ts-jest[ts-compiler] (WARN) TypeScript diagnostics (customize using \`[jest-config].globals.ts-jest.diagnostics\` option):
75+
main.spec.ts:4:9 - error TS2322: Type 'number' is not assignable to type 'string'.
76+
77+
4 const x: string = g(5)
78+
~
79+
PASS ./main.spec.ts
80+
√ should pass
81+
82+
Test Suites: 1 passed, 1 total
83+
Tests: 1 passed, 1 total
84+
Snapshots: 0 total
85+
Time: XXs
86+
Ran all test suites.
87+
================================================================================
88+
`;
89+
90+
exports[`With compilerHost enabled and incremental disabled should pass using template "with-babel-7-string-config" 1`] = `
91+
√ jest --no-cache
92+
↳ exit code: 0
93+
===[ STDOUT ]===================================================================
94+
95+
===[ STDERR ]===================================================================
96+
ts-jest[ts-compiler] (WARN) TypeScript diagnostics (customize using \`[jest-config].globals.ts-jest.diagnostics\` option):
97+
main.spec.ts:4:9 - error TS2322: Type 'number' is not assignable to type 'string'.
98+
99+
4 const x: string = g(5)
100+
~
101+
PASS ./main.spec.ts
102+
√ should pass
103+
104+
Test Suites: 1 passed, 1 total
105+
Tests: 1 passed, 1 total
106+
Snapshots: 0 total
107+
Time: XXs
108+
Ran all test suites.
109+
================================================================================
110+
`;
111+
112+
exports[`With compilerHost enabled and incremental enabled should fail using template "with-typescript-2-7" 1`] = `
113+
× jest --no-cache
114+
↳ exit code: 1
115+
===[ STDOUT ]===================================================================
116+
117+
===[ STDERR ]===================================================================
118+
FAIL ./main.spec.ts
119+
● Test suite failed to run
120+
121+
TypeError: Cannot read property 'maxNodeModuleJsDepth' of undefined
122+
123+
at Object.createProgram (../../__templates__/with-typescript-2-7/node_modules/typescript/lib/typescript.js:73929:51)
124+
125+
Test Suites: 1 failed, 1 total
126+
Tests: 0 total
127+
Snapshots: 0 total
128+
Time: XXs
129+
Ran all test suites.
130+
================================================================================
131+
`;
132+
133+
exports[`With compilerHost enabled and incremental enabled should fail using template "with-unsupported-version" 1`] = `
134+
× jest --no-cache
135+
↳ exit code: 1
136+
===[ STDOUT ]===================================================================
137+
138+
===[ STDERR ]===================================================================
139+
ts-jest[versions] (WARN) Version 2.5.3 of typescript installed has not been tested with ts-jest. If you're experiencing issues, consider using a supported version (>=2.7.0 <4.0.0). Please do not report issues in ts-jest if you are using unsupported versions.
140+
FAIL ./main.spec.ts
141+
● Test suite failed to run
142+
143+
TypeError: Cannot read property 'maxNodeModuleJsDepth' of undefined
144+
145+
at Object.createProgram (../../__templates__/with-unsupported-version/node_modules/typescript/lib/typescript.js:69709:51)
146+
147+
Test Suites: 1 failed, 1 total
148+
Tests: 0 total
149+
Snapshots: 0 total
150+
Time: XXs
151+
Ran all test suites.
152+
================================================================================
153+
`;
154+
155+
exports[`With compilerHost enabled and incremental enabled should pass using template "default" 1`] = `
156+
√ jest --no-cache
157+
↳ exit code: 0
158+
===[ STDOUT ]===================================================================
159+
160+
===[ STDERR ]===================================================================
161+
ts-jest[ts-compiler] (WARN) TypeScript diagnostics (customize using \`[jest-config].globals.ts-jest.diagnostics\` option):
162+
main.spec.ts:4:9 - error TS2322: Type 'number' is not assignable to type 'string'.
163+
164+
4 const x: string = g(5)
165+
~
166+
PASS ./main.spec.ts
167+
√ should pass
168+
169+
Test Suites: 1 passed, 1 total
170+
Tests: 1 passed, 1 total
171+
Snapshots: 0 total
172+
Time: XXs
173+
Ran all test suites.
174+
================================================================================
175+
`;
176+
177+
exports[`With compilerHost enabled and incremental enabled should pass using template "with-babel-7" 1`] = `
178+
√ jest --no-cache
179+
↳ exit code: 0
180+
===[ STDOUT ]===================================================================
181+
182+
===[ STDERR ]===================================================================
183+
ts-jest[ts-compiler] (WARN) TypeScript diagnostics (customize using \`[jest-config].globals.ts-jest.diagnostics\` option):
184+
main.spec.ts:4:9 - error TS2322: Type 'number' is not assignable to type 'string'.
185+
186+
4 const x: string = g(5)
187+
~
188+
PASS ./main.spec.ts
189+
√ should pass
190+
191+
Test Suites: 1 passed, 1 total
192+
Tests: 1 passed, 1 total
193+
Snapshots: 0 total
194+
Time: XXs
195+
Ran all test suites.
196+
================================================================================
197+
`;
198+
199+
exports[`With compilerHost enabled and incremental enabled should pass using template "with-babel-7-string-config" 1`] = `
200+
√ jest --no-cache
201+
↳ exit code: 0
202+
===[ STDOUT ]===================================================================
203+
204+
===[ STDERR ]===================================================================
205+
ts-jest[ts-compiler] (WARN) TypeScript diagnostics (customize using \`[jest-config].globals.ts-jest.diagnostics\` option):
206+
main.spec.ts:4:9 - error TS2322: Type 'number' is not assignable to type 'string'.
207+
208+
4 const x: string = g(5)
209+
~
210+
PASS ./main.spec.ts
211+
√ should pass
212+
213+
Test Suites: 1 passed, 1 total
214+
Tests: 1 passed, 1 total
215+
Snapshots: 0 total
216+
Time: XXs
217+
Ran all test suites.
218+
================================================================================
219+
`;

0 commit comments

Comments
 (0)