Skip to content

Commit 368298b

Browse files
authored
chore(compiler): improve typescript compiler (#1417)
1 parent 98a13a2 commit 368298b

26 files changed

Lines changed: 1304 additions & 694 deletions
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"compilerOptions": {
33
"target": "es5",
4-
"allowJs": true
4+
"allowJs": true,
5+
"outDir": "$$ts-jest$$"
56
}
67
}

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

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

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
"downlevelIteration": true,
1414
"strict": true,
1515
"moduleResolution": "node",
16-
"esModuleInterop": true
16+
"esModuleInterop": true,
17+
"incremental": true
1718
},
1819
"include": [
1920
"./src"

e2e/__tests__/__snapshots__/logger.test.ts.snap

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -19,21 +19,21 @@ Array [
1919
"[level:20] readTsConfig(): reading <cwd>/tsconfig.json",
2020
"[level:20] normalized typescript config",
2121
"[level:20] processing <cwd>/Hello.spec.ts",
22-
"[level:20] creating typescript compiler (language service)",
2322
"[level:20] file caching disabled",
24-
"[level:20] creating language service",
23+
"[level:20] compileUsingLanguageService(): creating language service",
2524
"[level:20] readThrough(): no cache",
26-
"[level:20] getOutput(): compiling using language service",
27-
"[level:20] updateMemoryCache()",
25+
"[level:20] updateMemoryCache() for language service",
26+
"[level:20] compiler rebuilt Program instance when getting output",
2827
"[level:20] visitSourceFileNode(): hoisting",
29-
"[level:20] getOutput(): computing diagnostics",
28+
"[level:20] getOutput(): computing diagnostics for language service",
29+
"[level:20] invariant: Is service.getProject() identical before and after getting emit output and diagnostics? (should always be true) true",
3030
"[level:20] computing cache key for <cwd>/Hello.ts",
3131
"[level:20] processing <cwd>/Hello.ts",
3232
"[level:20] readThrough(): no cache",
33-
"[level:20] getOutput(): compiling using language service",
34-
"[level:20] updateMemoryCache()",
33+
"[level:20] updateMemoryCache() for language service",
3534
"[level:20] visitSourceFileNode(): hoisting",
36-
"[level:20] getOutput(): computing diagnostics",
35+
"[level:20] getOutput(): computing diagnostics for language service",
36+
"[level:20] invariant: Is service.getProject() identical before and after getting emit output and diagnostics? (should always be true) true",
3737
]
3838
`;
3939
@@ -60,22 +60,22 @@ Array [
6060
"[level:20] loaded module babel-jest",
6161
"[level:20] patching babel-jest",
6262
"[level:20] checking version of babel-jest: OK",
63-
"[level:20] creating typescript compiler (language service)",
6463
"[level:20] file caching disabled",
65-
"[level:20] creating language service",
64+
"[level:20] compileUsingLanguageService(): creating language service",
6665
"[level:20] readThrough(): no cache",
67-
"[level:20] getOutput(): compiling using language service",
68-
"[level:20] updateMemoryCache()",
66+
"[level:20] updateMemoryCache() for language service",
67+
"[level:20] compiler rebuilt Program instance when getting output",
6968
"[level:20] visitSourceFileNode(): hoisting",
70-
"[level:20] getOutput(): computing diagnostics",
69+
"[level:20] getOutput(): computing diagnostics for language service",
70+
"[level:20] invariant: Is service.getProject() identical before and after getting emit output and diagnostics? (should always be true) true",
7171
"[level:20] calling babel-jest processor",
7272
"[level:20] computing cache key for <cwd>/Hello.ts",
7373
"[level:20] processing <cwd>/Hello.ts",
7474
"[level:20] readThrough(): no cache",
75-
"[level:20] getOutput(): compiling using language service",
76-
"[level:20] updateMemoryCache()",
75+
"[level:20] updateMemoryCache() for language service",
7776
"[level:20] visitSourceFileNode(): hoisting",
78-
"[level:20] getOutput(): computing diagnostics",
77+
"[level:20] getOutput(): computing diagnostics for language service",
78+
"[level:20] invariant: Is service.getProject() identical before and after getting emit output and diagnostics? (should always be true) true",
7979
"[level:20] calling babel-jest processor",
8080
]
8181
`;
@@ -104,22 +104,22 @@ Array [
104104
"[level:20] loaded module babel-jest",
105105
"[level:20] patching babel-jest",
106106
"[level:20] checking version of babel-jest: OK",
107-
"[level:20] creating typescript compiler (language service)",
108107
"[level:20] file caching disabled",
109-
"[level:20] creating language service",
108+
"[level:20] compileUsingLanguageService(): creating language service",
110109
"[level:20] readThrough(): no cache",
111-
"[level:20] getOutput(): compiling using language service",
112-
"[level:20] updateMemoryCache()",
110+
"[level:20] updateMemoryCache() for language service",
111+
"[level:20] compiler rebuilt Program instance when getting output",
113112
"[level:20] visitSourceFileNode(): hoisting",
114-
"[level:20] getOutput(): computing diagnostics",
113+
"[level:20] getOutput(): computing diagnostics for language service",
114+
"[level:20] invariant: Is service.getProject() identical before and after getting emit output and diagnostics? (should always be true) true",
115115
"[level:20] calling babel-jest processor",
116116
"[level:20] computing cache key for <cwd>/Hello.ts",
117117
"[level:20] processing <cwd>/Hello.ts",
118118
"[level:20] readThrough(): no cache",
119-
"[level:20] getOutput(): compiling using language service",
120-
"[level:20] updateMemoryCache()",
119+
"[level:20] updateMemoryCache() for language service",
121120
"[level:20] visitSourceFileNode(): hoisting",
122-
"[level:20] getOutput(): computing diagnostics",
121+
"[level:20] getOutput(): computing diagnostics for language service",
122+
"[level:20] invariant: Is service.getProject() identical before and after getting emit output and diagnostics? (should always be true) true",
123123
"[level:20] calling babel-jest processor",
124124
]
125125
`;
@@ -143,21 +143,21 @@ Array [
143143
"[level:20] readTsConfig(): reading <cwd>/tsconfig.json",
144144
"[level:20] normalized typescript config",
145145
"[level:20] processing <cwd>/Hello.spec.ts",
146-
"[level:20] creating typescript compiler (language service)",
147146
"[level:20] file caching disabled",
148-
"[level:20] creating language service",
147+
"[level:20] compileUsingLanguageService(): creating language service",
149148
"[level:20] readThrough(): no cache",
150-
"[level:20] getOutput(): compiling using language service",
151-
"[level:20] updateMemoryCache()",
149+
"[level:20] updateMemoryCache() for language service",
150+
"[level:20] compiler rebuilt Program instance when getting output",
152151
"[level:20] visitSourceFileNode(): hoisting",
153-
"[level:20] getOutput(): computing diagnostics",
152+
"[level:20] getOutput(): computing diagnostics for language service",
153+
"[level:20] invariant: Is service.getProject() identical before and after getting emit output and diagnostics? (should always be true) true",
154154
"[level:20] computing cache key for <cwd>/Hello.ts",
155155
"[level:20] processing <cwd>/Hello.ts",
156156
"[level:20] readThrough(): no cache",
157-
"[level:20] getOutput(): compiling using language service",
158-
"[level:20] updateMemoryCache()",
157+
"[level:20] updateMemoryCache() for language service",
159158
"[level:20] visitSourceFileNode(): hoisting",
160-
"[level:20] getOutput(): computing diagnostics",
159+
"[level:20] getOutput(): computing diagnostics for language service",
160+
"[level:20] invariant: Is service.getProject() identical before and after getting emit output and diagnostics? (should always be true) true",
161161
]
162162
`;
163163

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,6 @@
134134
]
135135
},
136136
"engines": {
137-
"node": ">= 6"
137+
"node": ">= 8"
138138
}
139139
}

src/__helpers__/fakers.ts

Lines changed: 25 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { Config } from '@jest/types'
22
import { resolve } from 'path'
33

4+
import { createCompiler } from '../compiler/instance'
5+
import { ConfigSet } from '../config/config-set'
46
import { BabelConfig, TsJestConfig, TsJestGlobalOptions } from '../types'
57
import { ImportReasons } from '../util/messages'
68

@@ -10,52 +12,10 @@ export function filePath(relPath: string): string {
1012

1113
export const rootDir = filePath('')
1214

13-
export function transpiledTsSource() {
14-
return `
15-
"use strict";
16-
var __importDefault = (this && this.__importDefault) || function (mod) {
17-
return (mod && mod.__esModule) ? mod : { "default": mod };
18-
};
19-
var upper_1 = __importDefault(require("./upper"));
20-
var lower_1 = __importDefault(require("./lower"));
21-
jest.mock('./upper', function () { return function (s) { return s.toUpperCase(); }; });
22-
describe('hello', function () {
23-
test('my test', function () {
24-
expect(upper_1.default('hello')).toBe('HELLO');
25-
expect(lower_1.default('HELLO')).toBe('hello');
26-
jest.mock('./lower', function () { return function (s) { return s.toLowerCase(); }; });
27-
});
28-
});
29-
`
30-
}
31-
32-
export function htmlSource() {
33-
return `
34-
<div>
35-
<span>some text with \`backtick\`</span>
36-
</div>
37-
`
38-
}
39-
40-
export function typescriptSource() {
41-
return `
42-
import upper from './upper';
43-
import lower from './lower';
44-
45-
jest.mock('./upper', () => (s) => s.toUpperCase());
46-
47-
describe('hello', () => {
48-
test('my test', () => {
49-
expect(upper('hello')).toBe('HELLO');
50-
expect(lower('HELLO')).toBe('hello');
51-
jest.mock('./lower', () => (s) => s.toLowerCase());
52-
});
53-
});
54-
`
55-
}
56-
5715
export function tsJestConfig(options?: Partial<TsJestConfig>): TsJestConfig {
5816
return {
17+
compilerHost: false,
18+
emit: false,
5919
isolatedModules: false,
6020
compiler: 'typescript',
6121
transformers: [],
@@ -68,7 +28,7 @@ export function tsJestConfig(options?: Partial<TsJestConfig>): TsJestConfig {
6828
}
6929
}
7030

71-
export function jestConfig<T extends Config.ProjectConfig>(
31+
export function getJestConfig<T extends Config.ProjectConfig>(
7232
options?: Partial<Config.InitialOptions | Config.ProjectConfig>,
7333
tsJestOptions?: TsJestGlobalOptions,
7434
): T {
@@ -93,3 +53,23 @@ export function babelConfig<T extends BabelConfig>(options?: BabelConfig): T {
9353
export function importReason(text = '[[BECAUSE]]'): ImportReasons {
9454
return text as any
9555
}
56+
57+
// not really unit-testing here, but it's hard to mock all those values :-D
58+
export function makeCompiler({
59+
jestConfig,
60+
tsJestConfig,
61+
parentConfig,
62+
}: {
63+
jestConfig?: Partial<Config.ProjectConfig>
64+
tsJestConfig?: TsJestGlobalOptions
65+
parentConfig?: TsJestGlobalOptions
66+
} = {}) {
67+
tsJestConfig = { ...tsJestConfig }
68+
tsJestConfig.diagnostics = {
69+
...(tsJestConfig.diagnostics as any),
70+
pretty: false,
71+
}
72+
const cs = new ConfigSet(getJestConfig(jestConfig, tsJestConfig), parentConfig)
73+
74+
return createCompiler(cs)
75+
}

src/__snapshots__/compiler.spec.ts.snap

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

0 commit comments

Comments
 (0)