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): add incremental option #1418

Merged
merged 5 commits into from Mar 7, 2020
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions .eslintrc.js
Expand Up @@ -7,6 +7,9 @@ module.exports = {
ecmaVersion: 2020,
sourceType: 'module',
impliedStrict: true,
ecmaFeatures: {
jsx: true,
},
},
rules: {
'no-console': ['error', { allow: ['warn', 'error', 'log'] }],
Expand Down
2 changes: 1 addition & 1 deletion CHANGELOG.md
@@ -1,5 +1,5 @@
<a name="25.2.1"></a>
# [25.2.1](https://github.com/kulshekhar/ts-jest/compare/25.2.1...25.2.0) (2020-02-21)
# [25.2.1](https://github.com/kulshekhar/ts-jest/compare/25.2.0...25.2.1) (2020-02-21)


### Bug Fixes
Expand Down
93 changes: 93 additions & 0 deletions docs/user/config/compilerHost.md
@@ -0,0 +1,93 @@
---
title: Compiler Host option
---

By default `ts-jest` uses TypeScript `LanguageService` API in the context of a project (yours), with full type-checking and features.
But TypeScript `Program` can also be used to achieve the same behavior as `LanguageService`.
That's what the `compilerHost` option (which defaults to `false`) does.

There are 2 types of TypeScript `Program`, one is `Incremental Program` which is only available from TypeScript 3.4
and the other one is normal `Program`.

By default `ts-jest` uses `Incremental Program` if `compilerHost` is enabled. The priority of using TypeScript APIs in `ts-jest`
as below:
- Default TypeScript API is `LanguageService`.
- `compilerHost` is enabled:
- `incremental` is enabled (**default**): use TypeScript `Incremental Program`.
- `incremental` is disabled: use TypeScript `Program`.
- `isolatedModules` is enabled, use TypeScript transpile modules.

Here is how to enable `ts-jest` to compile using TypeScript `Program`

### Example

<div class="row"><div class="col-md-6" markdown="block">

```js
// jest.config.js
module.exports = {
// [...]
globals: {
'ts-jest': {
compilerHost: true,
incremental: false,
}
}
};
```

</div><div class="col-md-6" markdown="block">

```js
// OR package.json
{
// [...]
"jest": {
"globals": {
"ts-jest": {
"compilerHost": true,
"incremental": false
}
}
}
}
```

</div></div>


Here is how to enable `ts-jest` to compile using TypeScript `IncrementalProgram`

### Example

<div class="row"><div class="col-md-6" markdown="block">

```js
// jest.config.js
module.exports = {
// [...]
globals: {
'ts-jest': {
compilerHost: true
}
}
};
```

</div><div class="col-md-6" markdown="block">

```js
// OR package.json
{
// [...]
"jest": {
"globals": {
"ts-jest": {
"compilerHost": true
}
}
}
}
```

</div></div>
1 change: 1 addition & 0 deletions docs/user/config/index.md
Expand Up @@ -220,6 +220,7 @@ npx ts-jest config:migrate package.json
[compiler]: compiler
[tsConfig]: tsConfig
[isolatedModules]: isolatedModules
[compilerHost]: compilerHost
[diagnostics]: diagnostics
[babelConfig]: babelConfig
[stringifyContentPathRegex]: stringifyContentPathRegex
Expand Down
6 changes: 6 additions & 0 deletions e2e/__cases__/compiler-host/main.spec.ts
@@ -0,0 +1,6 @@
import { g } from './main'

it('should pass', () => {
const x: string = g(5)
expect(x).toBe(5)
})
1 change: 1 addition & 0 deletions e2e/__cases__/compiler-host/main.ts
@@ -0,0 +1 @@
export const g = (v: number) => v
3 changes: 0 additions & 3 deletions e2e/__cases__/composite/foo.spec.ts

This file was deleted.

6 changes: 0 additions & 6 deletions e2e/__cases__/composite/tsconfig.json

This file was deleted.

6 changes: 4 additions & 2 deletions e2e/__helpers__/templates.ts
Expand Up @@ -6,5 +6,7 @@ export enum PackageSets {
// invalid
unsupportedVersion = 'with-unsupported-version',
}
export const allValidPackageSets = [PackageSets.default, PackageSets.babel7, PackageSets.babel7StringConfig, PackageSets.typescript2_7]
export const allPackageSetsWithPreset = [PackageSets.default, PackageSets.babel7, PackageSets.babel7StringConfig, PackageSets.typescript2_7]
export const allValidPackageSets = [PackageSets.default, PackageSets.babel7, PackageSets.babel7StringConfig, PackageSets.typescript2_7],
allPackageSetsWithPreset = [PackageSets.default, PackageSets.babel7, PackageSets.babel7StringConfig, PackageSets.typescript2_7],
allPackageSetsWithProgram = [PackageSets.default, PackageSets.babel7, PackageSets.babel7StringConfig],
allPackageSetsWithoutProgram = [PackageSets.typescript2_7, PackageSets.unsupportedVersion]
3 changes: 2 additions & 1 deletion e2e/__monorepos__/simple/with-dependency/package.json
Expand Up @@ -32,7 +32,8 @@
"ts-jest": {
"diagnostics": true,
"tsConfig": "<rootDir>/tsconfig.json",
"compilerHost": true
"compilerHost": true,
"incremental": true
}
}
},
Expand Down
3 changes: 1 addition & 2 deletions e2e/__monorepos__/simple/with-dependency/tsconfig.json
Expand Up @@ -13,8 +13,7 @@
"downlevelIteration": true,
"strict": true,
"moduleResolution": "node",
"esModuleInterop": true,
"incremental": true
"esModuleInterop": true
},
"include": [
"./src"
Expand Down
219 changes: 219 additions & 0 deletions e2e/__tests__/__snapshots__/compiler-host.test.ts.snap
@@ -0,0 +1,219 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`With compilerHost enabled and incremental disabled should fail using template "with-typescript-2-7" 1`] = `
× jest --no-cache
↳ exit code: 1
===[ STDOUT ]===================================================================

===[ STDERR ]===================================================================
FAIL ./main.spec.ts
● Test suite failed to run

TypeError: Cannot read property 'maxNodeModuleJsDepth' of undefined

at Object.createProgram (../../__templates__/with-typescript-2-7/node_modules/typescript/lib/typescript.js:73929:51)

Test Suites: 1 failed, 1 total
Tests: 0 total
Snapshots: 0 total
Time: XXs
Ran all test suites.
================================================================================
`;

exports[`With compilerHost enabled and incremental disabled should fail using template "with-unsupported-version" 1`] = `
× jest --no-cache
↳ exit code: 1
===[ STDOUT ]===================================================================

===[ STDERR ]===================================================================
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.
FAIL ./main.spec.ts
● Test suite failed to run

TypeError: Cannot read property 'maxNodeModuleJsDepth' of undefined

at Object.createProgram (../../__templates__/with-unsupported-version/node_modules/typescript/lib/typescript.js:69709:51)

Test Suites: 1 failed, 1 total
Tests: 0 total
Snapshots: 0 total
Time: XXs
Ran all test suites.
================================================================================
`;

exports[`With compilerHost enabled and incremental disabled should pass using template "default" 1`] = `
√ jest --no-cache
↳ exit code: 0
===[ STDOUT ]===================================================================

===[ STDERR ]===================================================================
ts-jest[ts-compiler] (WARN) TypeScript diagnostics (customize using \`[jest-config].globals.ts-jest.diagnostics\` option):
main.spec.ts:4:9 - error TS2322: Type 'number' is not assignable to type 'string'.

4 const x: string = g(5)
~
PASS ./main.spec.ts
√ should pass

Test Suites: 1 passed, 1 total
Tests: 1 passed, 1 total
Snapshots: 0 total
Time: XXs
Ran all test suites.
================================================================================
`;

exports[`With compilerHost enabled and incremental disabled should pass using template "with-babel-7" 1`] = `
√ jest --no-cache
↳ exit code: 0
===[ STDOUT ]===================================================================

===[ STDERR ]===================================================================
ts-jest[ts-compiler] (WARN) TypeScript diagnostics (customize using \`[jest-config].globals.ts-jest.diagnostics\` option):
main.spec.ts:4:9 - error TS2322: Type 'number' is not assignable to type 'string'.

4 const x: string = g(5)
~
PASS ./main.spec.ts
√ should pass

Test Suites: 1 passed, 1 total
Tests: 1 passed, 1 total
Snapshots: 0 total
Time: XXs
Ran all test suites.
================================================================================
`;

exports[`With compilerHost enabled and incremental disabled should pass using template "with-babel-7-string-config" 1`] = `
√ jest --no-cache
↳ exit code: 0
===[ STDOUT ]===================================================================

===[ STDERR ]===================================================================
ts-jest[ts-compiler] (WARN) TypeScript diagnostics (customize using \`[jest-config].globals.ts-jest.diagnostics\` option):
main.spec.ts:4:9 - error TS2322: Type 'number' is not assignable to type 'string'.

4 const x: string = g(5)
~
PASS ./main.spec.ts
√ should pass

Test Suites: 1 passed, 1 total
Tests: 1 passed, 1 total
Snapshots: 0 total
Time: XXs
Ran all test suites.
================================================================================
`;

exports[`With compilerHost enabled and incremental enabled should fail using template "with-typescript-2-7" 1`] = `
× jest --no-cache
↳ exit code: 1
===[ STDOUT ]===================================================================

===[ STDERR ]===================================================================
FAIL ./main.spec.ts
● Test suite failed to run

TypeError: Cannot read property 'maxNodeModuleJsDepth' of undefined

at Object.createProgram (../../__templates__/with-typescript-2-7/node_modules/typescript/lib/typescript.js:73929:51)

Test Suites: 1 failed, 1 total
Tests: 0 total
Snapshots: 0 total
Time: XXs
Ran all test suites.
================================================================================
`;

exports[`With compilerHost enabled and incremental enabled should fail using template "with-unsupported-version" 1`] = `
× jest --no-cache
↳ exit code: 1
===[ STDOUT ]===================================================================

===[ STDERR ]===================================================================
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.
FAIL ./main.spec.ts
● Test suite failed to run

TypeError: Cannot read property 'maxNodeModuleJsDepth' of undefined

at Object.createProgram (../../__templates__/with-unsupported-version/node_modules/typescript/lib/typescript.js:69709:51)

Test Suites: 1 failed, 1 total
Tests: 0 total
Snapshots: 0 total
Time: XXs
Ran all test suites.
================================================================================
`;

exports[`With compilerHost enabled and incremental enabled should pass using template "default" 1`] = `
√ jest --no-cache
↳ exit code: 0
===[ STDOUT ]===================================================================

===[ STDERR ]===================================================================
ts-jest[ts-compiler] (WARN) TypeScript diagnostics (customize using \`[jest-config].globals.ts-jest.diagnostics\` option):
main.spec.ts:4:9 - error TS2322: Type 'number' is not assignable to type 'string'.

4 const x: string = g(5)
~
PASS ./main.spec.ts
√ should pass

Test Suites: 1 passed, 1 total
Tests: 1 passed, 1 total
Snapshots: 0 total
Time: XXs
Ran all test suites.
================================================================================
`;

exports[`With compilerHost enabled and incremental enabled should pass using template "with-babel-7" 1`] = `
√ jest --no-cache
↳ exit code: 0
===[ STDOUT ]===================================================================

===[ STDERR ]===================================================================
ts-jest[ts-compiler] (WARN) TypeScript diagnostics (customize using \`[jest-config].globals.ts-jest.diagnostics\` option):
main.spec.ts:4:9 - error TS2322: Type 'number' is not assignable to type 'string'.

4 const x: string = g(5)
~
PASS ./main.spec.ts
√ should pass

Test Suites: 1 passed, 1 total
Tests: 1 passed, 1 total
Snapshots: 0 total
Time: XXs
Ran all test suites.
================================================================================
`;

exports[`With compilerHost enabled and incremental enabled should pass using template "with-babel-7-string-config" 1`] = `
√ jest --no-cache
↳ exit code: 0
===[ STDOUT ]===================================================================

===[ STDERR ]===================================================================
ts-jest[ts-compiler] (WARN) TypeScript diagnostics (customize using \`[jest-config].globals.ts-jest.diagnostics\` option):
main.spec.ts:4:9 - error TS2322: Type 'number' is not assignable to type 'string'.

4 const x: string = g(5)
~
PASS ./main.spec.ts
√ should pass

Test Suites: 1 passed, 1 total
Tests: 1 passed, 1 total
Snapshots: 0 total
Time: XXs
Ran all test suites.
================================================================================
`;