config-set.js 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799
  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 __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
  14. var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
  15. if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
  16. else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
  17. return c > 3 && r && Object.defineProperty(target, key, r), r;
  18. };
  19. var __values = (this && this.__values) || function(o) {
  20. var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
  21. if (m) return m.call(o);
  22. if (o && typeof o.length === "number") return {
  23. next: function () {
  24. if (o && i >= o.length) o = void 0;
  25. return { value: o && o[i++], done: !o };
  26. }
  27. };
  28. throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
  29. };
  30. var __read = (this && this.__read) || function (o, n) {
  31. var m = typeof Symbol === "function" && o[Symbol.iterator];
  32. if (!m) return o;
  33. var i = m.call(o), r, ar = [], e;
  34. try {
  35. while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
  36. }
  37. catch (error) { e = { error: error }; }
  38. finally {
  39. try {
  40. if (r && !r.done && (m = i["return"])) m.call(i);
  41. }
  42. finally { if (e) throw e.error; }
  43. }
  44. return ar;
  45. };
  46. var __spread = (this && this.__spread) || function () {
  47. for (var ar = [], i = 0; i < arguments.length; i++) ar = ar.concat(__read(arguments[i]));
  48. return ar;
  49. };
  50. Object.defineProperty(exports, "__esModule", { value: true });
  51. var bs_logger_1 = require("bs-logger");
  52. var fs_1 = require("fs");
  53. var json5 = require("json5");
  54. var path_1 = require("path");
  55. var semver = require("semver");
  56. var __1 = require("..");
  57. var compiler_1 = require("../compiler");
  58. var transformers_1 = require("../transformers");
  59. var backports_1 = require("../util/backports");
  60. var get_package_version_1 = require("../util/get-package-version");
  61. var importer_1 = require("../util/importer");
  62. var json_1 = require("../util/json");
  63. var jsonable_value_1 = require("../util/jsonable-value");
  64. var logger_1 = require("../util/logger");
  65. var memoize_1 = require("../util/memoize");
  66. var messages_1 = require("../util/messages");
  67. var normalize_slashes_1 = require("../util/normalize-slashes");
  68. var sha1_1 = require("../util/sha1");
  69. var ts_error_1 = require("../util/ts-error");
  70. var logger = logger_1.rootLogger.child({ namespace: 'config' });
  71. exports.MATCH_NOTHING = /a^/;
  72. exports.IGNORE_DIAGNOSTIC_CODES = [
  73. 6059,
  74. 18002,
  75. 18003,
  76. ];
  77. var DiagnosticCodes;
  78. (function (DiagnosticCodes) {
  79. DiagnosticCodes[DiagnosticCodes["TsJest"] = 151000] = "TsJest";
  80. DiagnosticCodes[DiagnosticCodes["ConfigModuleOption"] = 151001] = "ConfigModuleOption";
  81. })(DiagnosticCodes = exports.DiagnosticCodes || (exports.DiagnosticCodes = {}));
  82. var normalizeRegex = function (pattern) {
  83. return pattern ? (typeof pattern === 'string' ? pattern : pattern.source) : undefined;
  84. };
  85. var toDiagnosticCode = function (code) {
  86. return code ? parseInt(("" + code).trim().replace(/^TS/, ''), 10) || undefined : undefined;
  87. };
  88. var toDiagnosticCodeList = function (items, into) {
  89. var e_1, _a;
  90. if (into === void 0) { into = []; }
  91. if (!Array.isArray(items))
  92. items = [items];
  93. try {
  94. for (var items_1 = __values(items), items_1_1 = items_1.next(); !items_1_1.done; items_1_1 = items_1.next()) {
  95. var item = items_1_1.value;
  96. if (!item)
  97. continue;
  98. if (Array.isArray(item)) {
  99. toDiagnosticCodeList(item, into);
  100. continue;
  101. }
  102. else if (typeof item === 'string') {
  103. var children = item.trim().split(/\s*,\s*/g);
  104. if (children.length > 1) {
  105. toDiagnosticCodeList(children, into);
  106. continue;
  107. }
  108. item = children[0];
  109. }
  110. if (!item)
  111. continue;
  112. var code = toDiagnosticCode(item);
  113. if (code && !into.includes(code))
  114. into.push(code);
  115. }
  116. }
  117. catch (e_1_1) { e_1 = { error: e_1_1 }; }
  118. finally {
  119. try {
  120. if (items_1_1 && !items_1_1.done && (_a = items_1.return)) _a.call(items_1);
  121. }
  122. finally { if (e_1) throw e_1.error; }
  123. }
  124. return into;
  125. };
  126. var ConfigSet = (function () {
  127. function ConfigSet(jestConfig, parentOptions, parentLogger) {
  128. var _a;
  129. this.parentOptions = parentOptions;
  130. this._jestConfig = jestConfig;
  131. this.logger = parentLogger ? parentLogger.child((_a = {}, _a[bs_logger_1.LogContexts.namespace] = 'config', _a)) : logger;
  132. }
  133. Object.defineProperty(ConfigSet.prototype, "projectPackageJson", {
  134. get: function () {
  135. var packageJson = this.tsJest.packageJson;
  136. if (packageJson && packageJson.kind === 'inline') {
  137. return packageJson.value;
  138. }
  139. if (packageJson && packageJson.kind === 'file' && packageJson.value) {
  140. var path = this.resolvePath(packageJson.value);
  141. if (fs_1.existsSync(path)) {
  142. return require(path);
  143. }
  144. this.logger.warn(messages_1.Errors.UnableToFindProjectRoot);
  145. return {};
  146. }
  147. var tsJestRoot = path_1.resolve(__dirname, '..', '..');
  148. var pkgPath = path_1.resolve(tsJestRoot, '..', '..', 'package.json');
  149. if (fs_1.existsSync(pkgPath)) {
  150. return require(pkgPath);
  151. }
  152. if (fs_1.realpathSync(this.rootDir) === fs_1.realpathSync(tsJestRoot)) {
  153. pkgPath = path_1.resolve(tsJestRoot, 'package.json');
  154. if (fs_1.existsSync(pkgPath)) {
  155. return require(pkgPath);
  156. }
  157. }
  158. this.logger.warn(messages_1.Errors.UnableToFindProjectRoot);
  159. return {};
  160. },
  161. enumerable: true,
  162. configurable: true
  163. });
  164. Object.defineProperty(ConfigSet.prototype, "projectDependencies", {
  165. get: function () {
  166. var pkg = this.projectPackageJson;
  167. var names = Object.keys(__assign(__assign(__assign(__assign({}, pkg.optionalDependencies), pkg.peerDependencies), pkg.devDependencies), pkg.dependencies));
  168. return names.reduce(function (map, name) {
  169. var version = get_package_version_1.getPackageVersion(name);
  170. if (version)
  171. map[name] = version;
  172. return map;
  173. }, {});
  174. },
  175. enumerable: true,
  176. configurable: true
  177. });
  178. Object.defineProperty(ConfigSet.prototype, "jest", {
  179. get: function () {
  180. var config = backports_1.backportJestConfig(this.logger, this._jestConfig);
  181. if (this.parentOptions) {
  182. var globals = config.globals || (config.globals = {});
  183. globals['ts-jest'] = __assign(__assign({}, this.parentOptions), globals['ts-jest']);
  184. }
  185. this.logger.debug({ jestConfig: config }, 'normalized jest config');
  186. return config;
  187. },
  188. enumerable: true,
  189. configurable: true
  190. });
  191. Object.defineProperty(ConfigSet.prototype, "tsJest", {
  192. get: function () {
  193. var _this = this;
  194. var parsedConfig = this.jest;
  195. var _a = parsedConfig.globals, globals = _a === void 0 ? {} : _a;
  196. var options = __assign({}, globals['ts-jest']);
  197. var tsConfigOpt = options.tsConfig;
  198. var tsConfig;
  199. if (typeof tsConfigOpt === 'string' || tsConfigOpt == null || tsConfigOpt === true) {
  200. tsConfig = {
  201. kind: 'file',
  202. value: typeof tsConfigOpt === 'string' ? this.resolvePath(tsConfigOpt) : undefined,
  203. };
  204. }
  205. else if (typeof tsConfigOpt === 'object') {
  206. tsConfig = {
  207. kind: 'inline',
  208. value: tsConfigOpt,
  209. };
  210. }
  211. var packageJsonOpt = options.packageJson;
  212. var packageJson;
  213. if (typeof packageJsonOpt === 'string' || packageJsonOpt == null || packageJsonOpt === true) {
  214. packageJson = {
  215. kind: 'file',
  216. value: typeof packageJsonOpt === 'string' ? this.resolvePath(packageJsonOpt) : undefined,
  217. };
  218. }
  219. else if (typeof packageJsonOpt === 'object') {
  220. packageJson = {
  221. kind: 'inline',
  222. value: packageJsonOpt,
  223. };
  224. }
  225. var transformers = (options.astTransformers || []).map(function (mod) { return _this.resolvePath(mod, { nodeResolve: true }); });
  226. var babelConfigOpt = options.babelConfig;
  227. var babelConfig;
  228. if (typeof babelConfigOpt === 'string' || babelConfigOpt === true) {
  229. babelConfig = {
  230. kind: 'file',
  231. value: babelConfigOpt === true ? undefined : this.resolvePath(babelConfigOpt),
  232. };
  233. }
  234. else if (babelConfigOpt) {
  235. babelConfig = {
  236. kind: 'inline',
  237. value: babelConfigOpt,
  238. };
  239. }
  240. var diagnostics;
  241. var _b = options.diagnostics, diagnosticsOpt = _b === void 0 ? true : _b;
  242. var ignoreList = [exports.IGNORE_DIAGNOSTIC_CODES, process.env.TS_JEST_IGNORE_DIAGNOSTICS];
  243. if (diagnosticsOpt === true || diagnosticsOpt == null) {
  244. diagnostics = { ignoreCodes: [], pretty: true, throws: true };
  245. }
  246. else if (diagnosticsOpt === false) {
  247. diagnostics = {
  248. throws: false,
  249. pretty: true,
  250. ignoreCodes: [],
  251. pathRegex: exports.MATCH_NOTHING.source,
  252. };
  253. }
  254. else {
  255. ignoreList.push(diagnosticsOpt.ignoreCodes);
  256. diagnostics = {
  257. pretty: diagnosticsOpt.pretty == null ? true : !!diagnosticsOpt.pretty,
  258. ignoreCodes: [],
  259. pathRegex: normalizeRegex(diagnosticsOpt.pathRegex),
  260. throws: !diagnosticsOpt.warnOnly,
  261. };
  262. }
  263. diagnostics.ignoreCodes = toDiagnosticCodeList(ignoreList);
  264. var stringifyContentPathRegex = normalizeRegex(options.stringifyContentPathRegex);
  265. var res = {
  266. tsConfig: tsConfig,
  267. packageJson: packageJson,
  268. babelConfig: babelConfig,
  269. diagnostics: diagnostics,
  270. isolatedModules: !!options.isolatedModules,
  271. compiler: options.compiler || 'typescript',
  272. transformers: transformers,
  273. stringifyContentPathRegex: stringifyContentPathRegex,
  274. };
  275. this.logger.debug({ tsJestConfig: res }, 'normalized ts-jest config');
  276. return res;
  277. },
  278. enumerable: true,
  279. configurable: true
  280. });
  281. Object.defineProperty(ConfigSet.prototype, "typescript", {
  282. get: function () {
  283. return this._typescript.resolved;
  284. },
  285. enumerable: true,
  286. configurable: true
  287. });
  288. Object.defineProperty(ConfigSet.prototype, "tsconfig", {
  289. get: function () {
  290. return this._typescript.input;
  291. },
  292. enumerable: true,
  293. configurable: true
  294. });
  295. Object.defineProperty(ConfigSet.prototype, "versions", {
  296. get: function () {
  297. var modules = ['jest', this.tsJest.compiler];
  298. if (this.tsJest.babelConfig) {
  299. modules.push('@babel/core', 'babel-jest');
  300. }
  301. return modules.reduce(function (map, name) {
  302. map[name] = get_package_version_1.getPackageVersion(name) || '-';
  303. return map;
  304. }, { 'ts-jest': __1.version });
  305. },
  306. enumerable: true,
  307. configurable: true
  308. });
  309. Object.defineProperty(ConfigSet.prototype, "_typescript", {
  310. get: function () {
  311. var tsConfig = this.tsJest.tsConfig;
  312. var configFilePath = tsConfig && tsConfig.kind === 'file' ? tsConfig.value : undefined;
  313. var result = this.readTsConfig(tsConfig && tsConfig.kind === 'inline' ? tsConfig.value : undefined, configFilePath, tsConfig == null);
  314. this.raiseDiagnostics(result.resolved.errors, configFilePath);
  315. this.logger.debug({ tsconfig: result }, 'normalized typescript config');
  316. return result;
  317. },
  318. enumerable: true,
  319. configurable: true
  320. });
  321. Object.defineProperty(ConfigSet.prototype, "raiseDiagnostics", {
  322. get: function () {
  323. var _this = this;
  324. var _a = this, createTsError = _a.createTsError, filterDiagnostics = _a.filterDiagnostics, throws = _a.tsJest.diagnostics.throws, DiagnosticCategory = _a.compilerModule.DiagnosticCategory;
  325. return function (diagnostics, filePath, logger) {
  326. if (logger === void 0) { logger = _this.logger; }
  327. var filteredDiagnostics = filterDiagnostics(diagnostics, filePath);
  328. if (filteredDiagnostics.length === 0)
  329. return;
  330. var error = createTsError(filteredDiagnostics);
  331. var importantCategories = [DiagnosticCategory.Warning, DiagnosticCategory.Error];
  332. if (throws && filteredDiagnostics.some(function (d) { return importantCategories.includes(d.category); })) {
  333. throw error;
  334. }
  335. logger.warn({ error: error }, error.message);
  336. };
  337. },
  338. enumerable: true,
  339. configurable: true
  340. });
  341. ConfigSet.loadConfig = function (base) {
  342. var _a = importer_1.importer.babelCore(messages_1.ImportReasons.BabelJest), OptionManager = _a.OptionManager, loadPartialConfig = _a.loadPartialConfig, version = _a.version;
  343. if (version && semver.satisfies(version, '>=6 <7')) {
  344. delete base.cwd;
  345. }
  346. if (typeof loadPartialConfig === 'function') {
  347. var partialConfig = loadPartialConfig(base);
  348. if (partialConfig) {
  349. return partialConfig.options;
  350. }
  351. }
  352. return new OptionManager().init(base);
  353. };
  354. Object.defineProperty(ConfigSet.prototype, "babel", {
  355. get: function () {
  356. var babelConfig = this.tsJest.babelConfig;
  357. if (babelConfig == null) {
  358. this.logger.debug('babel is disabled');
  359. return;
  360. }
  361. var base = { cwd: this.cwd };
  362. if (babelConfig.kind === 'file') {
  363. if (babelConfig.value) {
  364. if (path_1.extname(babelConfig.value) === '.js') {
  365. base = __assign(__assign({}, base), require(babelConfig.value));
  366. }
  367. else {
  368. base = __assign(__assign({}, base), json5.parse(fs_1.readFileSync(babelConfig.value, 'utf8')));
  369. }
  370. }
  371. }
  372. else if (babelConfig.kind === 'inline') {
  373. base = __assign(__assign({}, base), babelConfig.value);
  374. }
  375. var config = ConfigSet.loadConfig(base);
  376. this.logger.debug({ babelConfig: config }, 'normalized babel config');
  377. return config;
  378. },
  379. enumerable: true,
  380. configurable: true
  381. });
  382. Object.defineProperty(ConfigSet.prototype, "compilerModule", {
  383. get: function () {
  384. return importer_1.importer.typescript(messages_1.ImportReasons.TsJest, this.tsJest.compiler);
  385. },
  386. enumerable: true,
  387. configurable: true
  388. });
  389. Object.defineProperty(ConfigSet.prototype, "babelJestTransformer", {
  390. get: function () {
  391. var babel = this.babel;
  392. if (!babel)
  393. return;
  394. this.logger.debug('creating babel-jest transformer');
  395. return importer_1.importer.babelJest(messages_1.ImportReasons.BabelJest).createTransformer(babel);
  396. },
  397. enumerable: true,
  398. configurable: true
  399. });
  400. Object.defineProperty(ConfigSet.prototype, "tsCompiler", {
  401. get: function () {
  402. return compiler_1.createCompiler(this);
  403. },
  404. enumerable: true,
  405. configurable: true
  406. });
  407. Object.defineProperty(ConfigSet.prototype, "astTransformers", {
  408. get: function () {
  409. return __spread(transformers_1.internals, this.tsJest.transformers.map(function (m) { return require(m); }));
  410. },
  411. enumerable: true,
  412. configurable: true
  413. });
  414. Object.defineProperty(ConfigSet.prototype, "tsCustomTransformers", {
  415. get: function () {
  416. var _this = this;
  417. return {
  418. before: this.astTransformers.map(function (t) { return t.factory(_this); }),
  419. };
  420. },
  421. enumerable: true,
  422. configurable: true
  423. });
  424. Object.defineProperty(ConfigSet.prototype, "hooks", {
  425. get: function () {
  426. var hooksFile = process.env.TS_JEST_HOOKS;
  427. if (hooksFile) {
  428. hooksFile = path_1.resolve(this.cwd, hooksFile);
  429. return importer_1.importer.tryTheseOr(hooksFile, {});
  430. }
  431. return {};
  432. },
  433. enumerable: true,
  434. configurable: true
  435. });
  436. Object.defineProperty(ConfigSet.prototype, "filterDiagnostics", {
  437. get: function () {
  438. var _a = this, ignoreCodes = _a.tsJest.diagnostics.ignoreCodes, shouldReportDiagnostic = _a.shouldReportDiagnostic;
  439. return function (diagnostics, filePath) {
  440. if (filePath && !shouldReportDiagnostic(filePath))
  441. return [];
  442. return diagnostics.filter(function (diagnostic) {
  443. if (diagnostic.file && diagnostic.file.fileName && !shouldReportDiagnostic(diagnostic.file.fileName)) {
  444. return false;
  445. }
  446. return ignoreCodes.indexOf(diagnostic.code) === -1;
  447. });
  448. };
  449. },
  450. enumerable: true,
  451. configurable: true
  452. });
  453. Object.defineProperty(ConfigSet.prototype, "shouldReportDiagnostic", {
  454. get: function () {
  455. var pathRegex = this.tsJest.diagnostics.pathRegex;
  456. if (pathRegex) {
  457. var regex_1 = new RegExp(pathRegex);
  458. return function (file) { return regex_1.test(file); };
  459. }
  460. else {
  461. return function () { return true; };
  462. }
  463. },
  464. enumerable: true,
  465. configurable: true
  466. });
  467. Object.defineProperty(ConfigSet.prototype, "shouldStringifyContent", {
  468. get: function () {
  469. var stringifyContentPathRegex = this.tsJest.stringifyContentPathRegex;
  470. if (stringifyContentPathRegex) {
  471. var regex_2 = new RegExp(stringifyContentPathRegex);
  472. return function (file) { return regex_2.test(file); };
  473. }
  474. else {
  475. return function () { return false; };
  476. }
  477. },
  478. enumerable: true,
  479. configurable: true
  480. });
  481. Object.defineProperty(ConfigSet.prototype, "createTsError", {
  482. get: function () {
  483. var _this = this;
  484. var pretty = this.tsJest.diagnostics.pretty;
  485. var formatDiagnostics = pretty
  486. ? this.compilerModule.formatDiagnosticsWithColorAndContext
  487. : this.compilerModule.formatDiagnostics;
  488. var diagnosticHost = {
  489. getNewLine: function () { return '\n'; },
  490. getCurrentDirectory: function () { return _this.cwd; },
  491. getCanonicalFileName: function (path) { return path; },
  492. };
  493. return function (diagnostics) {
  494. var diagnosticText = formatDiagnostics(diagnostics, diagnosticHost);
  495. var diagnosticCodes = diagnostics.map(function (x) { return x.code; });
  496. return new ts_error_1.TSError(diagnosticText, diagnosticCodes);
  497. };
  498. },
  499. enumerable: true,
  500. configurable: true
  501. });
  502. Object.defineProperty(ConfigSet.prototype, "tsCacheDir", {
  503. get: function () {
  504. if (!this.jest.cache) {
  505. logger.debug('file caching disabled');
  506. return;
  507. }
  508. var cacheSuffix = sha1_1.sha1(json_1.stringify({
  509. version: this.compilerModule.version,
  510. digest: this.tsJestDigest,
  511. dependencies: this.projectDependencies,
  512. compiler: this.tsJest.compiler,
  513. compilerOptions: this.typescript.options,
  514. isolatedModules: this.tsJest.isolatedModules,
  515. diagnostics: this.tsJest.diagnostics,
  516. }));
  517. var res = path_1.join(this.jest.cacheDirectory, 'ts-jest', cacheSuffix.substr(0, 2), cacheSuffix.substr(2));
  518. logger.debug({ cacheDirectory: res }, 'will use file caching');
  519. return res;
  520. },
  521. enumerable: true,
  522. configurable: true
  523. });
  524. Object.defineProperty(ConfigSet.prototype, "overriddenCompilerOptions", {
  525. get: function () {
  526. var options = {
  527. sourceMap: true,
  528. inlineSourceMap: false,
  529. inlineSources: true,
  530. declaration: false,
  531. noEmit: false,
  532. outDir: '$$ts-jest$$',
  533. removeComments: false,
  534. out: undefined,
  535. outFile: undefined,
  536. composite: undefined,
  537. declarationDir: undefined,
  538. declarationMap: undefined,
  539. emitDeclarationOnly: undefined,
  540. incremental: undefined,
  541. sourceRoot: undefined,
  542. tsBuildInfoFile: undefined,
  543. };
  544. if (!this.tsJest.babelConfig) {
  545. options.module = this.compilerModule.ModuleKind.CommonJS;
  546. }
  547. return options;
  548. },
  549. enumerable: true,
  550. configurable: true
  551. });
  552. Object.defineProperty(ConfigSet.prototype, "rootDir", {
  553. get: function () {
  554. return path_1.normalize(this.jest.rootDir || this.cwd);
  555. },
  556. enumerable: true,
  557. configurable: true
  558. });
  559. Object.defineProperty(ConfigSet.prototype, "cwd", {
  560. get: function () {
  561. return path_1.normalize(this.jest.cwd || process.cwd());
  562. },
  563. enumerable: true,
  564. configurable: true
  565. });
  566. Object.defineProperty(ConfigSet.prototype, "isDoctoring", {
  567. get: function () {
  568. return !!process.env.TS_JEST_DOCTOR;
  569. },
  570. enumerable: true,
  571. configurable: true
  572. });
  573. Object.defineProperty(ConfigSet.prototype, "tsJestDigest", {
  574. get: function () {
  575. return __1.digest;
  576. },
  577. enumerable: true,
  578. configurable: true
  579. });
  580. Object.defineProperty(ConfigSet.prototype, "jsonValue", {
  581. get: function () {
  582. var jest = __assign({}, this.jest);
  583. var globals = (jest.globals = __assign({}, jest.globals));
  584. delete jest.name;
  585. delete jest.cacheDirectory;
  586. delete globals['ts-jest'];
  587. return new jsonable_value_1.JsonableValue({
  588. versions: this.versions,
  589. projectDepVersions: this.projectDependencies,
  590. digest: this.tsJestDigest,
  591. transformers: this.astTransformers.map(function (t) { return t.name + "@" + t.version; }),
  592. jest: jest,
  593. tsJest: this.tsJest,
  594. babel: this.babel,
  595. tsconfig: this.typescript.options,
  596. });
  597. },
  598. enumerable: true,
  599. configurable: true
  600. });
  601. Object.defineProperty(ConfigSet.prototype, "cacheKey", {
  602. get: function () {
  603. return this.jsonValue.serialized;
  604. },
  605. enumerable: true,
  606. configurable: true
  607. });
  608. ConfigSet.prototype.makeDiagnostic = function (code, messageText, options) {
  609. if (options === void 0) { options = {}; }
  610. var _a = options.category, category = _a === void 0 ? this.compilerModule.DiagnosticCategory.Warning : _a, file = options.file, start = options.start, length = options.length;
  611. return {
  612. code: code,
  613. messageText: messageText,
  614. category: category,
  615. file: file,
  616. start: start,
  617. length: length,
  618. };
  619. };
  620. ConfigSet.prototype.readTsConfig = function (compilerOptions, resolvedConfigFile, noProject) {
  621. var e_2, _a;
  622. var config = { compilerOptions: {} };
  623. var basePath = normalize_slashes_1.normalizeSlashes(this.rootDir);
  624. var configFileName;
  625. var ts = this.compilerModule;
  626. var input;
  627. if (noProject) {
  628. input = { compilerOptions: __assign({}, compilerOptions) };
  629. }
  630. else {
  631. configFileName = resolvedConfigFile
  632. ? normalize_slashes_1.normalizeSlashes(resolvedConfigFile)
  633. : ts.findConfigFile(normalize_slashes_1.normalizeSlashes(this.rootDir), ts.sys.fileExists);
  634. if (configFileName) {
  635. this.logger.debug({ tsConfigFileName: configFileName }, 'readTsConfig(): reading', configFileName);
  636. var result_1 = ts.readConfigFile(configFileName, ts.sys.readFile);
  637. if (result_1.error) {
  638. return {
  639. resolved: { errors: [result_1.error], fileNames: [], options: {} },
  640. };
  641. }
  642. config = result_1.config;
  643. input = __assign(__assign({}, result_1.config), { compilerOptions: __assign(__assign({}, (result_1.config && result_1.config.compilerOptions)), compilerOptions) });
  644. basePath = normalize_slashes_1.normalizeSlashes(path_1.dirname(configFileName));
  645. }
  646. }
  647. config.compilerOptions = __assign(__assign({}, config.compilerOptions), compilerOptions);
  648. var result = ts.parseJsonConfigFileContent(config, ts.sys, basePath, undefined, configFileName);
  649. var forcedOptions = this.overriddenCompilerOptions;
  650. var finalOptions = result.options;
  651. if (finalOptions.target === undefined) {
  652. finalOptions.target = ts.ScriptTarget.ES5;
  653. }
  654. var target = finalOptions.target;
  655. var defaultModule = [ts.ScriptTarget.ES3, ts.ScriptTarget.ES5].includes(target)
  656. ? ts.ModuleKind.CommonJS
  657. : ts.ModuleKind.ESNext;
  658. var moduleValue = finalOptions.module == null ? defaultModule : finalOptions.module;
  659. if ('module' in forcedOptions &&
  660. moduleValue !== forcedOptions.module &&
  661. !(finalOptions.esModuleInterop || finalOptions.allowSyntheticDefaultImports)) {
  662. result.errors.push(this.makeDiagnostic(DiagnosticCodes.ConfigModuleOption, messages_1.Errors.ConfigNoModuleInterop, {
  663. category: ts.DiagnosticCategory.Message,
  664. }));
  665. if (!('allowSyntheticDefaultImports' in config.compilerOptions)) {
  666. finalOptions.allowSyntheticDefaultImports = true;
  667. }
  668. }
  669. try {
  670. for (var _b = __values(Object.keys(forcedOptions)), _c = _b.next(); !_c.done; _c = _b.next()) {
  671. var key = _c.value;
  672. var val = forcedOptions[key];
  673. if (val === undefined) {
  674. delete finalOptions[key];
  675. }
  676. else {
  677. finalOptions[key] = val;
  678. }
  679. }
  680. }
  681. catch (e_2_1) { e_2 = { error: e_2_1 }; }
  682. finally {
  683. try {
  684. if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
  685. }
  686. finally { if (e_2) throw e_2.error; }
  687. }
  688. return { input: input, resolved: result };
  689. };
  690. ConfigSet.prototype.resolvePath = function (inputPath, _a) {
  691. var _b = _a === void 0 ? {} : _a, _c = _b.throwIfMissing, throwIfMissing = _c === void 0 ? true : _c, _d = _b.nodeResolve, nodeResolve = _d === void 0 ? false : _d;
  692. var path = inputPath;
  693. var nodeResolved = false;
  694. if (path.startsWith('<rootDir>')) {
  695. path = path_1.resolve(path_1.join(this.rootDir, path.substr(9)));
  696. }
  697. else if (!path_1.isAbsolute(path)) {
  698. if (!path.startsWith('.') && nodeResolve) {
  699. try {
  700. path = require.resolve(path);
  701. nodeResolved = true;
  702. }
  703. catch (_) { }
  704. }
  705. if (!nodeResolved) {
  706. path = path_1.resolve(this.cwd, path);
  707. }
  708. }
  709. if (!nodeResolved && nodeResolve) {
  710. try {
  711. path = require.resolve(path);
  712. nodeResolved = true;
  713. }
  714. catch (_) { }
  715. }
  716. if (throwIfMissing && !fs_1.existsSync(path)) {
  717. throw new Error(messages_1.interpolate(messages_1.Errors.FileNotFound, { inputPath: inputPath, resolvedPath: path }));
  718. }
  719. this.logger.debug({ fromPath: inputPath, toPath: path }, 'resolved path from', inputPath, 'to', path);
  720. return path;
  721. };
  722. ConfigSet.prototype.toJSON = function () {
  723. return this.jsonValue.value;
  724. };
  725. __decorate([
  726. memoize_1.Memoize()
  727. ], ConfigSet.prototype, "projectPackageJson", null);
  728. __decorate([
  729. memoize_1.Memoize()
  730. ], ConfigSet.prototype, "projectDependencies", null);
  731. __decorate([
  732. memoize_1.Memoize()
  733. ], ConfigSet.prototype, "jest", null);
  734. __decorate([
  735. memoize_1.Memoize()
  736. ], ConfigSet.prototype, "tsJest", null);
  737. __decorate([
  738. memoize_1.Memoize()
  739. ], ConfigSet.prototype, "versions", null);
  740. __decorate([
  741. memoize_1.Memoize()
  742. ], ConfigSet.prototype, "_typescript", null);
  743. __decorate([
  744. memoize_1.Memoize()
  745. ], ConfigSet.prototype, "raiseDiagnostics", null);
  746. __decorate([
  747. memoize_1.Memoize()
  748. ], ConfigSet.prototype, "babel", null);
  749. __decorate([
  750. memoize_1.Memoize()
  751. ], ConfigSet.prototype, "compilerModule", null);
  752. __decorate([
  753. memoize_1.Memoize()
  754. ], ConfigSet.prototype, "babelJestTransformer", null);
  755. __decorate([
  756. memoize_1.Memoize()
  757. ], ConfigSet.prototype, "tsCompiler", null);
  758. __decorate([
  759. memoize_1.Memoize()
  760. ], ConfigSet.prototype, "astTransformers", null);
  761. __decorate([
  762. memoize_1.Memoize()
  763. ], ConfigSet.prototype, "tsCustomTransformers", null);
  764. __decorate([
  765. memoize_1.Memoize()
  766. ], ConfigSet.prototype, "hooks", null);
  767. __decorate([
  768. memoize_1.Memoize()
  769. ], ConfigSet.prototype, "filterDiagnostics", null);
  770. __decorate([
  771. memoize_1.Memoize()
  772. ], ConfigSet.prototype, "shouldReportDiagnostic", null);
  773. __decorate([
  774. memoize_1.Memoize()
  775. ], ConfigSet.prototype, "shouldStringifyContent", null);
  776. __decorate([
  777. memoize_1.Memoize()
  778. ], ConfigSet.prototype, "createTsError", null);
  779. __decorate([
  780. memoize_1.Memoize()
  781. ], ConfigSet.prototype, "tsCacheDir", null);
  782. __decorate([
  783. memoize_1.Memoize()
  784. ], ConfigSet.prototype, "overriddenCompilerOptions", null);
  785. __decorate([
  786. memoize_1.Memoize()
  787. ], ConfigSet.prototype, "rootDir", null);
  788. __decorate([
  789. memoize_1.Memoize()
  790. ], ConfigSet.prototype, "cwd", null);
  791. __decorate([
  792. memoize_1.Memoize()
  793. ], ConfigSet.prototype, "tsJestDigest", null);
  794. __decorate([
  795. memoize_1.Memoize()
  796. ], ConfigSet.prototype, "jsonValue", null);
  797. return ConfigSet;
  798. }());
  799. exports.ConfigSet = ConfigSet;