@depup/ts-jest 29.4.6-depup.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.lintstagedrc +4 -0
- package/.ts-jest-digest +1 -0
- package/CHANGELOG.md +2025 -0
- package/CONTRIBUTING.md +121 -0
- package/LICENSE.md +21 -0
- package/README.md +33 -0
- package/TROUBLESHOOTING.md +3 -0
- package/changes.json +18 -0
- package/cli.js +3 -0
- package/dist/cli/config/init.d.ts +6 -0
- package/dist/cli/config/init.js +139 -0
- package/dist/cli/config/migrate.d.ts +1 -0
- package/dist/cli/config/migrate.js +177 -0
- package/dist/cli/help.d.ts +1 -0
- package/dist/cli/help.js +25 -0
- package/dist/cli/helpers/presets.d.ts +1 -0
- package/dist/cli/helpers/presets.js +41 -0
- package/dist/cli/index.d.ts +1 -0
- package/dist/cli/index.js +63 -0
- package/dist/config/index.d.ts +2 -0
- package/dist/config/index.js +18 -0
- package/dist/config/paths-to-module-name-mapper.d.ts +9 -0
- package/dist/config/paths-to-module-name-mapper.js +52 -0
- package/dist/config/types.d.ts +2 -0
- package/dist/config/types.js +2 -0
- package/dist/constants.d.ts +13 -0
- package/dist/constants.js +22 -0
- package/dist/index.d.ts +15 -0
- package/dist/index.js +31 -0
- package/dist/legacy/compiler/compiler-utils.d.ts +8 -0
- package/dist/legacy/compiler/compiler-utils.js +32 -0
- package/dist/legacy/compiler/index.d.ts +2 -0
- package/dist/legacy/compiler/index.js +18 -0
- package/dist/legacy/compiler/ts-compiler.d.ts +27 -0
- package/dist/legacy/compiler/ts-compiler.js +434 -0
- package/dist/legacy/compiler/ts-jest-compiler.d.ts +8 -0
- package/dist/legacy/compiler/ts-jest-compiler.js +18 -0
- package/dist/legacy/config/config-set.d.ts +36 -0
- package/dist/legacy/config/config-set.js +622 -0
- package/dist/legacy/index.d.ts +6 -0
- package/dist/legacy/index.js +6 -0
- package/dist/legacy/ts-jest-transformer.d.ts +28 -0
- package/dist/legacy/ts-jest-transformer.js +293 -0
- package/dist/presets/all-presets.d.ts +15 -0
- package/dist/presets/all-presets.js +42 -0
- package/dist/presets/create-jest-preset.d.ts +18 -0
- package/dist/presets/create-jest-preset.js +180 -0
- package/dist/raw-compiler-options.d.ts +556 -0
- package/dist/raw-compiler-options.js +2 -0
- package/dist/transformers/hoist-jest.d.ts +9 -0
- package/dist/transformers/hoist-jest.js +111 -0
- package/dist/transpilers/typescript/transpile-module.d.ts +8 -0
- package/dist/transpilers/typescript/transpile-module.js +174 -0
- package/dist/types.d.ts +282 -0
- package/dist/types.js +3 -0
- package/dist/utils/backports.d.ts +1 -0
- package/dist/utils/backports.js +118 -0
- package/dist/utils/diagnostics.d.ts +5 -0
- package/dist/utils/diagnostics.js +8 -0
- package/dist/utils/get-package-version.d.ts +1 -0
- package/dist/utils/get-package-version.js +14 -0
- package/dist/utils/importer.d.ts +1 -0
- package/dist/utils/importer.js +146 -0
- package/dist/utils/index.d.ts +4 -0
- package/dist/utils/index.js +20 -0
- package/dist/utils/json.d.ts +2 -0
- package/dist/utils/json.js +16 -0
- package/dist/utils/jsonable-value.d.ts +10 -0
- package/dist/utils/jsonable-value.js +29 -0
- package/dist/utils/logger.d.ts +1 -0
- package/dist/utils/logger.js +20 -0
- package/dist/utils/memoize.d.ts +1 -0
- package/dist/utils/memoize.js +60 -0
- package/dist/utils/messages.d.ts +1 -0
- package/dist/utils/messages.js +21 -0
- package/dist/utils/normalize-slashes.d.ts +1 -0
- package/dist/utils/normalize-slashes.js +9 -0
- package/dist/utils/sha1.d.ts +1 -0
- package/dist/utils/sha1.js +38 -0
- package/dist/utils/ts-error.d.ts +1 -0
- package/dist/utils/ts-error.js +37 -0
- package/jest-preset.js +1 -0
- package/legacy.js +1 -0
- package/package.json +167 -0
- package/preprocessor.js +7 -0
- package/presets/default/jest-preset.js +1 -0
- package/presets/default-esm/jest-preset.js +1 -0
- package/presets/default-esm-legacy/jest-preset.js +1 -0
- package/presets/default-legacy/jest-preset.js +1 -0
- package/presets/index.d.ts +3 -0
- package/presets/index.js +3 -0
- package/presets/js-with-babel/jest-preset.js +1 -0
- package/presets/js-with-babel-esm/jest-preset.js +1 -0
- package/presets/js-with-babel-esm-legacy/jest-preset.js +1 -0
- package/presets/js-with-babel-legacy/jest-preset.js +1 -0
- package/presets/js-with-ts/jest-preset.js +1 -0
- package/presets/js-with-ts-esm/jest-preset.js +1 -0
- package/presets/js-with-ts-esm-legacy/jest-preset.js +1 -0
- package/presets/js-with-ts-legacy/jest-preset.js +1 -0
- package/tsconfig.base.json +20 -0
|
@@ -0,0 +1,434 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.TsCompiler = void 0;
|
|
7
|
+
const path_1 = require("path");
|
|
8
|
+
const bs_logger_1 = require("bs-logger");
|
|
9
|
+
const lodash_memoize_1 = __importDefault(require("lodash.memoize"));
|
|
10
|
+
const typescript_1 = __importDefault(require("typescript"));
|
|
11
|
+
const constants_1 = require("../../constants");
|
|
12
|
+
const transpile_module_1 = require("../../transpilers/typescript/transpile-module");
|
|
13
|
+
const utils_1 = require("../../utils");
|
|
14
|
+
const messages_1 = require("../../utils/messages");
|
|
15
|
+
const compiler_utils_1 = require("./compiler-utils");
|
|
16
|
+
const assertCompilerOptionsWithJestTransformMode = (compilerOptions, isEsmMode, logger) => {
|
|
17
|
+
if (isEsmMode && compilerOptions.module === typescript_1.default.ModuleKind.CommonJS) {
|
|
18
|
+
logger.error("The current compiler option \"module\" value is not suitable for Jest ESM mode. Please either use ES module kinds or Node16/NodeNext module kinds with \"type: module\" in package.json" /* Errors.InvalidModuleKindForEsm */);
|
|
19
|
+
}
|
|
20
|
+
};
|
|
21
|
+
class TsCompiler {
|
|
22
|
+
configSet;
|
|
23
|
+
runtimeCacheFS;
|
|
24
|
+
_logger;
|
|
25
|
+
_ts;
|
|
26
|
+
_initialCompilerOptions;
|
|
27
|
+
_compilerOptions;
|
|
28
|
+
/**
|
|
29
|
+
* @private
|
|
30
|
+
*/
|
|
31
|
+
_runtimeCacheFS;
|
|
32
|
+
/**
|
|
33
|
+
* @private
|
|
34
|
+
*/
|
|
35
|
+
_fileContentCache;
|
|
36
|
+
/**
|
|
37
|
+
* @internal
|
|
38
|
+
*/
|
|
39
|
+
_parsedTsConfig;
|
|
40
|
+
/**
|
|
41
|
+
* @internal
|
|
42
|
+
*/
|
|
43
|
+
_fileVersionCache;
|
|
44
|
+
/**
|
|
45
|
+
* @internal
|
|
46
|
+
*/
|
|
47
|
+
_cachedReadFile;
|
|
48
|
+
/**
|
|
49
|
+
* @internal
|
|
50
|
+
*/
|
|
51
|
+
_projectVersion = 1;
|
|
52
|
+
/**
|
|
53
|
+
* @internal
|
|
54
|
+
*/
|
|
55
|
+
_languageService;
|
|
56
|
+
/**
|
|
57
|
+
* @internal
|
|
58
|
+
*/
|
|
59
|
+
_moduleResolutionHost;
|
|
60
|
+
/**
|
|
61
|
+
* @internal
|
|
62
|
+
*/
|
|
63
|
+
_moduleResolutionCache;
|
|
64
|
+
program;
|
|
65
|
+
constructor(configSet, runtimeCacheFS) {
|
|
66
|
+
this.configSet = configSet;
|
|
67
|
+
this.runtimeCacheFS = runtimeCacheFS;
|
|
68
|
+
this._ts = configSet.compilerModule;
|
|
69
|
+
this._logger = utils_1.rootLogger.child({ namespace: 'ts-compiler' });
|
|
70
|
+
this._parsedTsConfig = this.configSet.parsedTsConfig;
|
|
71
|
+
this._initialCompilerOptions = { ...this._parsedTsConfig.options };
|
|
72
|
+
this._compilerOptions = { ...this._initialCompilerOptions };
|
|
73
|
+
this._runtimeCacheFS = runtimeCacheFS;
|
|
74
|
+
if (!this.configSet.isolatedModules) {
|
|
75
|
+
this._fileContentCache = new Map();
|
|
76
|
+
this._fileVersionCache = new Map();
|
|
77
|
+
this._cachedReadFile = this._logger.wrap({
|
|
78
|
+
namespace: 'ts:serviceHost',
|
|
79
|
+
call: null,
|
|
80
|
+
[bs_logger_1.LogContexts.logLevel]: bs_logger_1.LogLevels.trace,
|
|
81
|
+
}, 'readFile', (0, lodash_memoize_1.default)(this._ts.sys.readFile));
|
|
82
|
+
/* istanbul ignore next */
|
|
83
|
+
this._moduleResolutionHost = {
|
|
84
|
+
fileExists: (0, lodash_memoize_1.default)(this._ts.sys.fileExists),
|
|
85
|
+
readFile: this._cachedReadFile,
|
|
86
|
+
directoryExists: (0, lodash_memoize_1.default)(this._ts.sys.directoryExists),
|
|
87
|
+
getCurrentDirectory: () => this.configSet.cwd,
|
|
88
|
+
realpath: this._ts.sys.realpath && (0, lodash_memoize_1.default)(this._ts.sys.realpath),
|
|
89
|
+
getDirectories: (0, lodash_memoize_1.default)(this._ts.sys.getDirectories),
|
|
90
|
+
useCaseSensitiveFileNames: () => this._ts.sys.useCaseSensitiveFileNames,
|
|
91
|
+
};
|
|
92
|
+
this._moduleResolutionCache = this._ts.createModuleResolutionCache(this.configSet.cwd, this._ts.sys.useCaseSensitiveFileNames ? (x) => x : (x) => x.toLowerCase(), this._compilerOptions);
|
|
93
|
+
this._createLanguageService();
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
getResolvedModules(fileContent, fileName, runtimeCacheFS) {
|
|
97
|
+
// In watch mode, it is possible that the initial cacheFS becomes empty
|
|
98
|
+
if (!this.runtimeCacheFS.size) {
|
|
99
|
+
this._runtimeCacheFS = runtimeCacheFS;
|
|
100
|
+
}
|
|
101
|
+
this._logger.debug({ fileName }, 'getResolvedModules(): resolve direct imported module paths');
|
|
102
|
+
const importedModulePaths = Array.from(new Set(this._getImportedModulePaths(fileContent, fileName)));
|
|
103
|
+
this._logger.debug({ fileName }, 'getResolvedModules(): resolve nested imported module paths from directed imported module paths');
|
|
104
|
+
importedModulePaths.forEach((importedModulePath) => {
|
|
105
|
+
const resolvedFileContent = this._getFileContentFromCache(importedModulePath);
|
|
106
|
+
importedModulePaths.push(...this._getImportedModulePaths(resolvedFileContent, importedModulePath).filter((modulePath) => !importedModulePaths.includes(modulePath)));
|
|
107
|
+
});
|
|
108
|
+
return importedModulePaths;
|
|
109
|
+
}
|
|
110
|
+
fixupCompilerOptionsForModuleKind(compilerOptions, isEsm) {
|
|
111
|
+
const moduleResolution = this._ts.ModuleResolutionKind.Node10 ?? this._ts.ModuleResolutionKind.NodeJs;
|
|
112
|
+
if (!isEsm) {
|
|
113
|
+
return {
|
|
114
|
+
...compilerOptions,
|
|
115
|
+
module: this._ts.ModuleKind.CommonJS,
|
|
116
|
+
moduleResolution,
|
|
117
|
+
/**
|
|
118
|
+
* This option is only supported in `Node16`/`NodeNext` and `Bundler` module, see https://www.typescriptlang.org/tsconfig/#customConditions
|
|
119
|
+
*/
|
|
120
|
+
customConditions: undefined,
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
let moduleKind = compilerOptions.module ?? this._ts.ModuleKind.ESNext;
|
|
124
|
+
let esModuleInterop = compilerOptions.esModuleInterop;
|
|
125
|
+
if ((0, transpile_module_1.isModernNodeModuleKind)(moduleKind)) {
|
|
126
|
+
esModuleInterop = true;
|
|
127
|
+
moduleKind = this._ts.ModuleKind.ESNext;
|
|
128
|
+
}
|
|
129
|
+
return {
|
|
130
|
+
...compilerOptions,
|
|
131
|
+
module: moduleKind,
|
|
132
|
+
esModuleInterop,
|
|
133
|
+
moduleResolution,
|
|
134
|
+
/**
|
|
135
|
+
* This option is only supported in `Node16`/`NodeNext` and `Bundler` module, see https://www.typescriptlang.org/tsconfig/#customConditions
|
|
136
|
+
*/
|
|
137
|
+
customConditions: undefined,
|
|
138
|
+
};
|
|
139
|
+
}
|
|
140
|
+
getCompiledOutput(fileContent, fileName, options) {
|
|
141
|
+
const isEsmMode = this.configSet.useESM && options.supportsStaticESM;
|
|
142
|
+
this._compilerOptions = this.fixupCompilerOptionsForModuleKind(this._initialCompilerOptions, isEsmMode);
|
|
143
|
+
const moduleKind = this._initialCompilerOptions.module;
|
|
144
|
+
const currentModuleKind = this._compilerOptions.module;
|
|
145
|
+
if (this._languageService) {
|
|
146
|
+
if (constants_1.JS_JSX_REGEX.test(fileName) && !this._compilerOptions.allowJs) {
|
|
147
|
+
this._logger.warn({ fileName: fileName }, (0, messages_1.interpolate)("Got a `.js` file to compile while `allowJs` option is not set to `true` (file: {{path}}). To fix this:\n - if you want TypeScript to process JS files, set `allowJs` to `true` in your TypeScript config (usually tsconfig.json)\n - if you do not want TypeScript to process your `.js` files, in your Jest config change the `transform` key which value is `ts-jest` so that it does not match `.js` files anymore" /* Errors.GotJsFileButAllowJsFalse */, { path: fileName }));
|
|
148
|
+
return {
|
|
149
|
+
code: fileContent,
|
|
150
|
+
};
|
|
151
|
+
}
|
|
152
|
+
this._logger.debug({ fileName }, 'getCompiledOutput(): compiling using language service');
|
|
153
|
+
// Must set memory cache before attempting to compile
|
|
154
|
+
this._updateMemoryCache(fileContent, fileName, currentModuleKind === moduleKind);
|
|
155
|
+
const output = this._languageService.getEmitOutput(fileName);
|
|
156
|
+
const diagnostics = this.getDiagnostics(fileName);
|
|
157
|
+
if ((0, transpile_module_1.isModernNodeModuleKind)(this._initialCompilerOptions.module)) {
|
|
158
|
+
this.configSet.raiseDiagnostics([
|
|
159
|
+
{
|
|
160
|
+
category: this._ts.DiagnosticCategory.Message,
|
|
161
|
+
code: utils_1.TsJestDiagnosticCodes.ModernNodeModule,
|
|
162
|
+
messageText: messages_1.Helps.UsingModernNodeResolution,
|
|
163
|
+
file: undefined,
|
|
164
|
+
start: undefined,
|
|
165
|
+
length: undefined,
|
|
166
|
+
},
|
|
167
|
+
]);
|
|
168
|
+
}
|
|
169
|
+
if (!isEsmMode && diagnostics.length) {
|
|
170
|
+
this.configSet.raiseDiagnostics(diagnostics, fileName, this._logger);
|
|
171
|
+
if (options.watchMode) {
|
|
172
|
+
this._logger.debug({ fileName }, '_doTypeChecking(): starting watch mode computing diagnostics');
|
|
173
|
+
for (const entry of options.depGraphs.entries()) {
|
|
174
|
+
const normalizedModuleNames = entry[1].resolvedModuleNames.map((moduleName) => (0, path_1.normalize)(moduleName));
|
|
175
|
+
const fileToReTypeCheck = entry[0];
|
|
176
|
+
if (normalizedModuleNames.includes(fileName) && this.configSet.shouldReportDiagnostics(fileToReTypeCheck)) {
|
|
177
|
+
this._logger.debug({ fileToReTypeCheck }, '_doTypeChecking(): computing diagnostics using language service');
|
|
178
|
+
this._updateMemoryCache(this._getFileContentFromCache(fileToReTypeCheck), fileToReTypeCheck);
|
|
179
|
+
const importedModulesDiagnostics = [
|
|
180
|
+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
181
|
+
...this._languageService.getSemanticDiagnostics(fileToReTypeCheck),
|
|
182
|
+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
183
|
+
...this._languageService.getSyntacticDiagnostics(fileToReTypeCheck),
|
|
184
|
+
];
|
|
185
|
+
// will raise or just warn diagnostics depending on config
|
|
186
|
+
this.configSet.raiseDiagnostics(importedModulesDiagnostics, fileName, this._logger);
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
if (output.emitSkipped) {
|
|
192
|
+
if (constants_1.TS_TSX_REGEX.test(fileName)) {
|
|
193
|
+
throw new Error((0, messages_1.interpolate)("Unable to process '{{file}}', please make sure that `outDir` in your tsconfig is neither `''` or `'.'`. You can also configure Jest config option `transformIgnorePatterns` to inform `ts-jest` to transform {{file}}" /* Errors.CannotProcessFile */, { file: fileName }));
|
|
194
|
+
}
|
|
195
|
+
else {
|
|
196
|
+
this._logger.warn((0, messages_1.interpolate)("Unable to process '{{file}}', falling back to original file content. You can also configure Jest config option `transformIgnorePatterns` to ignore {{file}} from transformation or make sure that `outDir` in your tsconfig is neither `''` or `'.'`" /* Errors.CannotProcessFileReturnOriginal */, { file: fileName }));
|
|
197
|
+
return {
|
|
198
|
+
code: fileContent,
|
|
199
|
+
};
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
// Throw an error when requiring `.d.ts` files.
|
|
203
|
+
if (!output.outputFiles.length) {
|
|
204
|
+
throw new TypeError((0, messages_1.interpolate)("Unable to require `.d.ts` file for file: {{file}}.\nThis is usually the result of a faulty configuration or import. Make sure there is a `.js`, `.json` or another executable extension available alongside `{{file}}`." /* Errors.UnableToRequireDefinitionFile */, {
|
|
205
|
+
file: (0, path_1.basename)(fileName),
|
|
206
|
+
}));
|
|
207
|
+
}
|
|
208
|
+
const { outputFiles } = output;
|
|
209
|
+
return this._compilerOptions.sourceMap
|
|
210
|
+
? {
|
|
211
|
+
code: (0, compiler_utils_1.updateOutput)(outputFiles[1].text, fileName, outputFiles[0].text),
|
|
212
|
+
diagnostics,
|
|
213
|
+
}
|
|
214
|
+
: {
|
|
215
|
+
code: (0, compiler_utils_1.updateOutput)(outputFiles[0].text, fileName),
|
|
216
|
+
diagnostics,
|
|
217
|
+
};
|
|
218
|
+
}
|
|
219
|
+
else {
|
|
220
|
+
this._logger.debug({ fileName }, 'getCompiledOutput(): compiling as isolated module');
|
|
221
|
+
assertCompilerOptionsWithJestTransformMode(this._initialCompilerOptions, isEsmMode, this._logger);
|
|
222
|
+
const result = this._transpileOutput(fileContent, fileName);
|
|
223
|
+
if (result.diagnostics && this.configSet.shouldReportDiagnostics(fileName)) {
|
|
224
|
+
this.configSet.raiseDiagnostics(result.diagnostics, fileName, this._logger);
|
|
225
|
+
}
|
|
226
|
+
return {
|
|
227
|
+
code: (0, compiler_utils_1.updateOutput)(result.outputText, fileName, result.sourceMapText),
|
|
228
|
+
};
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
_transpileOutput(fileContent, fileName) {
|
|
232
|
+
/**
|
|
233
|
+
* @deprecated
|
|
234
|
+
*
|
|
235
|
+
* This code path should be removed in the next major version to benefit from checking on compiler options
|
|
236
|
+
*/
|
|
237
|
+
if (!(0, transpile_module_1.isModernNodeModuleKind)(this._initialCompilerOptions.module)) {
|
|
238
|
+
return this._ts.transpileModule(fileContent, {
|
|
239
|
+
fileName,
|
|
240
|
+
transformers: this._makeTransformers(this.configSet.resolvedTransformers),
|
|
241
|
+
compilerOptions: this._compilerOptions,
|
|
242
|
+
reportDiagnostics: this.configSet.shouldReportDiagnostics(fileName),
|
|
243
|
+
});
|
|
244
|
+
}
|
|
245
|
+
return (0, transpile_module_1.tsTranspileModule)(fileContent, {
|
|
246
|
+
fileName,
|
|
247
|
+
transformers: (program) => {
|
|
248
|
+
this.program = program;
|
|
249
|
+
return this._makeTransformers(this.configSet.resolvedTransformers);
|
|
250
|
+
},
|
|
251
|
+
compilerOptions: this._initialCompilerOptions,
|
|
252
|
+
reportDiagnostics: fileName ? this.configSet.shouldReportDiagnostics(fileName) : false,
|
|
253
|
+
});
|
|
254
|
+
}
|
|
255
|
+
_makeTransformers(customTransformers) {
|
|
256
|
+
return {
|
|
257
|
+
before: customTransformers.before.map((beforeTransformer) => beforeTransformer.factory(this, beforeTransformer.options)),
|
|
258
|
+
after: customTransformers.after.map((afterTransformer) => afterTransformer.factory(this, afterTransformer.options)),
|
|
259
|
+
afterDeclarations: customTransformers.afterDeclarations.map((afterDeclarations) => afterDeclarations.factory(this, afterDeclarations.options)),
|
|
260
|
+
};
|
|
261
|
+
}
|
|
262
|
+
/**
|
|
263
|
+
* @internal
|
|
264
|
+
*/
|
|
265
|
+
_createLanguageService() {
|
|
266
|
+
// Initialize memory cache for typescript compiler
|
|
267
|
+
this._parsedTsConfig.fileNames
|
|
268
|
+
.filter((fileName) => constants_1.TS_TSX_REGEX.test(fileName) && !this.configSet.isTestFile(fileName))
|
|
269
|
+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
270
|
+
.forEach((fileName) => this._fileVersionCache.set(fileName, 0));
|
|
271
|
+
/* istanbul ignore next */
|
|
272
|
+
const serviceHost = {
|
|
273
|
+
useCaseSensitiveFileNames: () => this._ts.sys.useCaseSensitiveFileNames,
|
|
274
|
+
getProjectVersion: () => String(this._projectVersion),
|
|
275
|
+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
276
|
+
getScriptFileNames: () => [...this._fileVersionCache.keys()],
|
|
277
|
+
getScriptVersion: (fileName) => {
|
|
278
|
+
const normalizedFileName = (0, path_1.normalize)(fileName);
|
|
279
|
+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
280
|
+
const version = this._fileVersionCache.get(normalizedFileName);
|
|
281
|
+
// We need to return `undefined` and not a string here because TypeScript will use
|
|
282
|
+
// `getScriptVersion` and compare against their own version - which can be `undefined`.
|
|
283
|
+
// If we don't return `undefined` it results in `undefined === "undefined"` and run
|
|
284
|
+
// `createProgram` again (which is very slow). Using a `string` assertion here to avoid
|
|
285
|
+
// TypeScript errors from the function signature (expects `(x: string) => string`).
|
|
286
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
287
|
+
return version === undefined ? undefined : String(version);
|
|
288
|
+
},
|
|
289
|
+
getScriptSnapshot: (fileName) => {
|
|
290
|
+
const normalizedFileName = (0, path_1.normalize)(fileName);
|
|
291
|
+
const hit = this._isFileInCache(normalizedFileName);
|
|
292
|
+
this._logger.trace({ normalizedFileName, cacheHit: hit }, 'getScriptSnapshot():', 'cache', hit ? 'hit' : 'miss');
|
|
293
|
+
// Read file content from either memory cache or Jest runtime cache or fallback to file system read
|
|
294
|
+
if (!hit) {
|
|
295
|
+
const fileContent =
|
|
296
|
+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
297
|
+
this._fileContentCache.get(normalizedFileName) ??
|
|
298
|
+
this._runtimeCacheFS.get(normalizedFileName) ??
|
|
299
|
+
this._cachedReadFile?.(normalizedFileName) ??
|
|
300
|
+
undefined;
|
|
301
|
+
if (fileContent !== undefined) {
|
|
302
|
+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
303
|
+
this._fileContentCache.set(normalizedFileName, fileContent);
|
|
304
|
+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
305
|
+
this._fileVersionCache.set(normalizedFileName, 1);
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
309
|
+
const contents = this._fileContentCache.get(normalizedFileName);
|
|
310
|
+
if (contents === undefined)
|
|
311
|
+
return;
|
|
312
|
+
return this._ts.ScriptSnapshot.fromString(contents);
|
|
313
|
+
},
|
|
314
|
+
fileExists: (0, lodash_memoize_1.default)(this._ts.sys.fileExists),
|
|
315
|
+
readFile: this._cachedReadFile ?? this._ts.sys.readFile,
|
|
316
|
+
readDirectory: (0, lodash_memoize_1.default)(this._ts.sys.readDirectory),
|
|
317
|
+
getDirectories: (0, lodash_memoize_1.default)(this._ts.sys.getDirectories),
|
|
318
|
+
directoryExists: (0, lodash_memoize_1.default)(this._ts.sys.directoryExists),
|
|
319
|
+
realpath: this._ts.sys.realpath && (0, lodash_memoize_1.default)(this._ts.sys.realpath),
|
|
320
|
+
getNewLine: () => constants_1.LINE_FEED,
|
|
321
|
+
getCurrentDirectory: () => this.configSet.cwd,
|
|
322
|
+
getCompilationSettings: () => this._compilerOptions,
|
|
323
|
+
getDefaultLibFileName: () => this._ts.getDefaultLibFilePath(this._compilerOptions),
|
|
324
|
+
getCustomTransformers: () => this._makeTransformers(this.configSet.resolvedTransformers),
|
|
325
|
+
resolveModuleNames: (moduleNames, containingFile) => moduleNames.map((moduleName) => this._resolveModuleName(moduleName, containingFile).resolvedModule),
|
|
326
|
+
};
|
|
327
|
+
this._logger.debug('created language service');
|
|
328
|
+
this._languageService = this._ts.createLanguageService(serviceHost, this._ts.createDocumentRegistry(this._ts.sys.useCaseSensitiveFileNames, this.configSet.cwd));
|
|
329
|
+
this.program = this._languageService.getProgram();
|
|
330
|
+
}
|
|
331
|
+
/**
|
|
332
|
+
* @internal
|
|
333
|
+
*/
|
|
334
|
+
_getFileContentFromCache(filePath) {
|
|
335
|
+
const normalizedFilePath = (0, path_1.normalize)(filePath);
|
|
336
|
+
let resolvedFileContent = this._runtimeCacheFS.get(normalizedFilePath);
|
|
337
|
+
if (!resolvedFileContent) {
|
|
338
|
+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
339
|
+
resolvedFileContent = this._moduleResolutionHost.readFile(normalizedFilePath);
|
|
340
|
+
this._runtimeCacheFS.set(normalizedFilePath, resolvedFileContent);
|
|
341
|
+
}
|
|
342
|
+
return resolvedFileContent;
|
|
343
|
+
}
|
|
344
|
+
/**
|
|
345
|
+
* @internal
|
|
346
|
+
*/
|
|
347
|
+
_getImportedModulePaths(resolvedFileContent, containingFile) {
|
|
348
|
+
return this._ts
|
|
349
|
+
.preProcessFile(resolvedFileContent, true, true)
|
|
350
|
+
.importedFiles.map((importedFile) => {
|
|
351
|
+
const { resolvedModule } = this._resolveModuleName(importedFile.fileName, containingFile);
|
|
352
|
+
/* istanbul ignore next already covered */
|
|
353
|
+
const resolvedFileName = resolvedModule?.resolvedFileName;
|
|
354
|
+
/* istanbul ignore next already covered */
|
|
355
|
+
return resolvedFileName && !resolvedModule?.isExternalLibraryImport ? resolvedFileName : '';
|
|
356
|
+
})
|
|
357
|
+
.filter((resolveFileName) => !!resolveFileName);
|
|
358
|
+
}
|
|
359
|
+
/**
|
|
360
|
+
* @internal
|
|
361
|
+
*/
|
|
362
|
+
_resolveModuleName(moduleNameToResolve, containingFile) {
|
|
363
|
+
return this._ts.resolveModuleName(moduleNameToResolve, containingFile, this._compilerOptions,
|
|
364
|
+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
365
|
+
this._moduleResolutionHost,
|
|
366
|
+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
367
|
+
this._moduleResolutionCache);
|
|
368
|
+
}
|
|
369
|
+
/**
|
|
370
|
+
* @internal
|
|
371
|
+
*/
|
|
372
|
+
_isFileInCache(fileName) {
|
|
373
|
+
return (
|
|
374
|
+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
375
|
+
this._fileContentCache.has(fileName) &&
|
|
376
|
+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
377
|
+
this._fileVersionCache.has(fileName) &&
|
|
378
|
+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
379
|
+
this._fileVersionCache.get(fileName) !== 0);
|
|
380
|
+
}
|
|
381
|
+
/**
|
|
382
|
+
* @internal
|
|
383
|
+
*/
|
|
384
|
+
_updateMemoryCache(contents, fileName, isModuleKindTheSame = true) {
|
|
385
|
+
this._logger.debug({ fileName }, 'updateMemoryCache: update memory cache for language service');
|
|
386
|
+
let shouldIncrementProjectVersion = false;
|
|
387
|
+
const hit = this._isFileInCache(fileName);
|
|
388
|
+
if (!hit) {
|
|
389
|
+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
390
|
+
this._fileVersionCache.set(fileName, 1);
|
|
391
|
+
shouldIncrementProjectVersion = true;
|
|
392
|
+
}
|
|
393
|
+
else {
|
|
394
|
+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
395
|
+
const prevVersion = this._fileVersionCache.get(fileName);
|
|
396
|
+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
397
|
+
const previousContents = this._fileContentCache.get(fileName);
|
|
398
|
+
// Avoid incrementing cache when nothing has changed.
|
|
399
|
+
if (previousContents !== contents) {
|
|
400
|
+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
401
|
+
this._fileVersionCache.set(fileName, prevVersion + 1);
|
|
402
|
+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
403
|
+
this._fileContentCache.set(fileName, contents);
|
|
404
|
+
shouldIncrementProjectVersion = true;
|
|
405
|
+
}
|
|
406
|
+
/**
|
|
407
|
+
* When a file is from node_modules or referenced to a referenced project and jest wants to transform it, we need
|
|
408
|
+
* to make sure that the Program is updated with this information
|
|
409
|
+
*/
|
|
410
|
+
if (!this._parsedTsConfig.fileNames.includes(fileName) || !isModuleKindTheSame) {
|
|
411
|
+
shouldIncrementProjectVersion = true;
|
|
412
|
+
}
|
|
413
|
+
}
|
|
414
|
+
if (shouldIncrementProjectVersion)
|
|
415
|
+
this._projectVersion++;
|
|
416
|
+
}
|
|
417
|
+
/**
|
|
418
|
+
* @internal
|
|
419
|
+
*/
|
|
420
|
+
getDiagnostics(fileName) {
|
|
421
|
+
const diagnostics = [];
|
|
422
|
+
if (this.configSet.shouldReportDiagnostics(fileName)) {
|
|
423
|
+
this._logger.debug({ fileName }, '_doTypeChecking(): computing diagnostics using language service');
|
|
424
|
+
// Get the relevant diagnostics - this is 3x faster than `getPreEmitDiagnostics`.
|
|
425
|
+
diagnostics.push(
|
|
426
|
+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
427
|
+
...this._languageService.getSemanticDiagnostics(fileName),
|
|
428
|
+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
429
|
+
...this._languageService.getSyntacticDiagnostics(fileName));
|
|
430
|
+
}
|
|
431
|
+
return diagnostics;
|
|
432
|
+
}
|
|
433
|
+
}
|
|
434
|
+
exports.TsCompiler = TsCompiler;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { CompilerInstance, CompiledOutput, StringMap, TsJestCompileOptions } from '../../types';
|
|
2
|
+
import type { ConfigSet } from '../config/config-set';
|
|
3
|
+
export declare class TsJestCompiler implements CompilerInstance {
|
|
4
|
+
private readonly _compilerInstance;
|
|
5
|
+
constructor(configSet: ConfigSet, runtimeCacheFS: StringMap);
|
|
6
|
+
getResolvedModules(fileContent: string, fileName: string, runtimeCacheFS: StringMap): string[];
|
|
7
|
+
getCompiledOutput(fileContent: string, fileName: string, options: TsJestCompileOptions): CompiledOutput;
|
|
8
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.TsJestCompiler = void 0;
|
|
4
|
+
const ts_compiler_1 = require("./ts-compiler");
|
|
5
|
+
class TsJestCompiler {
|
|
6
|
+
_compilerInstance;
|
|
7
|
+
constructor(configSet, runtimeCacheFS) {
|
|
8
|
+
// Later we can add swc/esbuild or other typescript compiler instance here
|
|
9
|
+
this._compilerInstance = new ts_compiler_1.TsCompiler(configSet, runtimeCacheFS);
|
|
10
|
+
}
|
|
11
|
+
getResolvedModules(fileContent, fileName, runtimeCacheFS) {
|
|
12
|
+
return this._compilerInstance.getResolvedModules(fileContent, fileName, runtimeCacheFS);
|
|
13
|
+
}
|
|
14
|
+
getCompiledOutput(fileContent, fileName, options) {
|
|
15
|
+
return this._compilerInstance.getCompiledOutput(fileContent, fileName, options);
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
exports.TsJestCompiler = TsJestCompiler;
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { Logger } from 'bs-logger';
|
|
2
|
+
import type * as ts from 'typescript';
|
|
3
|
+
import type { TsConfigCompilerOptionsJson } from '../../config';
|
|
4
|
+
import type { RawCompilerOptions } from '../../raw-compiler-options';
|
|
5
|
+
import type { TsJestAstTransformer, TsJestTransformOptions, TTypeScript } from '../../types';
|
|
6
|
+
export declare class ConfigSet {
|
|
7
|
+
readonly parentLogger?: Logger | undefined;
|
|
8
|
+
/**
|
|
9
|
+
* Use by e2e, don't mark as internal
|
|
10
|
+
*/
|
|
11
|
+
readonly tsJestDigest: string;
|
|
12
|
+
readonly logger: Logger;
|
|
13
|
+
readonly compilerModule: TTypeScript;
|
|
14
|
+
readonly isolatedModules: boolean;
|
|
15
|
+
readonly cwd: string;
|
|
16
|
+
readonly rootDir: string;
|
|
17
|
+
cacheSuffix: string;
|
|
18
|
+
tsCacheDir: string | undefined;
|
|
19
|
+
parsedTsConfig: ts.ParsedCommandLine | Record<string, any>;
|
|
20
|
+
resolvedTransformers: TsJestAstTransformer;
|
|
21
|
+
useESM: boolean;
|
|
22
|
+
constructor(jestConfig: TsJestTransformOptions['config'] | undefined, parentLogger?: Logger | undefined);
|
|
23
|
+
/**
|
|
24
|
+
* Load TypeScript configuration. Returns the parsed TypeScript config and any `tsconfig` options specified in ts-jest
|
|
25
|
+
* Subclasses which extend `ConfigSet` can override the default behavior
|
|
26
|
+
*/
|
|
27
|
+
protected _resolveTsConfig(compilerOptions?: RawCompilerOptions | TsConfigCompilerOptionsJson, resolvedConfigFile?: string): Record<string, any>;
|
|
28
|
+
isTestFile(fileName: string): boolean;
|
|
29
|
+
shouldStringifyContent(filePath: string): boolean;
|
|
30
|
+
raiseDiagnostics(diagnostics: ts.Diagnostic[], filePath?: string, logger?: Logger): void;
|
|
31
|
+
shouldReportDiagnostics(filePath: string): boolean;
|
|
32
|
+
resolvePath(inputPath: string, { throwIfMissing, nodeResolve }?: {
|
|
33
|
+
throwIfMissing?: boolean;
|
|
34
|
+
nodeResolve?: boolean;
|
|
35
|
+
}): string;
|
|
36
|
+
}
|