Skip to content

Commit e46caae

Browse files
committed
fix: big refactor + fixes (mainly cache key + coverage)
1 parent 9a2d74f commit e46caae

27 files changed

+299
-166
lines changed

index.d.ts

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

index.js

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

package.json

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
{
22
"name": "ts-jest",
33
"version": "23.0.1",
4-
"main": "index.js",
4+
"main": "dist/index.js",
55
"types": "./dist/index.d.ts",
66
"description": "A preprocessor with sourcemap support to help use Typescript with Jest",
77
"scripts": {
8-
"build": "cpx index.d.ts dist/ && tsc -p .",
9-
"build:watch": "cpx index.d.ts dist/ && tsc -p . -w",
8+
"build": "tsc -p tsconfig.build.json",
9+
"build:watch": "tsc -p tsconfig.build.json -w",
1010
"test:nolint": "npm run clean-build && node scripts/tests.js",
11-
"clean": "rimraf dist/**/* && rimraf tests/simple/coverage && rimraf tests/simple-async/coverage && rimraf tests/**/*/debug.txt && rimraf tests/**/node_modules",
11+
"clean": "rimraf dist/**/* && rimraf tests/*/coverage && rimraf tests/*/debug.txt && rimraf tests/*/node_modules",
1212
"clean-build": "npm run clean && npm run build",
1313
"pretest": "npm run tslint && npm run clean-build",
1414
"test": "node scripts/tests.js",
15-
"tslint": "tslint src/*.ts",
15+
"tslint": "tslint src/**/*.ts",
1616
"doc": "doctoc .",
1717
"prepublish": "npm run clean-build",
1818
"precommit": "lint-staged",
@@ -38,7 +38,7 @@
3838
"homepage": "https://github.com/kulshekhar/ts-jest#readme",
3939
"jest": {
4040
"transform": {
41-
"^.+\\.tsx?$": "<rootDir>/dist/preprocessor.js"
41+
"^.+\\.tsx?$": "<rootDir>/dist/index.js"
4242
},
4343
"testRegex": "tests/__tests__/.*\\.spec\\.ts$",
4444
"testPathIgnorePatterns": [

preprocessor.js

Lines changed: 0 additions & 1 deletion
This file was deleted.

scripts/tests.js

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,6 @@ function createIntegrationMock() {
2525
const rootDir = path.resolve('.');
2626
const testCaseModuleFolder = path.join(testCaseNodeModules, 'ts-jest');
2727

28-
// Copy javascript files
29-
fs.copySync(
30-
path.resolve(rootDir, 'index.js'),
31-
path.resolve(testCaseModuleFolder, 'index.js')
32-
);
33-
fs.copySync(
34-
path.resolve(rootDir, 'preprocessor.js'),
35-
path.resolve(testCaseModuleFolder, 'preprocessor.js')
36-
);
37-
3828
// Copy package.json
3929
fs.copySync(
4030
path.resolve(rootDir, 'package.json'),

src/index.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// structure of this file heavilly inspired on:
2+
// https://github.com/facebook/jest/blob/master/packages/babel-jest/src/index.js
3+
4+
import jestPreset from 'babel-preset-jest';
5+
import getCacheKeyForArgs from './utils/get-cache-key';
6+
import { TsJestContext, JestCacheKeyArguments } from './types';
7+
import preprocess from './preprocess';
8+
9+
const createTransformer = (options?: any): jest.Transformer => {
10+
const cache = Object.create(null);
11+
12+
options = Object.assign({}, options, {
13+
compact: false,
14+
plugins: (options && options.plugins) || [],
15+
presets: ((options && options.presets) || []).concat([jestPreset]),
16+
sourceMaps: 'both',
17+
});
18+
delete options.cacheDirectory;
19+
delete options.filename;
20+
21+
const context: TsJestContext = { cache, options };
22+
23+
const getCacheKey = (...args: any[]) =>
24+
getCacheKeyForArgs(args as JestCacheKeyArguments, context);
25+
26+
return {
27+
canInstrument: true,
28+
getCacheKey,
29+
process: preprocess,
30+
createTransformer: undefined as any,
31+
};
32+
};
33+
34+
export default createTransformer();
35+
export { createTransformer };

src/postprocess.ts

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,26 +16,24 @@ function importBabelDeps() {
1616
istanbulPlugin = require('babel-plugin-istanbul').default;
1717
jestPreset = require('babel-preset-jest');
1818
}
19-
import { CompilerOptions } from 'typescript/lib/typescript';
19+
import { CompilerOptions } from 'typescript';
2020
import {
2121
BabelTransformOptions,
22-
CodeSourceMapPair,
23-
JestConfig,
2422
PostProcessHook,
25-
TransformOptions,
23+
JestCacheKeyOptions,
2624
TsJestConfig,
27-
} from './jest-types';
28-
import { logOnce } from './logger';
25+
} from './types';
26+
import { logOnce } from './utils/logger';
2927

3028
// Function that takes the transpiled typescript and runs it through babel/whatever.
3129
export function postProcessCode(
3230
compilerOptions: CompilerOptions,
33-
jestConfig: JestConfig,
31+
jestConfig: jest.ProjectConfig,
3432
tsJestConfig: TsJestConfig,
35-
transformOptions: TransformOptions,
36-
transpileOutput: CodeSourceMapPair,
33+
transformOptions: jest.TransformOptions,
34+
transpileOutput: jest.TransformedSource,
3735
filePath: string,
38-
): CodeSourceMapPair {
36+
): jest.TransformedSource {
3937
const postHook = getPostProcessHook(
4038
compilerOptions,
4139
jestConfig,
@@ -58,11 +56,11 @@ function createBabelTransformer(
5856
delete options.filename;
5957

6058
return (
61-
codeSourcemapPair: CodeSourceMapPair,
59+
codeSourcemapPair: jest.TransformedSource,
6260
filename: string,
63-
config: JestConfig,
64-
transformOptions: TransformOptions,
65-
): CodeSourceMapPair => {
61+
config: jest.ProjectConfig,
62+
transformOptions: JestCacheKeyOptions,
63+
): jest.TransformedSource => {
6664
const theseOptions = Object.assign(
6765
{ filename, inputSourceMap: codeSourcemapPair.map },
6866
options,
@@ -81,17 +79,18 @@ function createBabelTransformer(
8179
],
8280
]);
8381
}
84-
// Babel has incorrect typings, where the map is an object instead of a string. So we have to typecast it here
85-
return (babel.transform(
82+
83+
// we typecast here because babel returns a more complete object than the one expected by jest
84+
return babel.transform(
8685
codeSourcemapPair.code,
8786
theseOptions,
88-
) as any) as CodeSourceMapPair;
87+
) as jest.TransformedSource;
8988
};
9089
}
9190

9291
export const getPostProcessHook = (
9392
tsCompilerOptions: CompilerOptions,
94-
jestConfig: JestConfig,
93+
jestConfig: jest.ProjectConfig,
9594
tsJestConfig: TsJestConfig,
9695
): PostProcessHook => {
9796
if (tsJestConfig.skipBabel) {

src/preprocessor.ts renamed to src/preprocess.ts

Lines changed: 7 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,14 @@
1-
import * as crypto from 'crypto';
2-
import {
3-
BabelTransformOptions,
4-
CodeSourceMapPair,
5-
JestConfig,
6-
Path,
7-
TransformOptions,
8-
} from './jest-types';
9-
import { flushLogs, logOnce } from './logger';
1+
import { flushLogs, logOnce } from './utils/logger';
102
import { postProcessCode } from './postprocess';
113
import { getTSConfig, getTSJestConfig, runTsDiagnostics } from './utils';
124
import { transpileTypescript } from './transpiler';
135

14-
export function process(
6+
export default function preprocess(
157
src: string,
16-
filePath: Path,
17-
jestConfig: JestConfig,
18-
transformOptions: TransformOptions = { instrument: false },
19-
): CodeSourceMapPair | string {
8+
filePath: jest.Path,
9+
jestConfig: jest.ProjectConfig,
10+
transformOptions: jest.TransformOptions,
11+
): jest.TransformedSource | string {
2012
// transformOptions.instrument is a proxy for collectCoverage
2113
// https://github.com/kulshekhar/ts-jest/issues/201#issuecomment-300572902
2214
const compilerOptions = getTSConfig(jestConfig.globals, jestConfig.rootDir);
@@ -28,7 +20,7 @@ export function process(
2820
const isHtmlFile = /\.html$/.test(filePath);
2921

3022
// This is to support angular 2. See https://github.com/kulshekhar/ts-jest/pull/145
31-
if (isHtmlFile && jestConfig.globals.__TRANSFORM_HTML__) {
23+
if (isHtmlFile && (jestConfig.globals as any).__TRANSFORM_HTML__) {
3224
src = 'module.exports=' + JSON.stringify(src) + ';';
3325
}
3426

@@ -76,24 +68,3 @@ export function process(
7668

7769
return { code: outputText.code, map: outputText.map };
7870
}
79-
80-
/**
81-
* This is the function Jest uses to check if it has the file already in cache
82-
*/
83-
export function getCacheKey(
84-
fileData: string,
85-
filePath: Path,
86-
jestConfigStr: string,
87-
transformOptions: TransformOptions = { instrument: false },
88-
): string {
89-
const jestConfig: JestConfig = JSON.parse(jestConfigStr);
90-
91-
const tsConfig = getTSConfig(jestConfig.globals, jestConfig.rootDir);
92-
93-
return crypto
94-
.createHash('md5')
95-
.update(JSON.stringify(tsConfig), 'utf8')
96-
.update(JSON.stringify(transformOptions), 'utf8')
97-
.update(fileData + filePath + jestConfigStr, 'utf8')
98-
.digest('hex');
99-
}

src/transpile-if-ts.ts

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

src/transpiler.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
import * as ts from 'typescript';
2-
import { logOnce } from './logger';
3-
import { CodeSourceMapPair } from './jest-types';
2+
import { logOnce } from './utils/logger';
43

54
// Takes the typescript code and by whatever method configured, makes it into javascript code.
65
export function transpileTypescript(
76
filePath: string,
87
fileSrc: string,
98
compilerOptions: ts.CompilerOptions,
10-
): CodeSourceMapPair {
9+
): jest.TransformedSource {
1110
logOnce('Compiling via normal transpileModule call');
1211
const transpileOutput = transpileViaTranspileModule(
1312
filePath,

0 commit comments

Comments
 (0)