@pkg-nec/babel-jest 30.4.1

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/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ MIT License
2
+
3
+ Copyright (c) Meta Platforms, Inc. and affiliates.
4
+ Copyright Contributors to the Jest project.
5
+
6
+ Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ of this software and associated documentation files (the "Software"), to deal
8
+ in the Software without restriction, including without limitation the rights
9
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ copies of the Software, and to permit persons to whom the Software is
11
+ furnished to do so, subject to the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be included in all
14
+ copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,43 @@
1
+ # babel-jest
2
+
3
+ [Babel](https://github.com/babel/babel) [jest](https://github.com/jestjs/jest) plugin
4
+
5
+ ## Usage
6
+
7
+ If you are already using `@pkg-nec/jest-cli`, add `@pkg-nec/babel-jest` and it will automatically compile JavaScript code using Babel.
8
+
9
+ ```bash
10
+ yarn add --dev @pkg-nec/babel-jest @babel/core
11
+ ```
12
+
13
+ If you would like to write your own preprocessor, uninstall and delete babel-jest and set the [config.transform](https://jestjs.io/docs/configuration#transform-object-string-string) option to your preprocessor.
14
+
15
+ ## Setup
16
+
17
+ _Note: this step is only required if you are using `@pkg-nec/babel-jest` with additional code preprocessors._
18
+
19
+ To explicitly define `@pkg-nec/babel-jest` as a transformer for your JavaScript code, map _.js_ files to the `@pkg-nec/babel-jest` module. Typescript files are also supported.
20
+
21
+ By default, it loads your existing Babel configuration (if any)
22
+
23
+ ```json
24
+ "transform": {
25
+ "\\.[jt]sx?$": "babel-jest"
26
+ },
27
+ ```
28
+
29
+ You can also pass further [babel options](https://babeljs.io/docs/options)
30
+
31
+ ```json
32
+ "transform": {
33
+ "\\.[jt]sx?$": ["babel-jest", { "extends": "./babel.config.js", "plugins": ["babel-plugin-transform-import-meta"] }]
34
+ },
35
+ ```
36
+
37
+ By default, `@pkg-nec/babel-jest` includes `@pkg-nec/babel-preset-jest`. In addition to the babel options, we introduce a new option, `excludeJestPreset`, which allows you to disable this behavior. Note that this will break `jest.mock` hoisting.
38
+
39
+ ```json
40
+ "transform": {
41
+ "\\.[jt]sx?$": ["babel-jest", { "excludeJestPreset": true }],
42
+ }
43
+ ```
@@ -0,0 +1,26 @@
1
+ /**
2
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ */
7
+
8
+ import {TransformOptions} from '@babel/core';
9
+ import {SyncTransformer, TransformerCreator} from '@pkg-nec/jest-transform';
10
+
11
+ export declare const createTransformer: TransformerCreator<
12
+ SyncTransformer<TransformerConfig>,
13
+ TransformerConfig
14
+ >;
15
+
16
+ export declare interface TransformerConfig extends TransformOptions {
17
+ excludeJestPreset?: boolean;
18
+ }
19
+
20
+ declare const transformerFactory: {
21
+ createTransformer: TransformerCreator<
22
+ SyncTransformer<TransformerConfig>,
23
+ TransformerConfig
24
+ >;
25
+ };
26
+ export default transformerFactory;
package/build/index.js ADDED
@@ -0,0 +1,300 @@
1
+ /*!
2
+ * /**
3
+ * * Copyright (c) Meta Platforms, Inc. and affiliates.
4
+ * *
5
+ * * This source code is licensed under the MIT license found in the
6
+ * * LICENSE file in the root directory of this source tree.
7
+ * * /
8
+ */
9
+ /******/ (() => { // webpackBootstrap
10
+ /******/ "use strict";
11
+ /******/ var __webpack_modules__ = ({
12
+
13
+ /***/ "./src/babel.ts"
14
+ (__unused_webpack_module, exports) {
15
+
16
+
17
+
18
+ Object.defineProperty(exports, "__esModule", ({
19
+ value: true
20
+ }));
21
+ Object.defineProperty(exports, "loadPartialConfigAsync", ({
22
+ enumerable: true,
23
+ get: function () {
24
+ return _core().loadPartialConfigAsync;
25
+ }
26
+ }));
27
+ exports.loadPartialConfigSync = void 0;
28
+ Object.defineProperty(exports, "transformAsync", ({
29
+ enumerable: true,
30
+ get: function () {
31
+ return _core().transformAsync;
32
+ }
33
+ }));
34
+ Object.defineProperty(exports, "transformSync", ({
35
+ enumerable: true,
36
+ get: function () {
37
+ return _core().transformSync;
38
+ }
39
+ }));
40
+ function _core() {
41
+ const data = require("@babel/core");
42
+ _core = function () {
43
+ return data;
44
+ };
45
+ return data;
46
+ }
47
+ /**
48
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
49
+ *
50
+ * This source code is licensed under the MIT license found in the
51
+ * LICENSE file in the root directory of this source tree.
52
+ */
53
+
54
+ // this is a separate file so it can be mocked in tests
55
+
56
+ // Old babel 7 versions didn't have loadPartialConfigSync
57
+ const _loadPartialConfigSync = exports.loadPartialConfigSync = _core().loadPartialConfigSync ?? _core().loadPartialConfig;
58
+
59
+ /***/ }
60
+
61
+ /******/ });
62
+ /************************************************************************/
63
+ /******/ // The module cache
64
+ /******/ var __webpack_module_cache__ = {};
65
+ /******/
66
+ /******/ // The require function
67
+ /******/ function __webpack_require__(moduleId) {
68
+ /******/ // Check if module is in cache
69
+ /******/ var cachedModule = __webpack_module_cache__[moduleId];
70
+ /******/ if (cachedModule !== undefined) {
71
+ /******/ return cachedModule.exports;
72
+ /******/ }
73
+ /******/ // Create a new module (and put it into the cache)
74
+ /******/ var module = __webpack_module_cache__[moduleId] = {
75
+ /******/ // no module.id needed
76
+ /******/ // no module.loaded needed
77
+ /******/ exports: {}
78
+ /******/ };
79
+ /******/
80
+ /******/ // Execute the module function
81
+ /******/ __webpack_modules__[moduleId](module, module.exports, __webpack_require__);
82
+ /******/
83
+ /******/ // Return the exports of the module
84
+ /******/ return module.exports;
85
+ /******/ }
86
+ /******/
87
+ /************************************************************************/
88
+ var __webpack_exports__ = {};
89
+ // This entry needs to be wrapped in an IIFE because it uses a non-standard name for the exports (exports).
90
+ (() => {
91
+ var exports = __webpack_exports__;
92
+
93
+
94
+ Object.defineProperty(exports, "__esModule", ({
95
+ value: true
96
+ }));
97
+ exports["default"] = exports.createTransformer = void 0;
98
+ function _nodeCrypto() {
99
+ const data = require("node:crypto");
100
+ _nodeCrypto = function () {
101
+ return data;
102
+ };
103
+ return data;
104
+ }
105
+ function path() {
106
+ const data = _interopRequireWildcard(require("node:path"));
107
+ path = function () {
108
+ return data;
109
+ };
110
+ return data;
111
+ }
112
+ function _chalk() {
113
+ const data = _interopRequireDefault(require("chalk"));
114
+ _chalk = function () {
115
+ return data;
116
+ };
117
+ return data;
118
+ }
119
+ function fs() {
120
+ const data = _interopRequireWildcard(require("graceful-fs"));
121
+ fs = function () {
122
+ return data;
123
+ };
124
+ return data;
125
+ }
126
+ function _slash() {
127
+ const data = _interopRequireDefault(require("slash"));
128
+ _slash = function () {
129
+ return data;
130
+ };
131
+ return data;
132
+ }
133
+ var _babel = __webpack_require__("./src/babel.ts");
134
+ function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
135
+ function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); }
136
+ /**
137
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
138
+ *
139
+ * This source code is licensed under the MIT license found in the
140
+ * LICENSE file in the root directory of this source tree.
141
+ */
142
+
143
+ const THIS_FILE = fs().readFileSync(__filename);
144
+ const jestPresetPath = require.resolve('@pkg-nec/babel-preset-jest');
145
+ const babelIstanbulPlugin = require.resolve('babel-plugin-istanbul');
146
+ function assertLoadedBabelConfig(babelConfig, cwd, filename) {
147
+ if (!babelConfig) {
148
+ throw new Error(`babel-jest: Babel ignores ${_chalk().default.bold((0, _slash().default)(path().relative(cwd, filename)))} - make sure to include the file in Jest's ${_chalk().default.bold('transformIgnorePatterns')} as well.`);
149
+ }
150
+ }
151
+ function addIstanbulInstrumentation(filename, babelOptions, transformOptions) {
152
+ if (transformOptions.instrument) {
153
+ const copiedBabelOptions = {
154
+ ...babelOptions,
155
+ auxiliaryCommentBefore: ' istanbul ignore next '
156
+ };
157
+ // Copied from jest-runtime transform.js
158
+ copiedBabelOptions.plugins = [...(copiedBabelOptions.plugins ?? []), [babelIstanbulPlugin, {
159
+ // files outside `cwd` will not be instrumented
160
+ cwd: transformOptions.config.cwd,
161
+ exclude: [],
162
+ extension: [path().extname(filename)]
163
+ }]];
164
+ return copiedBabelOptions;
165
+ }
166
+ return babelOptions;
167
+ }
168
+ function getCacheKeyFromConfig(sourceText, sourcePath, babelOptions, transformOptions) {
169
+ const {
170
+ config,
171
+ configString,
172
+ instrument
173
+ } = transformOptions;
174
+ const configPath = [babelOptions.config ?? '', babelOptions.babelrc ?? ''];
175
+ return (0, _nodeCrypto().createHash)('sha1').update(THIS_FILE).update('\0', 'utf8').update(JSON.stringify(babelOptions.options)).update('\0', 'utf8').update(sourceText).update('\0', 'utf8').update(path().relative(config.rootDir, sourcePath)).update('\0', 'utf8').update(configString).update('\0', 'utf8').update(configPath.join('')).update('\0', 'utf8').update(instrument ? 'instrument' : '').update('\0', 'utf8').update("production" ?? 0).update('\0', 'utf8').update(process.env.BABEL_ENV ?? '').update('\0', 'utf8').update(process.version).digest('hex').slice(0, 32);
176
+ }
177
+ function loadBabelConfig(cwd, filename, transformOptions) {
178
+ const babelConfig = (0, _babel.loadPartialConfigSync)(transformOptions);
179
+ assertLoadedBabelConfig(babelConfig, cwd, filename);
180
+ return babelConfig;
181
+ }
182
+ async function loadBabelConfigAsync(cwd, filename, transformOptions) {
183
+ const babelConfig = await (0, _babel.loadPartialConfigAsync)(transformOptions);
184
+ assertLoadedBabelConfig(babelConfig, cwd, filename);
185
+ return babelConfig;
186
+ }
187
+ function loadBabelOptions(cwd, filename, transformOptions, jestTransformOptions) {
188
+ const {
189
+ options
190
+ } = loadBabelConfig(cwd, filename, transformOptions);
191
+ return addIstanbulInstrumentation(filename, options, jestTransformOptions);
192
+ }
193
+ async function loadBabelOptionsAsync(cwd, filename, transformOptions, jestTransformOptions) {
194
+ const {
195
+ options
196
+ } = await loadBabelConfigAsync(cwd, filename, transformOptions);
197
+ return addIstanbulInstrumentation(filename, options, jestTransformOptions);
198
+ }
199
+ const createTransformer = transformerConfig => {
200
+ const {
201
+ excludeJestPreset,
202
+ ...inputOptions
203
+ } = transformerConfig ?? {};
204
+ const options = {
205
+ ...inputOptions,
206
+ caller: {
207
+ name: 'babel-jest',
208
+ supportsDynamicImport: false,
209
+ supportsExportNamespaceFrom: false,
210
+ supportsStaticESM: false,
211
+ supportsTopLevelAwait: false,
212
+ ...inputOptions.caller
213
+ },
214
+ compact: false,
215
+ plugins: inputOptions.plugins ?? [],
216
+ presets: [...(inputOptions.presets ?? []), ...(excludeJestPreset === true ? [] : [jestPresetPath])],
217
+ sourceMaps: 'both'
218
+ };
219
+ function mergeBabelTransformOptions(filename, transformOptions) {
220
+ const {
221
+ cwd,
222
+ rootDir
223
+ } = transformOptions.config;
224
+ // `cwd` and `root` first to allow incoming options to override it
225
+ return {
226
+ cwd,
227
+ root: rootDir,
228
+ ...options,
229
+ caller: {
230
+ ...options.caller,
231
+ supportsDynamicImport: transformOptions.supportsDynamicImport ?? options.caller.supportsDynamicImport,
232
+ supportsExportNamespaceFrom: transformOptions.supportsExportNamespaceFrom ?? options.caller.supportsExportNamespaceFrom,
233
+ supportsStaticESM: transformOptions.supportsStaticESM ?? options.caller.supportsStaticESM,
234
+ supportsTopLevelAwait: transformOptions.supportsTopLevelAwait ?? options.caller.supportsTopLevelAwait
235
+ },
236
+ filename
237
+ };
238
+ }
239
+ return {
240
+ canInstrument: true,
241
+ getCacheKey(sourceText, sourcePath, transformOptions) {
242
+ const babelOptions = loadBabelConfig(transformOptions.config.cwd, sourcePath, mergeBabelTransformOptions(sourcePath, transformOptions));
243
+ return getCacheKeyFromConfig(sourceText, sourcePath, babelOptions, transformOptions);
244
+ },
245
+ async getCacheKeyAsync(sourceText, sourcePath, transformOptions) {
246
+ const babelOptions = await loadBabelConfigAsync(transformOptions.config.cwd, sourcePath, mergeBabelTransformOptions(sourcePath, transformOptions));
247
+ return getCacheKeyFromConfig(sourceText, sourcePath, babelOptions, transformOptions);
248
+ },
249
+ process(sourceText, sourcePath, transformOptions) {
250
+ const babelOptions = loadBabelOptions(transformOptions.config.cwd, sourcePath, mergeBabelTransformOptions(sourcePath, transformOptions), transformOptions);
251
+ const transformResult = (0, _babel.transformSync)(sourceText, babelOptions);
252
+ if (transformResult) {
253
+ const {
254
+ code,
255
+ map
256
+ } = transformResult;
257
+ if (typeof code === 'string') {
258
+ return {
259
+ code,
260
+ map
261
+ };
262
+ }
263
+ }
264
+ return {
265
+ code: sourceText
266
+ };
267
+ },
268
+ async processAsync(sourceText, sourcePath, transformOptions) {
269
+ const babelOptions = await loadBabelOptionsAsync(transformOptions.config.cwd, sourcePath, mergeBabelTransformOptions(sourcePath, transformOptions), transformOptions);
270
+ const transformResult = await (0, _babel.transformAsync)(sourceText, babelOptions);
271
+ if (transformResult) {
272
+ const {
273
+ code,
274
+ map
275
+ } = transformResult;
276
+ if (typeof code === 'string') {
277
+ return {
278
+ code,
279
+ map
280
+ };
281
+ }
282
+ }
283
+ return {
284
+ code: sourceText
285
+ };
286
+ }
287
+ };
288
+ };
289
+ exports.createTransformer = createTransformer;
290
+ const transformerFactory = {
291
+ // Assigned here, instead of as a separate export, due to limitations in Jest's
292
+ // requireOrImportModule, requiring all exports to be on the `default` export
293
+ createTransformer
294
+ };
295
+ var _default = exports["default"] = transformerFactory;
296
+ })();
297
+
298
+ module.exports = __webpack_exports__;
299
+ /******/ })()
300
+ ;
@@ -0,0 +1,4 @@
1
+ import cjsModule from './index.js';
2
+
3
+ export const createTransformer = cjsModule.createTransformer;
4
+ export default cjsModule.default;
package/package.json ADDED
@@ -0,0 +1,46 @@
1
+ {
2
+ "name": "@pkg-nec/babel-jest",
3
+ "description": "Jest plugin to use babel for transformation.",
4
+ "version": "30.4.1",
5
+ "repository": {
6
+ "type": "git",
7
+ "url": "https://github.com/jestjs/jest.git",
8
+ "directory": "packages/babel-jest"
9
+ },
10
+ "license": "MIT",
11
+ "main": "./build/index.js",
12
+ "types": "./build/index.d.ts",
13
+ "exports": {
14
+ ".": {
15
+ "types": "./build/index.d.ts",
16
+ "require": "./build/index.js",
17
+ "import": "./build/index.mjs",
18
+ "default": "./build/index.js"
19
+ },
20
+ "./package.json": "./package.json"
21
+ },
22
+ "dependencies": {
23
+ "@pkg-nec/babel-preset-jest": "30.4.0",
24
+ "@pkg-nec/jest-transform": "30.4.1",
25
+ "@types/babel__core": "^7.20.5",
26
+ "babel-plugin-istanbul": "^7.0.1",
27
+ "chalk": "^4.1.2",
28
+ "graceful-fs": "^4.2.11",
29
+ "slash": "^3.0.0"
30
+ },
31
+ "devDependencies": {
32
+ "@babel-8/core": "npm:@babel/core@8.0.0-rc.4",
33
+ "@babel/core": "^7.27.4",
34
+ "@pkg-nec/jest-test-utils": "30.4.1",
35
+ "@types/graceful-fs": "^4.1.9"
36
+ },
37
+ "peerDependencies": {
38
+ "@babel/core": "^7.11.0 || ^8.0.0-0"
39
+ },
40
+ "engines": {
41
+ "node": "^18.14.0 || ^20.0.0 || ^22.0.0 || >=24.0.0"
42
+ },
43
+ "publishConfig": {
44
+ "access": "public"
45
+ }
46
+ }