Skip to content

Commit 7d5a9fb

Browse files
authored
Better memoization & minor fixes (#501)
* fixed minor things * minor cleanups * commen * fixed 1 issue * removed params * reverted to earlier configuration * reverted to earlier configuration * fixed types
1 parent 719a547 commit 7d5a9fb

7 files changed

Lines changed: 33 additions & 45 deletions

File tree

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@
6868
"cpx": "^1.5.0",
6969
"fs-extra": "6.0.0",
7070
"jest-config": "^22.4.3",
71+
"lodash": "^4.17.10",
7172
"pkg-dir": "^2.0.0",
7273
"yargs": "^11.0.0"
7374
},
@@ -76,6 +77,7 @@
7677
"typescript": "2.x"
7778
},
7879
"devDependencies": {
80+
"@types/lodash": "^4.14.108",
7981
"@types/babel-core": "latest",
8082
"@types/es6-shim": "0.31.36",
8183
"@types/fs-extra": "5.0.2",

src/logger.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,6 @@ export function flushLogs() {
3636
fs.writeFileSync(filePath, logString);
3737
}
3838

39-
function includes<T>(array: T[], subject: T) {
40-
return array.indexOf(subject) !== -1;
41-
}
42-
4339
function convertToJSONIfPossible(object: any): string {
4440
try {
4541
return JSON.stringify(object, null, 2);

src/preprocessor.ts

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import { JestConfig, Path, TransformOptions } from './jest-types';
44
import { flushLogs, logOnce } from './logger';
55
import { getPostProcessHook } from './postprocess';
66
import {
7-
cacheFile,
87
getTSConfig,
98
getTSJestConfig,
109
runTsDiagnostics,
@@ -19,11 +18,7 @@ export function process(
1918
) {
2019
// transformOptions.instrument is a proxy for collectCoverage
2120
// https://github.com/kulshekhar/ts-jest/issues/201#issuecomment-300572902
22-
const compilerOptions = getTSConfig(
23-
jestConfig.globals,
24-
jestConfig.rootDir,
25-
transformOptions.instrument,
26-
);
21+
const compilerOptions = getTSConfig(jestConfig.globals, jestConfig.rootDir);
2722

2823
logOnce('final compilerOptions:', compilerOptions);
2924

@@ -92,6 +87,9 @@ export function process(
9287
return modified;
9388
}
9489

90+
/**
91+
* This is the function Jest uses to check if it has the file already in cache
92+
*/
9593
export function getCacheKey(
9694
fileData: string,
9795
filePath: Path,
@@ -100,11 +98,7 @@ export function getCacheKey(
10098
): string {
10199
const jestConfig: JestConfig = JSON.parse(jestConfigStr);
102100

103-
const tsConfig = getTSConfig(
104-
jestConfig.globals,
105-
jestConfig.rootDir,
106-
transformOptions.instrument,
107-
);
101+
const tsConfig = getTSConfig(jestConfig.globals, jestConfig.rootDir);
108102

109103
return crypto
110104
.createHash('md5')

src/test-utils.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@ import * as fs from 'fs';
22
import { normalize } from 'jest-config';
33
import * as setFromArgv from 'jest-config/build/set_from_argv';
44
import * as path from 'path';
5+
import * as _ from 'lodash';
56

6-
function readRawConfig(argv, root) {
7+
function readRawConfig(argv: string, root: string) {
78
const rawConfig = parseConfig(argv);
89

910
if (typeof rawConfig === 'string') {
@@ -16,6 +17,7 @@ function readRawConfig(argv, root) {
1617
return normalize(config, argv);
1718
}
1819

20+
// Rawconfig is undefined
1921
const packageConfig = loadJestConfigFromPackage(
2022
path.join(root, 'package.json'),
2123
argv,
@@ -43,7 +45,7 @@ function loadJestConfigFromPackage(filePath, argv) {
4345
function parseConfig(argv) {
4446
if (argv.config && typeof argv.config === 'string') {
4547
// If the passed in value looks like JSON, treat it as an object.
46-
if (argv.config[0] === '{' && argv.config[argv.config.length - 1] === '}') {
48+
if (argv.config.startsWith('{') && argv.config.endsWith('}')) {
4749
return JSON.parse(argv.config);
4850
}
4951
}
@@ -58,7 +60,7 @@ function loadJestConfigFromFile(filePath, argv) {
5860
return normalize(config, argv);
5961
}
6062

61-
export function getJestConfig(root) {
63+
export function getJestConfig(root: string) {
6264
const yargs = require('yargs');
6365
const argv = yargs(process.argv.slice(2)).argv;
6466
const rawConfig = readRawConfig(argv, root);

src/transpile-if-ts.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ export function transpileIfTypescript(
1212
compilerOptions: getTSConfig(
1313
config || mockGlobalTSConfigSchema(global),
1414
rootDir,
15-
true,
1615
),
1716
fileName: path,
1817
});

src/utils.ts

Lines changed: 13 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@ import * as fs from 'fs';
33
import * as fsExtra from 'fs-extra';
44
import * as path from 'path';
55
import * as tsc from 'typescript';
6-
import { JestConfig, TsJestConfig } from './jest-types';
6+
import { ConfigGlobals, JestConfig, TsJestConfig } from './jest-types';
77
import { logOnce } from './logger';
8+
import * as _ from 'lodash';
89

9-
export function getTSJestConfig(globals: any): TsJestConfig {
10+
export function getTSJestConfig(globals: ConfigGlobals): TsJestConfig {
1011
return globals && globals['ts-jest'] ? globals['ts-jest'] : {};
1112
}
1213

@@ -94,8 +95,7 @@ function getPathToClosestTSConfig(
9495
return getPathToClosestTSConfig(path.join(startDir, '..'), startDir);
9596
}
9697

97-
// TODO: This can take something more specific than globals
98-
function getTSConfigPathFromConfig(globals: any): string {
98+
function getTSConfigPathFromConfig(globals: ConfigGlobals): string {
9999
const defaultTSConfigFile = getPathToClosestTSConfig();
100100
if (!globals) {
101101
return defaultTSConfigFile;
@@ -115,29 +115,18 @@ export function mockGlobalTSConfigSchema(globals: any) {
115115
return { 'ts-jest': { tsConfigFile: configPath } };
116116
}
117117

118-
const tsConfigCache: { [key: string]: any } = {};
119-
// TODO: Perhaps rename collectCoverage to here, as it seems to be the official jest name now:
120-
// https://github.com/facebook/jest/issues/3524
121-
export function getTSConfig(
122-
globals,
123-
rootDir: string = '',
124-
collectCoverage: boolean = false,
125-
) {
126-
const configPath = getTSConfigPathFromConfig(globals);
127-
logOnce(`Reading tsconfig file from path ${configPath}`);
128-
const skipBabel = getTSJestConfig(globals).skipBabel;
129-
118+
export const getTSConfig = _.memoize(getTSConfig_local, (globals, rootDir) => {
130119
// check cache before resolving configuration
131120
// NB: We use JSON.stringify() to create a consistent, unique signature. Although it lacks a uniform
132121
// shape, this is simpler and faster than using the crypto package to generate a hash signature.
133-
const tsConfigCacheKey = JSON.stringify([
134-
skipBabel,
135-
collectCoverage,
136-
configPath,
137-
]);
138-
if (tsConfigCacheKey in tsConfigCache) {
139-
return tsConfigCache[tsConfigCacheKey];
140-
}
122+
return JSON.stringify(globals, rootDir);
123+
});
124+
125+
// Non-memoized version of TSConfig
126+
function getTSConfig_local(globals, rootDir: string = '') {
127+
const configPath = getTSConfigPathFromConfig(globals);
128+
logOnce(`Reading tsconfig file from path ${configPath}`);
129+
const skipBabel = getTSJestConfig(globals).skipBabel;
141130

142131
const config = readCompilerOptions(configPath, rootDir);
143132
logOnce('Original typescript config before modifications: ', { ...config });
@@ -171,8 +160,6 @@ export function getTSConfig(
171160
config.module = tsc.ModuleKind.ES2015;
172161
}
173162

174-
// cache result for future requests
175-
tsConfigCache[tsConfigCacheKey] = config;
176163
return config;
177164
}
178165

yarn.lock

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,10 @@
6363
version "22.2.3"
6464
resolved "https://registry.yarnpkg.com/@types/jest/-/jest-22.2.3.tgz#0157c0316dc3722c43a7b71de3fdf3acbccef10d"
6565

66+
"@types/lodash@^4.14.108":
67+
version "4.14.108"
68+
resolved "https://registry.yarnpkg.com/@types/lodash/-/lodash-4.14.108.tgz#02656af3add2e5b3174f830862c47421c00ef817"
69+
6670
"@types/node@*":
6771
version "8.0.31"
6872
resolved "https://registry.yarnpkg.com/@types/node/-/node-8.0.31.tgz#d9af61093cf4bfc9f066ca34de0175012cfb0ce9"
@@ -3128,6 +3132,10 @@ lodash@^4.14.0, lodash@^4.17.5:
31283132
version "4.17.5"
31293133
resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.5.tgz#99a92d65c0272debe8c96b6057bc8fbfa3bed511"
31303134

3135+
lodash@^4.17.10:
3136+
version "4.17.10"
3137+
resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.10.tgz#1b7793cf7259ea38fb3661d4d38b3260af8ae4e7"
3138+
31313139
log-symbols@^1.0.2:
31323140
version "1.0.2"
31333141
resolved "https://registry.yarnpkg.com/log-symbols/-/log-symbols-1.0.2.tgz#376ff7b58ea3086a0f09facc74617eca501e1a18"

0 commit comments

Comments
 (0)