Skip to content

Commit

Permalink
chore(compiler): improve typescript compiler (#1417)
Browse files Browse the repository at this point in the history
  • Loading branch information
ahnpnl committed Mar 6, 2020
1 parent 98a13a2 commit 368298b
Show file tree
Hide file tree
Showing 26 changed files with 1,304 additions and 694 deletions.
3 changes: 2 additions & 1 deletion e2e/__cases__/allow-js/tsconfig.json
@@ -1,6 +1,7 @@
{
"compilerOptions": {
"target": "es5",
"allowJs": true
"allowJs": true,
"outDir": "$$ts-jest$$"
}
}
3 changes: 2 additions & 1 deletion e2e/__monorepos__/simple/with-dependency/package.json
Expand Up @@ -31,7 +31,8 @@
"globals": {
"ts-jest": {
"diagnostics": true,
"tsConfig": "<rootDir>/tsconfig.json"
"tsConfig": "<rootDir>/tsconfig.json",
"compilerHost": true
}
}
},
Expand Down
3 changes: 2 additions & 1 deletion e2e/__monorepos__/simple/with-dependency/tsconfig.json
Expand Up @@ -13,7 +13,8 @@
"downlevelIteration": true,
"strict": true,
"moduleResolution": "node",
"esModuleInterop": true
"esModuleInterop": true,
"incremental": true
},
"include": [
"./src"
Expand Down
64 changes: 32 additions & 32 deletions e2e/__tests__/__snapshots__/logger.test.ts.snap
Expand Up @@ -19,21 +19,21 @@ Array [
"[level:20] readTsConfig(): reading <cwd>/tsconfig.json",
"[level:20] normalized typescript config",
"[level:20] processing <cwd>/Hello.spec.ts",
"[level:20] creating typescript compiler (language service)",
"[level:20] file caching disabled",
"[level:20] creating language service",
"[level:20] compileUsingLanguageService(): creating language service",
"[level:20] readThrough(): no cache",
"[level:20] getOutput(): compiling using language service",
"[level:20] updateMemoryCache()",
"[level:20] updateMemoryCache() for language service",
"[level:20] compiler rebuilt Program instance when getting output",
"[level:20] visitSourceFileNode(): hoisting",
"[level:20] getOutput(): computing diagnostics",
"[level:20] getOutput(): computing diagnostics for language service",
"[level:20] invariant: Is service.getProject() identical before and after getting emit output and diagnostics? (should always be true) true",
"[level:20] computing cache key for <cwd>/Hello.ts",
"[level:20] processing <cwd>/Hello.ts",
"[level:20] readThrough(): no cache",
"[level:20] getOutput(): compiling using language service",
"[level:20] updateMemoryCache()",
"[level:20] updateMemoryCache() for language service",
"[level:20] visitSourceFileNode(): hoisting",
"[level:20] getOutput(): computing diagnostics",
"[level:20] getOutput(): computing diagnostics for language service",
"[level:20] invariant: Is service.getProject() identical before and after getting emit output and diagnostics? (should always be true) true",
]
`;
Expand All @@ -60,22 +60,22 @@ Array [
"[level:20] loaded module babel-jest",
"[level:20] patching babel-jest",
"[level:20] checking version of babel-jest: OK",
"[level:20] creating typescript compiler (language service)",
"[level:20] file caching disabled",
"[level:20] creating language service",
"[level:20] compileUsingLanguageService(): creating language service",
"[level:20] readThrough(): no cache",
"[level:20] getOutput(): compiling using language service",
"[level:20] updateMemoryCache()",
"[level:20] updateMemoryCache() for language service",
"[level:20] compiler rebuilt Program instance when getting output",
"[level:20] visitSourceFileNode(): hoisting",
"[level:20] getOutput(): computing diagnostics",
"[level:20] getOutput(): computing diagnostics for language service",
"[level:20] invariant: Is service.getProject() identical before and after getting emit output and diagnostics? (should always be true) true",
"[level:20] calling babel-jest processor",
"[level:20] computing cache key for <cwd>/Hello.ts",
"[level:20] processing <cwd>/Hello.ts",
"[level:20] readThrough(): no cache",
"[level:20] getOutput(): compiling using language service",
"[level:20] updateMemoryCache()",
"[level:20] updateMemoryCache() for language service",
"[level:20] visitSourceFileNode(): hoisting",
"[level:20] getOutput(): computing diagnostics",
"[level:20] getOutput(): computing diagnostics for language service",
"[level:20] invariant: Is service.getProject() identical before and after getting emit output and diagnostics? (should always be true) true",
"[level:20] calling babel-jest processor",
]
`;
Expand Down Expand Up @@ -104,22 +104,22 @@ Array [
"[level:20] loaded module babel-jest",
"[level:20] patching babel-jest",
"[level:20] checking version of babel-jest: OK",
"[level:20] creating typescript compiler (language service)",
"[level:20] file caching disabled",
"[level:20] creating language service",
"[level:20] compileUsingLanguageService(): creating language service",
"[level:20] readThrough(): no cache",
"[level:20] getOutput(): compiling using language service",
"[level:20] updateMemoryCache()",
"[level:20] updateMemoryCache() for language service",
"[level:20] compiler rebuilt Program instance when getting output",
"[level:20] visitSourceFileNode(): hoisting",
"[level:20] getOutput(): computing diagnostics",
"[level:20] getOutput(): computing diagnostics for language service",
"[level:20] invariant: Is service.getProject() identical before and after getting emit output and diagnostics? (should always be true) true",
"[level:20] calling babel-jest processor",
"[level:20] computing cache key for <cwd>/Hello.ts",
"[level:20] processing <cwd>/Hello.ts",
"[level:20] readThrough(): no cache",
"[level:20] getOutput(): compiling using language service",
"[level:20] updateMemoryCache()",
"[level:20] updateMemoryCache() for language service",
"[level:20] visitSourceFileNode(): hoisting",
"[level:20] getOutput(): computing diagnostics",
"[level:20] getOutput(): computing diagnostics for language service",
"[level:20] invariant: Is service.getProject() identical before and after getting emit output and diagnostics? (should always be true) true",
"[level:20] calling babel-jest processor",
]
`;
Expand All @@ -143,21 +143,21 @@ Array [
"[level:20] readTsConfig(): reading <cwd>/tsconfig.json",
"[level:20] normalized typescript config",
"[level:20] processing <cwd>/Hello.spec.ts",
"[level:20] creating typescript compiler (language service)",
"[level:20] file caching disabled",
"[level:20] creating language service",
"[level:20] compileUsingLanguageService(): creating language service",
"[level:20] readThrough(): no cache",
"[level:20] getOutput(): compiling using language service",
"[level:20] updateMemoryCache()",
"[level:20] updateMemoryCache() for language service",
"[level:20] compiler rebuilt Program instance when getting output",
"[level:20] visitSourceFileNode(): hoisting",
"[level:20] getOutput(): computing diagnostics",
"[level:20] getOutput(): computing diagnostics for language service",
"[level:20] invariant: Is service.getProject() identical before and after getting emit output and diagnostics? (should always be true) true",
"[level:20] computing cache key for <cwd>/Hello.ts",
"[level:20] processing <cwd>/Hello.ts",
"[level:20] readThrough(): no cache",
"[level:20] getOutput(): compiling using language service",
"[level:20] updateMemoryCache()",
"[level:20] updateMemoryCache() for language service",
"[level:20] visitSourceFileNode(): hoisting",
"[level:20] getOutput(): computing diagnostics",
"[level:20] getOutput(): computing diagnostics for language service",
"[level:20] invariant: Is service.getProject() identical before and after getting emit output and diagnostics? (should always be true) true",
]
`;
Expand Down
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -134,6 +134,6 @@
]
},
"engines": {
"node": ">= 6"
"node": ">= 8"
}
}
70 changes: 25 additions & 45 deletions src/__helpers__/fakers.ts
@@ -1,6 +1,8 @@
import { Config } from '@jest/types'
import { resolve } from 'path'

import { createCompiler } from '../compiler/instance'
import { ConfigSet } from '../config/config-set'
import { BabelConfig, TsJestConfig, TsJestGlobalOptions } from '../types'
import { ImportReasons } from '../util/messages'

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

export const rootDir = filePath('')

export function transpiledTsSource() {
return `
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
var upper_1 = __importDefault(require("./upper"));
var lower_1 = __importDefault(require("./lower"));
jest.mock('./upper', function () { return function (s) { return s.toUpperCase(); }; });
describe('hello', function () {
test('my test', function () {
expect(upper_1.default('hello')).toBe('HELLO');
expect(lower_1.default('HELLO')).toBe('hello');
jest.mock('./lower', function () { return function (s) { return s.toLowerCase(); }; });
});
});
`
}

export function htmlSource() {
return `
<div>
<span>some text with \`backtick\`</span>
</div>
`
}

export function typescriptSource() {
return `
import upper from './upper';
import lower from './lower';
jest.mock('./upper', () => (s) => s.toUpperCase());
describe('hello', () => {
test('my test', () => {
expect(upper('hello')).toBe('HELLO');
expect(lower('HELLO')).toBe('hello');
jest.mock('./lower', () => (s) => s.toLowerCase());
});
});
`
}

export function tsJestConfig(options?: Partial<TsJestConfig>): TsJestConfig {
return {
compilerHost: false,
emit: false,
isolatedModules: false,
compiler: 'typescript',
transformers: [],
Expand All @@ -68,7 +28,7 @@ export function tsJestConfig(options?: Partial<TsJestConfig>): TsJestConfig {
}
}

export function jestConfig<T extends Config.ProjectConfig>(
export function getJestConfig<T extends Config.ProjectConfig>(
options?: Partial<Config.InitialOptions | Config.ProjectConfig>,
tsJestOptions?: TsJestGlobalOptions,
): T {
Expand All @@ -93,3 +53,23 @@ export function babelConfig<T extends BabelConfig>(options?: BabelConfig): T {
export function importReason(text = '[[BECAUSE]]'): ImportReasons {
return text as any
}

// not really unit-testing here, but it's hard to mock all those values :-D
export function makeCompiler({
jestConfig,
tsJestConfig,
parentConfig,
}: {
jestConfig?: Partial<Config.ProjectConfig>
tsJestConfig?: TsJestGlobalOptions
parentConfig?: TsJestGlobalOptions
} = {}) {
tsJestConfig = { ...tsJestConfig }
tsJestConfig.diagnostics = {
...(tsJestConfig.diagnostics as any),
pretty: false,
}
const cs = new ConfigSet(getJestConfig(jestConfig, tsJestConfig), parentConfig)

return createCompiler(cs)
}
53 changes: 0 additions & 53 deletions src/__snapshots__/compiler.spec.ts.snap

This file was deleted.

0 comments on commit 368298b

Please sign in to comment.