UNPKG

6.5 kBJavaScriptView Raw
1"use strict";
2var __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};
13Object.defineProperty(exports, "__esModule", { value: true });
14exports.TsJestTransformer = void 0;
15var util_1 = require("util");
16var config_set_1 = require("./config/config-set");
17var constants_1 = require("./constants");
18var json_1 = require("./util/json");
19var jsonable_value_1 = require("./util/jsonable-value");
20var logger_1 = require("./util/logger");
21var messages_1 = require("./util/messages");
22var sha1_1 = require("./util/sha1");
23var INSPECT_CUSTOM = util_1.inspect.custom || 'inspect';
24var TsJestTransformer = (function () {
25 function TsJestTransformer(baseOptions) {
26 if (baseOptions === void 0) { baseOptions = {}; }
27 this.options = __assign({}, baseOptions);
28 this.id = TsJestTransformer._nextTransformerId;
29 this.logger = logger_1.rootLogger.child({
30 transformerId: this.id,
31 namespace: 'jest-transformer',
32 });
33 this.logger.debug({ baseOptions: baseOptions }, 'created new transformer');
34 }
35 Object.defineProperty(TsJestTransformer, "_nextTransformerId", {
36 get: function () {
37 return ++TsJestTransformer._lastTransformerId;
38 },
39 enumerable: false,
40 configurable: true
41 });
42 TsJestTransformer.prototype[INSPECT_CUSTOM] = function () {
43 return "[object TsJestTransformer<#" + this.id + ">]";
44 };
45 TsJestTransformer.prototype.configsFor = function (jestConfig) {
46 var csi = TsJestTransformer._configSetsIndex.find(function (cs) { return cs.jestConfig.value === jestConfig; });
47 if (csi)
48 return csi.configSet;
49 var serialized = json_1.stringify(jestConfig);
50 csi = TsJestTransformer._configSetsIndex.find(function (cs) { return cs.jestConfig.serialized === serialized; });
51 if (csi) {
52 csi.jestConfig.value = jestConfig;
53 return csi.configSet;
54 }
55 var jestConfigObj = jestConfig;
56 this.logger.info('no matching config-set found, creating a new one');
57 var configSet = new config_set_1.ConfigSet(jestConfigObj, this.options, this.logger);
58 TsJestTransformer._configSetsIndex.push({
59 jestConfig: new jsonable_value_1.JsonableValue(jestConfigObj),
60 configSet: configSet,
61 });
62 return configSet;
63 };
64 TsJestTransformer.prototype.process = function (input, filePath, jestConfig, transformOptions) {
65 this.logger.debug({ fileName: filePath, transformOptions: transformOptions }, 'processing', filePath);
66 var result;
67 var source = input;
68 var configs = this.configsFor(jestConfig);
69 var hooks = configs.hooks;
70 var shouldStringifyContent = configs.shouldStringifyContent(filePath);
71 var babelJest = shouldStringifyContent ? undefined : configs.babelJestTransformer;
72 var isDefinitionFile = filePath.endsWith('.d.ts');
73 var isJsFile = constants_1.JS_JSX_REGEX.test(filePath);
74 var isTsFile = !isDefinitionFile && constants_1.TS_TSX_REGEX.test(filePath);
75 if (shouldStringifyContent) {
76 result = "module.exports=" + json_1.stringify(source);
77 }
78 else if (isDefinitionFile) {
79 result = '';
80 }
81 else if (!configs.parsedTsConfig.options.allowJs && isJsFile) {
82 this.logger.warn({ fileName: filePath }, 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", { path: filePath }));
83 result = source;
84 }
85 else if (isJsFile || isTsFile) {
86 result = configs.tsCompiler.compile(source, filePath);
87 }
88 else {
89 var 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." : "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.";
90 this.logger.warn({ fileName: filePath }, messages_1.interpolate(message, { path: filePath }));
91 result = source;
92 }
93 if (babelJest) {
94 this.logger.debug({ fileName: filePath }, 'calling babel-jest processor');
95 result = babelJest.process(result, filePath, jestConfig, __assign(__assign({}, transformOptions), { instrument: false }));
96 }
97 if (hooks.afterProcess) {
98 this.logger.debug({ fileName: filePath, hookName: 'afterProcess' }, 'calling afterProcess hook');
99 var newResult = hooks.afterProcess([input, filePath, jestConfig, transformOptions], result);
100 if (newResult !== undefined) {
101 return newResult;
102 }
103 }
104 return result;
105 };
106 TsJestTransformer.prototype.getCacheKey = function (fileContent, filePath, _jestConfigStr, transformOptions) {
107 this.logger.debug({ fileName: filePath, transformOptions: transformOptions }, 'computing cache key for', filePath);
108 var configs = this.configsFor(transformOptions.config);
109 var _a = transformOptions.instrument, instrument = _a === void 0 ? false : _a, _b = transformOptions.rootDir, rootDir = _b === void 0 ? configs.rootDir : _b;
110 return sha1_1.sha1(configs.cacheKey, '\x00', rootDir, '\x00', "instrument:" + (instrument ? 'on' : 'off'), '\x00', fileContent, '\x00', filePath);
111 };
112 TsJestTransformer._configSetsIndex = [];
113 TsJestTransformer._lastTransformerId = 0;
114 return TsJestTransformer;
115}());
116exports.TsJestTransformer = TsJestTransformer;