ts-jest 29.3.1 → 29.3.3
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/.ts-jest-digest +1 -1
- package/CHANGELOG.md +19 -0
- package/README.md +1 -1
- package/dist/cli/config/init.js +103 -128
- package/dist/cli/config/migrate.js +113 -156
- package/dist/cli/help.js +14 -42
- package/dist/cli/helpers/presets.js +7 -8
- package/dist/cli/index.js +47 -101
- package/dist/config/paths-to-module-name-mapper.js +37 -61
- package/dist/constants.js +1 -5
- package/dist/index.js +2 -2
- package/dist/legacy/compiler/compiler-utils.js +5 -5
- package/dist/legacy/compiler/ts-compiler.js +200 -213
- package/dist/legacy/compiler/ts-jest-compiler.js +9 -9
- package/dist/legacy/config/config-set.js +281 -292
- package/dist/legacy/index.js +2 -4
- package/dist/legacy/ts-jest-transformer.d.ts +0 -3
- package/dist/legacy/ts-jest-transformer.js +152 -211
- package/dist/presets/all-presets.js +2 -2
- package/dist/presets/create-jest-preset.js +101 -139
- package/dist/transformers/hoist-jest.js +30 -43
- package/dist/transpilers/typescript/transpile-module.js +69 -98
- package/dist/types.js +1 -1
- package/dist/utils/backports.js +26 -28
- package/dist/utils/get-package-version.js +2 -2
- package/dist/utils/importer.js +53 -108
- package/dist/utils/json.js +2 -28
- package/dist/utils/jsonable-value.js +20 -27
- package/dist/utils/logger.js +11 -15
- package/dist/utils/memoize.js +11 -17
- package/dist/utils/messages.js +2 -3
- package/dist/utils/sha1.js +7 -11
- package/dist/utils/ts-error.js +19 -35
- package/package.json +20 -20
- package/tsconfig.base.json +20 -0
package/dist/legacy/index.js
CHANGED
|
@@ -1,8 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
|
|
3
|
+
const ts_jest_transformer_1 = require("./ts-jest-transformer");
|
|
4
4
|
exports.default = {
|
|
5
|
-
createTransformer:
|
|
6
|
-
return new ts_jest_transformer_1.TsJestTransformer(tsJestConfig);
|
|
7
|
-
},
|
|
5
|
+
createTransformer: (tsJestConfig) => new ts_jest_transformer_1.TsJestTransformer(tsJestConfig),
|
|
8
6
|
};
|
|
@@ -12,9 +12,6 @@ export declare class TsJestTransformer implements SyncTransformer<TsJestTransfor
|
|
|
12
12
|
private _configsFor;
|
|
13
13
|
protected _createConfigSet(config: TsJestTransformOptions['config'] | undefined): ConfigSet;
|
|
14
14
|
protected _createCompiler(configSet: ConfigSet, cacheFS: Map<string, string>): void;
|
|
15
|
-
/**
|
|
16
|
-
* @public
|
|
17
|
-
*/
|
|
18
15
|
process(sourceText: string, sourcePath: string, transformOptions: TsJestTransformOptions): TransformedSource;
|
|
19
16
|
processAsync(sourceText: string, sourcePath: string, transformOptions: TsJestTransformOptions): Promise<TransformedSource>;
|
|
20
17
|
private processWithTs;
|
|
@@ -1,99 +1,42 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __assign = (this && this.__assign) || function () {
|
|
3
|
-
__assign = Object.assign || function(t) {
|
|
4
|
-
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
5
|
-
s = arguments[i];
|
|
6
|
-
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
7
|
-
t[p] = s[p];
|
|
8
|
-
}
|
|
9
|
-
return t;
|
|
10
|
-
};
|
|
11
|
-
return __assign.apply(this, arguments);
|
|
12
|
-
};
|
|
13
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
14
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
15
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
16
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
17
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
18
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
19
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
20
|
-
});
|
|
21
|
-
};
|
|
22
|
-
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
23
|
-
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
24
|
-
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
25
|
-
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
26
|
-
function step(op) {
|
|
27
|
-
if (f) throw new TypeError("Generator is already executing.");
|
|
28
|
-
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
29
|
-
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
30
|
-
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
31
|
-
switch (op[0]) {
|
|
32
|
-
case 0: case 1: t = op; break;
|
|
33
|
-
case 4: _.label++; return { value: op[1], done: false };
|
|
34
|
-
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
35
|
-
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
36
|
-
default:
|
|
37
|
-
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
38
|
-
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
39
|
-
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
40
|
-
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
41
|
-
if (t[2]) _.ops.pop();
|
|
42
|
-
_.trys.pop(); continue;
|
|
43
|
-
}
|
|
44
|
-
op = body.call(thisArg, _);
|
|
45
|
-
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
46
|
-
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
47
|
-
}
|
|
48
|
-
};
|
|
49
|
-
var __read = (this && this.__read) || function (o, n) {
|
|
50
|
-
var m = typeof Symbol === "function" && o[Symbol.iterator];
|
|
51
|
-
if (!m) return o;
|
|
52
|
-
var i = m.call(o), r, ar = [], e;
|
|
53
|
-
try {
|
|
54
|
-
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
|
|
55
|
-
}
|
|
56
|
-
catch (error) { e = { error: error }; }
|
|
57
|
-
finally {
|
|
58
|
-
try {
|
|
59
|
-
if (r && !r.done && (m = i["return"])) m.call(i);
|
|
60
|
-
}
|
|
61
|
-
finally { if (e) throw e.error; }
|
|
62
|
-
}
|
|
63
|
-
return ar;
|
|
64
|
-
};
|
|
65
|
-
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
|
|
66
|
-
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
|
67
|
-
if (ar || !(i in from)) {
|
|
68
|
-
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
|
|
69
|
-
ar[i] = from[i];
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
return to.concat(ar || Array.prototype.slice.call(from));
|
|
73
|
-
};
|
|
74
2
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
75
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
76
4
|
};
|
|
77
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
78
6
|
exports.TsJestTransformer = exports.CACHE_KEY_EL_SEPARATOR = void 0;
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
7
|
+
const fs_1 = require("fs");
|
|
8
|
+
const path_1 = __importDefault(require("path"));
|
|
9
|
+
const typescript_1 = __importDefault(require("typescript"));
|
|
10
|
+
const constants_1 = require("../constants");
|
|
11
|
+
const utils_1 = require("../utils");
|
|
12
|
+
const importer_1 = require("../utils/importer");
|
|
13
|
+
const messages_1 = require("../utils/messages");
|
|
14
|
+
const sha1_1 = require("../utils/sha1");
|
|
15
|
+
const compiler_1 = require("./compiler");
|
|
16
|
+
const compiler_utils_1 = require("./compiler/compiler-utils");
|
|
17
|
+
const config_set_1 = require("./config/config-set");
|
|
18
|
+
const isNodeModule = (filePath) => {
|
|
19
|
+
return path_1.default.normalize(filePath).split(path_1.default.sep).includes('node_modules');
|
|
20
|
+
};
|
|
88
21
|
/**
|
|
89
22
|
* @internal
|
|
90
23
|
*/
|
|
91
24
|
exports.CACHE_KEY_EL_SEPARATOR = '\x00';
|
|
92
|
-
|
|
93
|
-
|
|
25
|
+
class TsJestTransformer {
|
|
26
|
+
transformerOptions;
|
|
27
|
+
/**
|
|
28
|
+
* cache ConfigSet between test runs
|
|
29
|
+
*
|
|
30
|
+
* @internal
|
|
31
|
+
*/
|
|
32
|
+
static _cachedConfigSets = [];
|
|
33
|
+
_logger;
|
|
34
|
+
_compiler;
|
|
35
|
+
_transformCfgStr;
|
|
36
|
+
_depGraphs = new Map();
|
|
37
|
+
_watchMode = false;
|
|
38
|
+
constructor(transformerOptions) {
|
|
94
39
|
this.transformerOptions = transformerOptions;
|
|
95
|
-
this._depGraphs = new Map();
|
|
96
|
-
this._watchMode = false;
|
|
97
40
|
this._logger = utils_1.rootLogger.child({ namespace: 'ts-jest-transformer' });
|
|
98
41
|
/**
|
|
99
42
|
* For some unknown reasons, `this` is undefined in `getCacheKey` and `process`
|
|
@@ -106,11 +49,10 @@ var TsJestTransformer = /** @class */ (function () {
|
|
|
106
49
|
this._logger.debug('created new transformer');
|
|
107
50
|
process.env.TS_JEST = '1';
|
|
108
51
|
}
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
var configSet;
|
|
52
|
+
_configsFor(transformOptions) {
|
|
53
|
+
const { config, cacheFS } = transformOptions;
|
|
54
|
+
const ccs = TsJestTransformer._cachedConfigSets.find((cs) => cs.jestConfig.value === config);
|
|
55
|
+
let configSet;
|
|
114
56
|
if (ccs) {
|
|
115
57
|
this._transformCfgStr = ccs.transformerCfgStr;
|
|
116
58
|
this._compiler = ccs.compiler;
|
|
@@ -120,8 +62,8 @@ var TsJestTransformer = /** @class */ (function () {
|
|
|
120
62
|
}
|
|
121
63
|
else {
|
|
122
64
|
// try to look-it up by stringified version
|
|
123
|
-
|
|
124
|
-
|
|
65
|
+
const serializedJestCfg = (0, utils_1.stringify)(config);
|
|
66
|
+
const serializedCcs = TsJestTransformer._cachedConfigSets.find((cs) => cs.jestConfig.serialized === serializedJestCfg);
|
|
125
67
|
if (serializedCcs) {
|
|
126
68
|
// update the object so that we can find it later
|
|
127
69
|
// this happens because jest first calls getCacheKey with stringified version of
|
|
@@ -136,24 +78,34 @@ var TsJestTransformer = /** @class */ (function () {
|
|
|
136
78
|
else {
|
|
137
79
|
// create the new record in the index
|
|
138
80
|
this._logger.info('no matching config-set found, creating a new one');
|
|
139
|
-
if (
|
|
81
|
+
if (config.globals?.['ts-jest']) {
|
|
140
82
|
this._logger.warn("Define `ts-jest` config under `globals` is deprecated. Please do\ntransform: {\n <transform_regex>: ['ts-jest', { /* ts-jest config goes here in Jest */ }],\n},\nSee more at https://kulshekhar.github.io/ts-jest/docs/getting-started/presets#advanced" /* Deprecations.GlobalsTsJestConfigOption */);
|
|
141
83
|
}
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
?
|
|
84
|
+
const jestGlobalsConfig = config.globals ?? {};
|
|
85
|
+
const tsJestGlobalsConfig = jestGlobalsConfig['ts-jest'] ?? {};
|
|
86
|
+
const migratedConfig = this.transformerOptions
|
|
87
|
+
? {
|
|
88
|
+
...config,
|
|
89
|
+
globals: {
|
|
90
|
+
...jestGlobalsConfig,
|
|
91
|
+
'ts-jest': {
|
|
92
|
+
...tsJestGlobalsConfig,
|
|
93
|
+
...this.transformerOptions,
|
|
94
|
+
},
|
|
95
|
+
},
|
|
96
|
+
}
|
|
97
|
+
: config;
|
|
146
98
|
configSet = this._createConfigSet(migratedConfig);
|
|
147
|
-
|
|
99
|
+
const jest = { ...migratedConfig };
|
|
148
100
|
// we need to remove some stuff from jest config
|
|
149
101
|
// this which does not depend on config
|
|
150
|
-
|
|
151
|
-
this._transformCfgStr =
|
|
102
|
+
jest.cacheDirectory = undefined; // eslint-disable-line @typescript-eslint/no-explicit-any
|
|
103
|
+
this._transformCfgStr = `${new utils_1.JsonableValue(jest).serialized}${configSet.cacheSuffix}`;
|
|
152
104
|
this._createCompiler(configSet, cacheFS);
|
|
153
105
|
this._watchMode = process.argv.includes('--watch');
|
|
154
106
|
TsJestTransformer._cachedConfigSets.push({
|
|
155
107
|
jestConfig: new utils_1.JsonableValue(config),
|
|
156
|
-
configSet
|
|
108
|
+
configSet,
|
|
157
109
|
transformerCfgStr: this._transformCfgStr,
|
|
158
110
|
compiler: this._compiler,
|
|
159
111
|
depGraphs: this._depGraphs,
|
|
@@ -162,76 +114,68 @@ var TsJestTransformer = /** @class */ (function () {
|
|
|
162
114
|
}
|
|
163
115
|
}
|
|
164
116
|
return configSet;
|
|
165
|
-
}
|
|
166
|
-
|
|
117
|
+
}
|
|
118
|
+
_createConfigSet(config) {
|
|
167
119
|
return new config_set_1.ConfigSet(config);
|
|
168
|
-
}
|
|
169
|
-
|
|
120
|
+
}
|
|
121
|
+
_createCompiler(configSet, cacheFS) {
|
|
170
122
|
this._compiler = new compiler_1.TsJestCompiler(configSet, cacheFS);
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
var shouldStringifyContent = configs.shouldStringifyContent(sourcePath);
|
|
179
|
-
var babelJest = shouldStringifyContent ? undefined : configs.babelJestTransformer;
|
|
180
|
-
var result = {
|
|
123
|
+
}
|
|
124
|
+
process(sourceText, sourcePath, transformOptions) {
|
|
125
|
+
this._logger.debug({ fileName: sourcePath, transformOptions }, 'processing', sourcePath);
|
|
126
|
+
const configs = this._configsFor(transformOptions);
|
|
127
|
+
const shouldStringifyContent = configs.shouldStringifyContent(sourcePath);
|
|
128
|
+
const babelJest = shouldStringifyContent ? undefined : configs.babelJestTransformer;
|
|
129
|
+
let result = {
|
|
181
130
|
code: this.processWithTs(sourceText, sourcePath, transformOptions).code,
|
|
182
131
|
};
|
|
183
132
|
if (babelJest) {
|
|
184
133
|
this._logger.debug({ fileName: sourcePath }, 'calling babel-jest processor');
|
|
185
134
|
// do not instrument here, jest will do it anyway afterwards
|
|
186
|
-
result = babelJest.process(result.code, sourcePath,
|
|
135
|
+
result = babelJest.process(result.code, sourcePath, {
|
|
136
|
+
...transformOptions,
|
|
137
|
+
instrument: false,
|
|
138
|
+
});
|
|
187
139
|
}
|
|
188
140
|
result = this.runTsJestHook(sourcePath, sourceText, transformOptions, result);
|
|
189
141
|
return result;
|
|
190
|
-
}
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
this._logger.debug({ fileName: sourcePath }, 'calling babel-jest processor');
|
|
211
|
-
return [4 /*yield*/, babelJest.processAsync(result.code, sourcePath, __assign(__assign({}, transformOptions), { instrument: false }))];
|
|
212
|
-
case 1:
|
|
213
|
-
// do not instrument here, jest will do it anyway afterwards
|
|
214
|
-
result = _b.sent();
|
|
215
|
-
_b.label = 2;
|
|
216
|
-
case 2:
|
|
217
|
-
result = this.runTsJestHook(sourcePath, sourceText, transformOptions, result);
|
|
218
|
-
return [2 /*return*/, result];
|
|
219
|
-
}
|
|
142
|
+
}
|
|
143
|
+
async processAsync(sourceText, sourcePath, transformOptions) {
|
|
144
|
+
this._logger.debug({ fileName: sourcePath, transformOptions }, 'processing', sourcePath);
|
|
145
|
+
const configs = this._configsFor(transformOptions);
|
|
146
|
+
const shouldStringifyContent = configs.shouldStringifyContent(sourcePath);
|
|
147
|
+
const babelJest = shouldStringifyContent ? undefined : configs.babelJestTransformer;
|
|
148
|
+
let result;
|
|
149
|
+
const processWithTsResult = this.processWithTs(sourceText, sourcePath, transformOptions);
|
|
150
|
+
result = {
|
|
151
|
+
code: processWithTsResult.code,
|
|
152
|
+
};
|
|
153
|
+
if (processWithTsResult.diagnostics?.length) {
|
|
154
|
+
throw configs.createTsError(processWithTsResult.diagnostics);
|
|
155
|
+
}
|
|
156
|
+
if (babelJest) {
|
|
157
|
+
this._logger.debug({ fileName: sourcePath }, 'calling babel-jest processor');
|
|
158
|
+
// do not instrument here, jest will do it anyway afterwards
|
|
159
|
+
result = await babelJest.processAsync(result.code, sourcePath, {
|
|
160
|
+
...transformOptions,
|
|
161
|
+
instrument: false,
|
|
220
162
|
});
|
|
221
|
-
}
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
163
|
+
}
|
|
164
|
+
result = this.runTsJestHook(sourcePath, sourceText, transformOptions, result);
|
|
165
|
+
return result;
|
|
166
|
+
}
|
|
167
|
+
processWithTs(sourceText, sourcePath, transformOptions) {
|
|
168
|
+
let result;
|
|
169
|
+
const configs = this._configsFor(transformOptions);
|
|
170
|
+
const shouldStringifyContent = configs.shouldStringifyContent(sourcePath);
|
|
171
|
+
const babelJest = shouldStringifyContent ? undefined : configs.babelJestTransformer;
|
|
172
|
+
const isDefinitionFile = sourcePath.endsWith(constants_1.DECLARATION_TYPE_EXT);
|
|
173
|
+
const isJsFile = constants_1.JS_JSX_REGEX.test(sourcePath);
|
|
174
|
+
const isTsFile = !isDefinitionFile && constants_1.TS_TSX_REGEX.test(sourcePath);
|
|
231
175
|
if (shouldStringifyContent) {
|
|
232
176
|
// handles here what we should simply stringify
|
|
233
177
|
result = {
|
|
234
|
-
code:
|
|
178
|
+
code: `module.exports=${(0, utils_1.stringify)(sourceText)}`,
|
|
235
179
|
};
|
|
236
180
|
}
|
|
237
181
|
else if (isDefinitionFile) {
|
|
@@ -240,51 +184,60 @@ var TsJestTransformer = /** @class */ (function () {
|
|
|
240
184
|
code: '',
|
|
241
185
|
};
|
|
242
186
|
}
|
|
243
|
-
else if (!configs.parsedTsConfig.options.allowJs && isJsFile) {
|
|
244
|
-
// we've got a '.js' but the compiler option `allowJs` is not set or set to false
|
|
245
|
-
this._logger.warn({ fileName: sourcePath }, (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: sourcePath }));
|
|
246
|
-
result = {
|
|
247
|
-
code: sourceText,
|
|
248
|
-
};
|
|
249
|
-
}
|
|
250
187
|
else if (isJsFile || isTsFile) {
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
188
|
+
if (isJsFile && isNodeModule(sourcePath)) {
|
|
189
|
+
const transpiledResult = typescript_1.default.transpileModule(sourceText, {
|
|
190
|
+
compilerOptions: {
|
|
191
|
+
...configs.parsedTsConfig.options,
|
|
192
|
+
module: transformOptions.supportsStaticESM && transformOptions.transformerConfig.useESM
|
|
193
|
+
? typescript_1.default.ModuleKind.ESNext
|
|
194
|
+
: typescript_1.default.ModuleKind.CommonJS,
|
|
195
|
+
},
|
|
196
|
+
fileName: sourcePath,
|
|
197
|
+
});
|
|
198
|
+
result = {
|
|
199
|
+
code: (0, compiler_utils_1.updateOutput)(transpiledResult.outputText, sourcePath, transpiledResult.sourceMapText),
|
|
200
|
+
};
|
|
201
|
+
}
|
|
202
|
+
else {
|
|
203
|
+
// transpile TS code (source maps are included)
|
|
204
|
+
result = this._compiler.getCompiledOutput(sourceText, sourcePath, {
|
|
205
|
+
depGraphs: this._depGraphs,
|
|
206
|
+
supportsStaticESM: transformOptions.supportsStaticESM,
|
|
207
|
+
watchMode: this._watchMode,
|
|
208
|
+
});
|
|
209
|
+
}
|
|
257
210
|
}
|
|
258
211
|
else {
|
|
259
212
|
// we should not get called for files with other extension than js[x], ts[x] and d.ts,
|
|
260
213
|
// TypeScript will bail if we try to compile, and if it was to call babel, users can
|
|
261
214
|
// define the transform value with `babel-jest` for this extension instead
|
|
262
|
-
|
|
215
|
+
const message = babelJest ? "Got a unknown file type to compile (file: {{path}}). To fix this, in your Jest config change the `transform` key which value is `ts-jest` so that it does not match this kind of files anymore. If you still want Babel to process it, add another entry to the `transform` option with value `babel-jest` which key matches this type of files." /* Errors.GotUnknownFileTypeWithBabel */ : "Got a unknown file type to compile (file: {{path}}). To fix this, in your Jest config change the `transform` key which value is `ts-jest` so that it does not match this kind of files anymore." /* Errors.GotUnknownFileTypeWithoutBabel */;
|
|
263
216
|
this._logger.warn({ fileName: sourcePath }, (0, messages_1.interpolate)(message, { path: sourcePath }));
|
|
264
217
|
result = {
|
|
265
218
|
code: sourceText,
|
|
266
219
|
};
|
|
267
220
|
}
|
|
268
221
|
return result;
|
|
269
|
-
}
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
222
|
+
}
|
|
223
|
+
runTsJestHook(sourcePath, sourceText, transformOptions, compiledOutput) {
|
|
224
|
+
let hooksFile = process.env.TS_JEST_HOOKS;
|
|
225
|
+
let hooks;
|
|
273
226
|
/* istanbul ignore next (cover by e2e) */
|
|
274
227
|
if (hooksFile) {
|
|
275
228
|
hooksFile = path_1.default.resolve(this._configsFor(transformOptions).cwd, hooksFile);
|
|
276
229
|
hooks = importer_1.importer.tryTheseOr(hooksFile, {});
|
|
277
230
|
}
|
|
278
231
|
// This is not supposed to be a public API but we keep it as some people use it
|
|
279
|
-
if (hooks
|
|
232
|
+
if (hooks?.afterProcess) {
|
|
280
233
|
this._logger.debug({ fileName: sourcePath, hookName: 'afterProcess' }, 'calling afterProcess hook');
|
|
281
|
-
|
|
234
|
+
const newResult = hooks.afterProcess([sourceText, sourcePath, transformOptions.config, transformOptions], compiledOutput);
|
|
282
235
|
if (newResult) {
|
|
283
236
|
return newResult;
|
|
284
237
|
}
|
|
285
238
|
}
|
|
286
239
|
return compiledOutput;
|
|
287
|
-
}
|
|
240
|
+
}
|
|
288
241
|
/**
|
|
289
242
|
* Jest uses this to cache the compiled version of a file
|
|
290
243
|
*
|
|
@@ -292,61 +245,49 @@ var TsJestTransformer = /** @class */ (function () {
|
|
|
292
245
|
*
|
|
293
246
|
* @public
|
|
294
247
|
*/
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
this._logger.debug({ fileName: filePath, transformOptions: transformOptions }, 'computing cache key for', filePath);
|
|
248
|
+
getCacheKey(fileContent, filePath, transformOptions) {
|
|
249
|
+
const configs = this._configsFor(transformOptions);
|
|
250
|
+
this._logger.debug({ fileName: filePath, transformOptions }, 'computing cache key for', filePath);
|
|
299
251
|
// we do not instrument, ensure it is false all the time
|
|
300
|
-
|
|
301
|
-
|
|
252
|
+
const { supportsStaticESM, instrument = false } = transformOptions;
|
|
253
|
+
const constructingCacheKeyElements = [
|
|
302
254
|
this._transformCfgStr,
|
|
303
255
|
exports.CACHE_KEY_EL_SEPARATOR,
|
|
304
256
|
configs.rootDir,
|
|
305
257
|
exports.CACHE_KEY_EL_SEPARATOR,
|
|
306
|
-
|
|
258
|
+
`instrument:${instrument ? 'on' : 'off'}`,
|
|
307
259
|
exports.CACHE_KEY_EL_SEPARATOR,
|
|
308
|
-
|
|
260
|
+
`supportsStaticESM:${supportsStaticESM ? 'on' : 'off'}`,
|
|
309
261
|
exports.CACHE_KEY_EL_SEPARATOR,
|
|
310
262
|
fileContent,
|
|
311
263
|
exports.CACHE_KEY_EL_SEPARATOR,
|
|
312
264
|
filePath,
|
|
313
265
|
];
|
|
314
266
|
if (!configs.isolatedModules && configs.tsCacheDir) {
|
|
315
|
-
|
|
316
|
-
if (
|
|
317
|
-
this._logger.debug({ fileName: filePath, transformOptions
|
|
267
|
+
let resolvedModuleNames;
|
|
268
|
+
if (this._depGraphs.get(filePath)?.fileContent === fileContent) {
|
|
269
|
+
this._logger.debug({ fileName: filePath, transformOptions }, 'getting resolved modules from disk caching or memory caching for', filePath);
|
|
318
270
|
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
319
271
|
resolvedModuleNames = this._depGraphs
|
|
320
272
|
.get(filePath)
|
|
321
|
-
.resolvedModuleNames.filter(
|
|
273
|
+
.resolvedModuleNames.filter((moduleName) => (0, fs_1.existsSync)(moduleName));
|
|
322
274
|
}
|
|
323
275
|
else {
|
|
324
|
-
this._logger.debug({ fileName: filePath, transformOptions
|
|
276
|
+
this._logger.debug({ fileName: filePath, transformOptions }, 'getting resolved modules from TypeScript API for', filePath);
|
|
325
277
|
resolvedModuleNames = this._compiler.getResolvedModules(fileContent, filePath, transformOptions.cacheFS);
|
|
326
278
|
this._depGraphs.set(filePath, {
|
|
327
|
-
fileContent
|
|
328
|
-
resolvedModuleNames
|
|
279
|
+
fileContent,
|
|
280
|
+
resolvedModuleNames,
|
|
329
281
|
});
|
|
330
282
|
}
|
|
331
|
-
resolvedModuleNames.forEach(
|
|
283
|
+
resolvedModuleNames.forEach((moduleName) => {
|
|
332
284
|
constructingCacheKeyElements.push(exports.CACHE_KEY_EL_SEPARATOR, moduleName, exports.CACHE_KEY_EL_SEPARATOR, (0, fs_1.statSync)(moduleName).mtimeMs.toString());
|
|
333
285
|
});
|
|
334
286
|
}
|
|
335
|
-
return
|
|
336
|
-
}
|
|
337
|
-
|
|
338
|
-
return
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
});
|
|
342
|
-
});
|
|
343
|
-
};
|
|
344
|
-
/**
|
|
345
|
-
* cache ConfigSet between test runs
|
|
346
|
-
*
|
|
347
|
-
* @internal
|
|
348
|
-
*/
|
|
349
|
-
TsJestTransformer._cachedConfigSets = [];
|
|
350
|
-
return TsJestTransformer;
|
|
351
|
-
}());
|
|
287
|
+
return (0, sha1_1.sha1)(...constructingCacheKeyElements);
|
|
288
|
+
}
|
|
289
|
+
async getCacheKeyAsync(sourceText, sourcePath, transformOptions) {
|
|
290
|
+
return Promise.resolve(this.getCacheKey(sourceText, sourcePath, transformOptions));
|
|
291
|
+
}
|
|
292
|
+
}
|
|
352
293
|
exports.TsJestTransformer = TsJestTransformer;
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
|
|
4
|
-
|
|
3
|
+
const create_jest_preset_1 = require("./create-jest-preset");
|
|
4
|
+
const allPresets = {
|
|
5
5
|
get defaults() {
|
|
6
6
|
return (0, create_jest_preset_1.createDefaultPreset)();
|
|
7
7
|
},
|