ts-jest 29.3.2 → 29.3.4

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 CHANGED
@@ -1 +1 @@
1
- f23c7d9044c4051d802f7e95e657393a9fc3c810
1
+ dfc4e7710788a7a7f79e90e8d15aee27450846bd
package/CHANGELOG.md CHANGED
@@ -1,3 +1,24 @@
1
+ ## [29.3.4](https://github.com/kulshekhar/ts-jest/compare/v29.3.3...v29.3.4) (2025-05-16)
2
+
3
+
4
+ ### Bug Fixes
5
+
6
+ * fix: fix `TsJestTransformerOptions` type ([3b11e29](https://github.com/kulshekhar/ts-jest/commit/3b11e29)), closes [#4247](https://github.com/kulshekhar/ts-jest/issues/4247)
7
+ * fix(cli): fix wrong path for preset creator fns ([249eb2c](https://github.com/kulshekhar/ts-jest/commit/249eb2c))
8
+ * fix(config): disable `rewriteRelativeImportExtensions` always ([9b1f472](https://github.com/kulshekhar/ts-jest/commit/9b1f472)), closes [#4855](https://github.com/kulshekhar/ts-jest/issues/4855)
9
+
10
+
11
+
12
+ ## [29.3.3](https://github.com/kulshekhar/ts-jest/compare/v29.3.2...v29.3.3) (2025-05-14)
13
+
14
+
15
+ ### Bug Fixes
16
+
17
+ * fix(cli): init config with preset creator functions ([cdd3039](https://github.com/kulshekhar/ts-jest/commit/cdd3039)), closes [#4840](https://github.com/kulshekhar/ts-jest/issues/4840)
18
+ * fix(config): disable `isolatedDeclarations` ([5d6b35f](https://github.com/kulshekhar/ts-jest/commit/5d6b35f)), closes [#4847](https://github.com/kulshekhar/ts-jest/issues/4847)
19
+
20
+
21
+
1
22
  ## [29.3.2](https://github.com/kulshekhar/ts-jest/compare/v29.3.1...v29.3.2) (2025-04-12)
2
23
 
3
24
 
@@ -4,81 +4,28 @@
4
4
  * ...and I saw a merged PR with `jest --init` tool!
5
5
  * TODO: see what's the best path for this
6
6
  */
7
- var __assign = (this && this.__assign) || function () {
8
- __assign = Object.assign || function(t) {
9
- for (var s, i = 1, n = arguments.length; i < n; i++) {
10
- s = arguments[i];
11
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
12
- t[p] = s[p];
13
- }
14
- return t;
15
- };
16
- return __assign.apply(this, arguments);
17
- };
18
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
19
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
20
- return new (P || (P = Promise))(function (resolve, reject) {
21
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
22
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
23
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
24
- step((generator = generator.apply(thisArg, _arguments || [])).next());
25
- });
26
- };
27
- var __generator = (this && this.__generator) || function (thisArg, body) {
28
- var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
29
- return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
30
- function verb(n) { return function (v) { return step([n, v]); }; }
31
- function step(op) {
32
- if (f) throw new TypeError("Generator is already executing.");
33
- while (g && (g = 0, op[0] && (_ = 0)), _) try {
34
- 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;
35
- if (y = 0, t) op = [op[0] & 2, t.value];
36
- switch (op[0]) {
37
- case 0: case 1: t = op; break;
38
- case 4: _.label++; return { value: op[1], done: false };
39
- case 5: _.label++; y = op[1]; op = [0]; continue;
40
- case 7: op = _.ops.pop(); _.trys.pop(); continue;
41
- default:
42
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
43
- if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
44
- if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
45
- if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
46
- if (t[2]) _.ops.pop();
47
- _.trys.pop(); continue;
48
- }
49
- op = body.call(thisArg, _);
50
- } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
51
- if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
52
- }
53
- };
54
- var __read = (this && this.__read) || function (o, n) {
55
- var m = typeof Symbol === "function" && o[Symbol.iterator];
56
- if (!m) return o;
57
- var i = m.call(o), r, ar = [], e;
58
- try {
59
- while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
60
- }
61
- catch (error) { e = { error: error }; }
62
- finally {
63
- try {
64
- if (r && !r.done && (m = i["return"])) m.call(i);
65
- }
66
- finally { if (e) throw e.error; }
67
- }
68
- return ar;
69
- };
70
7
  var __importDefault = (this && this.__importDefault) || function (mod) {
71
8
  return (mod && mod.__esModule) ? mod : { "default": mod };
72
9
  };
73
10
  Object.defineProperty(exports, "__esModule", { value: true });
74
11
  exports.help = exports.run = void 0;
75
- var fs_1 = require("fs");
76
- var path_1 = require("path");
77
- var ejs_1 = __importDefault(require("ejs"));
78
- var json5_1 = require("json5");
79
- var constants_1 = require("../../constants");
80
- var create_jest_preset_1 = require("../../presets/create-jest-preset");
81
- var ensureOnlyUsingDoubleQuotes = function (str) {
12
+ const fs_1 = require("fs");
13
+ const path_1 = require("path");
14
+ const ejs_1 = __importDefault(require("ejs"));
15
+ const json5_1 = require("json5");
16
+ const create_jest_preset_1 = require("../../presets/create-jest-preset");
17
+ const JEST_CONFIG_EJS_TEMPLATE = `const { <%= presetCreatorFn %> } = require("ts-jest");
18
+
19
+ const tsJestTransformCfg = <%= presetCreatorFn %>(<%- transformOpts %>).transform;
20
+
21
+ /** @type {import("jest").Config} **/
22
+ <%= exportKind %> {
23
+ testEnvironment: "<%= testEnvironment %>",
24
+ transform: {
25
+ ...tsJestTransformCfg,
26
+ },
27
+ };`;
28
+ const ensureOnlyUsingDoubleQuotes = (str) => {
82
29
  return str
83
30
  .replace(/"'(.*?)'"/g, '"$1"')
84
31
  .replace(/'ts-jest'/g, '"ts-jest"')
@@ -87,77 +34,105 @@ var ensureOnlyUsingDoubleQuotes = function (str) {
87
34
  /**
88
35
  * @internal
89
36
  */
90
- var run = function (args /* , logger: Logger */) { return __awaiter(void 0, void 0, void 0, function () {
91
- var askedTsconfig, force, jsdom, jsFilesProcessor, shouldPostProcessWithBabel, file, filePath, name, isPackageJsonConfig, isJestConfigFileExisted, pkgFile, isPackageJsonExisted, tsconfig, pkgJsonContent, body, resolvedTsconfigOption, transformConfig, _a, transformPattern, transformValue;
92
- var _b, _c;
93
- return __generator(this, function (_d) {
94
- askedTsconfig = args.tsconfig, force = args.force, jsdom = args.jsdom, jsFilesProcessor = args.js, shouldPostProcessWithBabel = args.babel;
95
- file = (_c = (_b = args._[0]) === null || _b === void 0 ? void 0 : _b.toString()) !== null && _c !== void 0 ? _c : 'jest.config.js';
96
- filePath = (0, path_1.join)(process.cwd(), file);
97
- name = (0, path_1.basename)(file);
98
- isPackageJsonConfig = name === 'package.json';
99
- isJestConfigFileExisted = (0, fs_1.existsSync)(filePath);
100
- pkgFile = isPackageJsonConfig ? filePath : (0, path_1.join)(process.cwd(), 'package.json');
101
- isPackageJsonExisted = isPackageJsonConfig || (0, fs_1.existsSync)(pkgFile);
102
- tsconfig = askedTsconfig === 'tsconfig.json' ? undefined : askedTsconfig;
103
- pkgJsonContent = isPackageJsonExisted ? JSON.parse((0, fs_1.readFileSync)(pkgFile, 'utf8')) : {};
104
- if (shouldPostProcessWithBabel) {
105
- console.warn("The option --babel is deprecated and will be removed in the next major version." +
106
- " Please specify 'js' option value (see more with npx ts-jest help) if you wish 'ts-jest' to process 'js' with TypeScript API or Babel.");
107
- }
108
- if (isPackageJsonConfig && !isJestConfigFileExisted) {
109
- throw new Error("File ".concat(file, " does not exists."));
110
- }
111
- else if (!isPackageJsonConfig && isJestConfigFileExisted && !force) {
112
- throw new Error("Configuration file ".concat(file, " already exists."));
113
- }
114
- if (!isPackageJsonConfig && !name.endsWith('.js')) {
115
- throw new TypeError("Configuration file ".concat(file, " must be a .js file or the package.json."));
37
+ const run = async (args /* , logger: Logger */) => {
38
+ const { tsconfig: askedTsconfig, force, jsdom, js: jsFilesProcessor, babel: shouldPostProcessWithBabel } = args;
39
+ const file = args._[0]?.toString() ?? 'jest.config.js';
40
+ const filePath = (0, path_1.join)(process.cwd(), file);
41
+ const name = (0, path_1.basename)(file);
42
+ const isPackageJsonConfig = name === 'package.json';
43
+ const isJestConfigFileExisted = (0, fs_1.existsSync)(filePath);
44
+ const pkgFile = isPackageJsonConfig ? filePath : (0, path_1.join)(process.cwd(), 'package.json');
45
+ const isPackageJsonExisted = isPackageJsonConfig || (0, fs_1.existsSync)(pkgFile);
46
+ const tsconfig = askedTsconfig === 'tsconfig.json' ? undefined : askedTsconfig;
47
+ const pkgJsonContent = isPackageJsonExisted ? JSON.parse((0, fs_1.readFileSync)(pkgFile, 'utf8')) : {};
48
+ if (shouldPostProcessWithBabel) {
49
+ console.warn(`The option --babel is deprecated and will be removed in the next major version.` +
50
+ ` Please specify 'js' option value (see more with npx ts-jest help) if you wish 'ts-jest' to process 'js' with TypeScript API or Babel.`);
51
+ }
52
+ if (isPackageJsonConfig && !isJestConfigFileExisted) {
53
+ throw new Error(`File ${file} does not exists.`);
54
+ }
55
+ else if (!isPackageJsonConfig && isJestConfigFileExisted && !force) {
56
+ throw new Error(`Configuration file ${file} already exists.`);
57
+ }
58
+ if (!isPackageJsonConfig && !name.endsWith('.js')) {
59
+ throw new TypeError(`Configuration file ${file} must be a .js file or the package.json.`);
60
+ }
61
+ if (isPackageJsonExisted && pkgJsonContent.jest) {
62
+ if (force && !isPackageJsonConfig) {
63
+ delete pkgJsonContent.jest;
64
+ (0, fs_1.writeFileSync)(pkgFile, JSON.stringify(pkgJsonContent, undefined, ' '));
116
65
  }
117
- if (isPackageJsonExisted && pkgJsonContent.jest) {
118
- if (force && !isPackageJsonConfig) {
119
- delete pkgJsonContent.jest;
120
- (0, fs_1.writeFileSync)(pkgFile, JSON.stringify(pkgJsonContent, undefined, ' '));
121
- }
122
- else if (!force) {
123
- throw new Error("A Jest configuration is already set in ".concat(pkgFile, "."));
124
- }
66
+ else if (!force) {
67
+ throw new Error(`A Jest configuration is already set in ${pkgFile}.`);
125
68
  }
126
- resolvedTsconfigOption = tsconfig ? { tsconfig: "".concat((0, json5_1.stringify)(tsconfig)) } : undefined;
69
+ }
70
+ let body;
71
+ const transformOpts = tsconfig
72
+ ? { tsconfig: `${(0, json5_1.stringify)(tsconfig)}` }
73
+ : undefined;
74
+ let transformConfig;
75
+ if (isPackageJsonConfig) {
127
76
  if (jsFilesProcessor === 'babel' || shouldPostProcessWithBabel) {
128
- transformConfig = (0, create_jest_preset_1.createJsWithBabelPreset)(resolvedTsconfigOption);
77
+ transformConfig = (0, create_jest_preset_1.createJsWithBabelPreset)(transformOpts);
129
78
  }
130
79
  else if (jsFilesProcessor === 'ts') {
131
- transformConfig = (0, create_jest_preset_1.createJsWithTsPreset)(resolvedTsconfigOption);
80
+ transformConfig = (0, create_jest_preset_1.createJsWithTsPreset)(transformOpts);
132
81
  }
133
82
  else {
134
- transformConfig = (0, create_jest_preset_1.createDefaultPreset)(resolvedTsconfigOption);
83
+ transformConfig = (0, create_jest_preset_1.createDefaultPreset)(transformOpts);
84
+ }
85
+ body = ensureOnlyUsingDoubleQuotes(JSON.stringify({
86
+ ...pkgJsonContent,
87
+ jest: transformConfig,
88
+ }, undefined, ' '));
89
+ }
90
+ else {
91
+ let presetCreatorFn;
92
+ if (jsFilesProcessor === 'babel' || shouldPostProcessWithBabel) {
93
+ presetCreatorFn = 'createJsWithBabelPreset';
135
94
  }
136
- if (isPackageJsonConfig) {
137
- body = ensureOnlyUsingDoubleQuotes(JSON.stringify(__assign(__assign({}, pkgJsonContent), { jest: transformConfig }), undefined, ' '));
95
+ else if (jsFilesProcessor === 'ts') {
96
+ presetCreatorFn = 'createJsWithTsPreset';
138
97
  }
139
98
  else {
140
- _a = __read(Object.entries(transformConfig.transform)[0], 2), transformPattern = _a[0], transformValue = _a[1];
141
- body = ejs_1.default.render(constants_1.JEST_CONFIG_EJS_TEMPLATE, {
142
- exportKind: pkgJsonContent.type === 'module' ? 'export default' : 'module.exports =',
143
- testEnvironment: jsdom ? 'jsdom' : 'node',
144
- transformPattern: transformPattern,
145
- transformValue: ensureOnlyUsingDoubleQuotes((0, json5_1.stringify)(transformValue)),
146
- });
99
+ presetCreatorFn = 'createDefaultPreset';
147
100
  }
148
- (0, fs_1.writeFileSync)(filePath, body);
149
- process.stderr.write("\nJest configuration written to \"".concat(filePath, "\".\n"));
150
- return [2 /*return*/];
151
- });
152
- }); };
101
+ body = ejs_1.default.render(JEST_CONFIG_EJS_TEMPLATE, {
102
+ exportKind: pkgJsonContent.type === 'module' ? 'export default' : 'module.exports =',
103
+ testEnvironment: jsdom ? 'jsdom' : 'node',
104
+ presetCreatorFn,
105
+ transformOpts: transformOpts ? ensureOnlyUsingDoubleQuotes(JSON.stringify(transformOpts, null, 2)) : undefined,
106
+ });
107
+ }
108
+ (0, fs_1.writeFileSync)(filePath, body);
109
+ process.stderr.write(`
110
+ Jest configuration written to "${filePath}".
111
+ `);
112
+ };
153
113
  exports.run = run;
154
114
  /**
155
115
  * @internal
156
116
  */
157
- var help = function () { return __awaiter(void 0, void 0, void 0, function () {
158
- return __generator(this, function (_a) {
159
- process.stdout.write("\nUsage:\n ts-jest config:init [options] [<config-file>]\n\nArguments:\n <config-file> Can be a js or json Jest config file. If it is a\n package.json file, the configuration will be read from\n the \"jest\" property.\n Default: jest.config.js\n\nOptions:\n --force Discard any existing Jest config\n --js ts|babel Process '.js' files with ts-jest if 'ts' or with\n babel-jest if 'babel'\n --no-jest-preset Disable the use of Jest presets\n --tsconfig <file> Path to the tsconfig.json file\n --babel Enable using Babel to process 'js' resulted content from 'ts-jest' processing\n --jsdom Use 'jsdom' as test environment instead of 'node'\n");
160
- return [2 /*return*/];
161
- });
162
- }); };
117
+ const help = async () => {
118
+ process.stdout.write(`
119
+ Usage:
120
+ ts-jest config:init [options] [<config-file>]
121
+
122
+ Arguments:
123
+ <config-file> Can be a js or json Jest config file. If it is a
124
+ package.json file, the configuration will be read from
125
+ the "jest" property.
126
+ Default: jest.config.js
127
+
128
+ Options:
129
+ --force Discard any existing Jest config
130
+ --js ts|babel Process '.js' files with ts-jest if 'ts' or with
131
+ babel-jest if 'babel'
132
+ --no-jest-preset Disable the use of Jest presets
133
+ --tsconfig <file> Path to the tsconfig.json file
134
+ --babel Enable using Babel to process 'js' resulted content from 'ts-jest' processing
135
+ --jsdom Use 'jsdom' as test environment instead of 'node'
136
+ `);
137
+ };
163
138
  exports.help = help;
@@ -1,181 +1,128 @@
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
2
  var __importDefault = (this && this.__importDefault) || function (mod) {
66
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
67
4
  };
68
5
  Object.defineProperty(exports, "__esModule", { value: true });
69
6
  exports.help = exports.run = void 0;
70
- var fs_1 = require("fs");
71
- var path_1 = require("path");
72
- var bs_logger_1 = require("bs-logger");
73
- var fast_json_stable_stringify_1 = __importDefault(require("fast-json-stable-stringify"));
74
- var json5_1 = require("json5");
75
- var create_jest_preset_1 = require("../../presets/create-jest-preset");
76
- var backports_1 = require("../../utils/backports");
77
- var presets_1 = require("../helpers/presets");
78
- var migrateGlobalConfigToTransformConfig = function (transformConfig, globalsTsJestConfig) {
7
+ const fs_1 = require("fs");
8
+ const path_1 = require("path");
9
+ const bs_logger_1 = require("bs-logger");
10
+ const fast_json_stable_stringify_1 = __importDefault(require("fast-json-stable-stringify"));
11
+ const json5_1 = require("json5");
12
+ const create_jest_preset_1 = require("../../presets/create-jest-preset");
13
+ const backports_1 = require("../../utils/backports");
14
+ const presets_1 = require("../helpers/presets");
15
+ const migrateGlobalConfigToTransformConfig = (transformConfig, globalsTsJestConfig) => {
79
16
  if (transformConfig) {
80
- return Object.entries(transformConfig).reduce(function (previousValue, currentValue) {
81
- var _a, _b;
82
- var _c = __read(currentValue, 2), key = _c[0], transformOptions = _c[1];
17
+ return Object.entries(transformConfig).reduce((previousValue, currentValue) => {
18
+ const [key, transformOptions] = currentValue;
83
19
  if (typeof transformOptions === 'string' && transformOptions.includes('ts-jest')) {
84
- return __assign(__assign({}, previousValue), (_a = {}, _a[key] = globalsTsJestConfig ? ['ts-jest', globalsTsJestConfig] : 'ts-jest', _a));
20
+ return {
21
+ ...previousValue,
22
+ [key]: globalsTsJestConfig ? ['ts-jest', globalsTsJestConfig] : 'ts-jest',
23
+ };
85
24
  }
86
- return __assign(__assign({}, previousValue), (_b = {}, _b[key] = transformOptions, _b));
25
+ return {
26
+ ...previousValue,
27
+ [key]: transformOptions,
28
+ };
87
29
  }, {});
88
30
  }
89
31
  return {};
90
32
  };
91
- var migratePresetToTransformConfig = function (transformConfig, preset, globalsTsJestConfig) {
33
+ const migratePresetToTransformConfig = (transformConfig, preset, globalsTsJestConfig) => {
92
34
  if (preset) {
93
- var transformConfigFromPreset = preset.name === "ts-jest/presets/js-with-ts" /* JestPresetNames.jsWithTs */
35
+ const transformConfigFromPreset = preset.name === "ts-jest/presets/js-with-ts" /* JestPresetNames.jsWithTs */
94
36
  ? (0, create_jest_preset_1.createJsWithTsPreset)(globalsTsJestConfig)
95
37
  : preset.name === "ts-jest/presets/js-with-babel" /* JestPresetNames.jsWIthBabel */
96
38
  ? (0, create_jest_preset_1.createJsWithBabelPreset)(globalsTsJestConfig)
97
39
  : (0, create_jest_preset_1.createDefaultPreset)(globalsTsJestConfig);
98
- return __assign(__assign({}, transformConfig), transformConfigFromPreset.transform);
40
+ return {
41
+ ...transformConfig,
42
+ ...transformConfigFromPreset.transform,
43
+ };
99
44
  }
100
45
  return transformConfig;
101
46
  };
102
47
  /**
103
48
  * @internal
104
49
  */
105
- var run = function (args /* , logger: Logger*/) { return __awaiter(void 0, void 0, void 0, function () {
106
- var nullLogger, file, filePath, name, isPackage, actualConfig, migratedConfig, preset, presetValue, migratedValue, presetValue, migratedValue, globalsTsJestConfig, before, after, stringify, prefix;
107
- var _a, _b, _c, _d, _e, _f, _g, _h;
108
- return __generator(this, function (_j) {
109
- nullLogger = (0, bs_logger_1.createLogger)({ targets: [] });
110
- file = (_a = args._[0]) === null || _a === void 0 ? void 0 : _a.toString();
111
- filePath = (0, path_1.resolve)(process.cwd(), file);
112
- if (!(0, fs_1.existsSync)(filePath)) {
113
- throw new Error("Configuration file ".concat(file, " does not exists."));
114
- }
115
- name = (0, path_1.basename)(file);
116
- isPackage = name === 'package.json';
117
- if (!/\.(js|json)$/.test(name)) {
118
- throw new TypeError("Configuration file ".concat(file, " must be a JavaScript or JSON file."));
119
- }
120
- actualConfig = require(filePath);
121
- if (isPackage) {
122
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
123
- actualConfig = actualConfig.jest;
124
- }
125
- if (!actualConfig)
126
- actualConfig = {};
127
- migratedConfig = (0, backports_1.backportJestConfig)(nullLogger, actualConfig);
128
- if (migratedConfig.preset) {
129
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
130
- preset = (_b = presets_1.allPresets[migratedConfig.preset]) !== null && _b !== void 0 ? _b : presets_1.allPresets["ts-jest/presets/default" /* JestPresetNames.default */];
50
+ const run = async (args /* , logger: Logger*/) => {
51
+ const nullLogger = (0, bs_logger_1.createLogger)({ targets: [] });
52
+ const file = args._[0]?.toString();
53
+ const filePath = (0, path_1.resolve)(process.cwd(), file);
54
+ if (!(0, fs_1.existsSync)(filePath)) {
55
+ throw new Error(`Configuration file ${file} does not exists.`);
56
+ }
57
+ const name = (0, path_1.basename)(file);
58
+ const isPackage = name === 'package.json';
59
+ if (!/\.(js|json)$/.test(name)) {
60
+ throw new TypeError(`Configuration file ${file} must be a JavaScript or JSON file.`);
61
+ }
62
+ let actualConfig = require(filePath);
63
+ if (isPackage) {
64
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
65
+ actualConfig = actualConfig.jest;
66
+ }
67
+ if (!actualConfig)
68
+ actualConfig = {};
69
+ // migrate
70
+ // first we backport our options
71
+ const migratedConfig = (0, backports_1.backportJestConfig)(nullLogger, actualConfig);
72
+ let preset;
73
+ if (migratedConfig.preset) {
74
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
75
+ preset = presets_1.allPresets[migratedConfig.preset] ?? presets_1.allPresets["ts-jest/presets/default" /* JestPresetNames.default */];
76
+ }
77
+ else {
78
+ if (args.js) {
79
+ preset = args.js === 'babel' ? presets_1.allPresets["ts-jest/presets/js-with-babel" /* JestPresetNames.jsWIthBabel */] : presets_1.allPresets["ts-jest/presets/js-with-ts" /* JestPresetNames.jsWithTs */];
131
80
  }
132
81
  else {
133
- if (args.js) {
134
- preset = args.js === 'babel' ? presets_1.allPresets["ts-jest/presets/js-with-babel" /* JestPresetNames.jsWIthBabel */] : presets_1.allPresets["ts-jest/presets/js-with-ts" /* JestPresetNames.jsWithTs */];
135
- }
136
- else {
137
- preset = presets_1.allPresets["ts-jest/presets/default" /* JestPresetNames.default */];
138
- }
82
+ preset = presets_1.allPresets["ts-jest/presets/default" /* JestPresetNames.default */];
139
83
  }
140
- // check the extensions
141
- if (((_c = migratedConfig.moduleFileExtensions) === null || _c === void 0 ? void 0 : _c.length) && preset) {
142
- presetValue = dedupSort((_d = preset.value.moduleFileExtensions) !== null && _d !== void 0 ? _d : []).join('::');
143
- migratedValue = dedupSort(migratedConfig.moduleFileExtensions).join('::');
144
- if (presetValue === migratedValue) {
145
- delete migratedConfig.moduleFileExtensions;
146
- }
84
+ }
85
+ // check the extensions
86
+ if (migratedConfig.moduleFileExtensions?.length && preset) {
87
+ const presetValue = dedupSort(preset.value.moduleFileExtensions ?? []).join('::');
88
+ const migratedValue = dedupSort(migratedConfig.moduleFileExtensions).join('::');
89
+ if (presetValue === migratedValue) {
90
+ delete migratedConfig.moduleFileExtensions;
147
91
  }
148
- // there is a testRegex, remove our testMatch
149
- if (typeof migratedConfig.testRegex === 'string' || ((_e = migratedConfig.testRegex) === null || _e === void 0 ? void 0 : _e.length)) {
92
+ }
93
+ // there is a testRegex, remove our testMatch
94
+ if (typeof migratedConfig.testRegex === 'string' || migratedConfig.testRegex?.length) {
95
+ delete migratedConfig.testMatch;
96
+ }
97
+ // check the testMatch
98
+ else if (migratedConfig.testMatch?.length && preset) {
99
+ const presetValue = dedupSort(preset.value.testMatch ?? []).join('::');
100
+ const migratedValue = dedupSort(migratedConfig.testMatch).join('::');
101
+ if (presetValue === migratedValue) {
150
102
  delete migratedConfig.testMatch;
151
103
  }
152
- // check the testMatch
153
- else if (((_f = migratedConfig.testMatch) === null || _f === void 0 ? void 0 : _f.length) && preset) {
154
- presetValue = dedupSort((_g = preset.value.testMatch) !== null && _g !== void 0 ? _g : []).join('::');
155
- migratedValue = dedupSort(migratedConfig.testMatch).join('::');
156
- if (presetValue === migratedValue) {
157
- delete migratedConfig.testMatch;
158
- }
159
- }
160
- globalsTsJestConfig = (_h = migratedConfig.globals) === null || _h === void 0 ? void 0 : _h['ts-jest'];
161
- migratedConfig.transform = migrateGlobalConfigToTransformConfig(migratedConfig.transform, globalsTsJestConfig);
162
- migratedConfig.transform = migratePresetToTransformConfig(migratedConfig.transform, preset, globalsTsJestConfig);
163
- cleanupConfig(actualConfig);
164
- cleanupConfig(migratedConfig);
165
- before = (0, fast_json_stable_stringify_1.default)(actualConfig);
166
- after = (0, fast_json_stable_stringify_1.default)(migratedConfig);
167
- if (after === before) {
168
- process.stderr.write("\nNo migration needed for given Jest configuration\n ");
169
- return [2 /*return*/];
170
- }
171
- stringify = file.endsWith('.json') ? JSON.stringify : json5_1.stringify;
172
- prefix = file.endsWith('.json') ? '"jest": ' : 'module.exports = ';
173
- // output new config
174
- process.stderr.write("\nMigrated Jest configuration:\n");
175
- process.stdout.write("".concat(prefix).concat(stringify(migratedConfig, undefined, ' '), "\n"));
176
- return [2 /*return*/];
177
- });
178
- }); };
104
+ }
105
+ const globalsTsJestConfig = migratedConfig.globals?.['ts-jest'];
106
+ migratedConfig.transform = migrateGlobalConfigToTransformConfig(migratedConfig.transform, globalsTsJestConfig);
107
+ migratedConfig.transform = migratePresetToTransformConfig(migratedConfig.transform, preset, globalsTsJestConfig);
108
+ cleanupConfig(actualConfig);
109
+ cleanupConfig(migratedConfig);
110
+ const before = (0, fast_json_stable_stringify_1.default)(actualConfig);
111
+ const after = (0, fast_json_stable_stringify_1.default)(migratedConfig);
112
+ if (after === before) {
113
+ process.stderr.write(`
114
+ No migration needed for given Jest configuration
115
+ `);
116
+ return;
117
+ }
118
+ const stringify = file.endsWith('.json') ? JSON.stringify : json5_1.stringify;
119
+ const prefix = file.endsWith('.json') ? '"jest": ' : 'module.exports = ';
120
+ // output new config
121
+ process.stderr.write(`
122
+ Migrated Jest configuration:
123
+ `);
124
+ process.stdout.write(`${prefix}${stringify(migratedConfig, undefined, ' ')}\n`);
125
+ };
179
126
  exports.run = run;
180
127
  function cleanupConfig(config) {
181
128
  if (config.globals) {
@@ -202,16 +149,26 @@ function cleanupConfig(config) {
202
149
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
203
150
  function dedupSort(arr) {
204
151
  return arr
205
- .filter(function (s, i, a) { return a.findIndex(function (e) { return s.toString() === e.toString(); }) === i; })
206
- .sort(function (a, b) { return (a.toString() > b.toString() ? 1 : a.toString() < b.toString() ? -1 : 0); });
152
+ .filter((s, i, a) => a.findIndex((e) => s.toString() === e.toString()) === i)
153
+ .sort((a, b) => (a.toString() > b.toString() ? 1 : a.toString() < b.toString() ? -1 : 0));
207
154
  }
208
155
  /**
209
156
  * @internal
210
157
  */
211
- var help = function () { return __awaiter(void 0, void 0, void 0, function () {
212
- return __generator(this, function (_a) {
213
- process.stdout.write("\nUsage:\n ts-jest config:migrate [options] <config-file>\n\nArguments:\n <config-file> Can be a js or json Jest config file. If it is a\n package.json file, the configuration will be read from\n the \"jest\" property.\n\nOptions:\n --js ts|babel Process .js files with ts-jest if 'ts' or with\n babel-jest if 'babel'\n --no-jest-preset Disable the use of Jest presets\n");
214
- return [2 /*return*/];
215
- });
216
- }); };
158
+ const help = async () => {
159
+ process.stdout.write(`
160
+ Usage:
161
+ ts-jest config:migrate [options] <config-file>
162
+
163
+ Arguments:
164
+ <config-file> Can be a js or json Jest config file. If it is a
165
+ package.json file, the configuration will be read from
166
+ the "jest" property.
167
+
168
+ Options:
169
+ --js ts|babel Process .js files with ts-jest if 'ts' or with
170
+ babel-jest if 'babel'
171
+ --no-jest-preset Disable the use of Jest presets
172
+ `);
173
+ };
217
174
  exports.help = help;