-
Notifications
You must be signed in to change notification settings - Fork 304
/
index.ts
98 lines (89 loc) · 4.04 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import type { TransformedSource } from '@jest/transform';
import type { Config } from '@jest/types';
import { DECLARATION_TYPE_EXT, JS_JSX_REGEX } from 'ts-jest/dist/constants';
import { TsJestTransformer } from 'ts-jest/dist/ts-jest-transformer';
import type { TransformOptionsTsJest, ProjectConfigTsJest } from 'ts-jest/dist/types';
import { stringify } from 'ts-jest/dist/utils/json';
import { JsonableValue } from 'ts-jest/dist/utils/jsonable-value';
import { NgJestConfig } from './config/ng-jest-config';
import { NgJestCompiler } from './compiler/ng-jest-compiler';
interface CachedConfigSet {
ngJestConfig: NgJestConfig;
jestConfig: JsonableValue<ProjectConfigTsJest>;
transformerCfgStr: string;
ngJestCompiler: NgJestCompiler;
}
class NgJestTransformer extends TsJestTransformer {
/**
* cache config set between each test run
*/
private static readonly _cachedConfigSets: CachedConfigSet[] = [];
protected _compiler!: NgJestCompiler;
process(
fileContent: string,
filePath: Config.Path,
transformOptions: TransformOptionsTsJest,
): TransformedSource | string {
const isDefinitionFile = filePath.endsWith(DECLARATION_TYPE_EXT);
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
const isJsFile = JS_JSX_REGEX.test(filePath);
const ngJestCfg = this._configsFor(transformOptions);
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
const shouldStringifyContent = ngJestCfg.shouldStringifyContent(filePath);
return shouldStringifyContent || isDefinitionFile || (!ngJestCfg.parsedTsConfig.options.allowJs && isJsFile)
? super.process(fileContent, filePath, transformOptions)
: this._compiler.getCompiledOutput(filePath, fileContent);
}
/**
* Override `ts-jest` method to load our `NgJestConfig` class
*/
protected _configsFor(transformOptions: TransformOptionsTsJest): NgJestConfig {
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
const jestConfig = { ...transformOptions.config };
const ccs: CachedConfigSet | undefined = NgJestTransformer._cachedConfigSets.find(
(cs) => cs.jestConfig.value === jestConfig,
);
let ngJestConfig: NgJestConfig;
if (ccs) {
this._transformCfgStr = ccs.transformerCfgStr;
this._compiler = ccs.ngJestCompiler;
ngJestConfig = ccs.ngJestConfig;
} else {
// try to look-it up by stringified version
const stringifiedJestCfg = stringify(jestConfig);
const serializedCcs = NgJestTransformer._cachedConfigSets.find(
(cs) => cs.jestConfig.serialized === stringifiedJestCfg,
);
if (serializedCcs) {
// update the object so that we can find it later
// this happens because jest first calls getCacheKey with stringified version of
// the config, and then it calls the transformer with the proper object
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
serializedCcs.jestConfig.value = jestConfig;
this._transformCfgStr = serializedCcs.transformerCfgStr;
this._compiler = serializedCcs.ngJestCompiler;
ngJestConfig = serializedCcs.ngJestConfig;
} else {
// create the new record in the index
this._logger.info('no matching config-set found, creating a new one');
ngJestConfig = new NgJestConfig(jestConfig);
this._compiler = new NgJestCompiler(ngJestConfig, transformOptions.cacheFS);
this._transformCfgStr = new JsonableValue({
...jestConfig,
...ngJestConfig.parsedTsConfig,
}).serialized;
NgJestTransformer._cachedConfigSets.push({
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
jestConfig: new JsonableValue(jestConfig),
ngJestConfig,
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
transformerCfgStr: this._transformCfgStr,
ngJestCompiler: this._compiler,
});
this._getFsCachedResolvedModules(ngJestConfig);
}
}
return ngJestConfig;
}
}
export = new NgJestTransformer();