UNPKG

jest-repl

Version:
126 lines (123 loc) 4.16 kB
/*! * /** * * Copyright (c) Meta Platforms, Inc. and affiliates. * * * * This source code is licensed under the MIT license found in the * * LICENSE file in the root directory of this source tree. * * / */ /******/ (() => { // webpackBootstrap /******/ "use strict"; var __webpack_exports__ = {}; function path() { const data = _interopRequireWildcard(require("path")); path = function () { return data; }; return data; } function repl() { const data = _interopRequireWildcard(require("repl")); repl = function () { return data; }; return data; } function util() { const data = _interopRequireWildcard(require("util")); util = function () { return data; }; return data; } function _vm() { const data = require("vm"); _vm = function () { return data; }; return data; } function _jestUtil() { const data = require("jest-util"); _jestUtil = function () { return data; }; return data; } 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); } /** * Copyright (c) Meta Platforms, Inc. and affiliates. * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. */ // TODO: support async as well let transformer; let transformerConfig; const evalCommand = (cmd, _context, _filename, callback) => { let result; try { if (transformer != null) { const transformResult = transformer.process(cmd, jestGlobalConfig.replname ?? 'jest.js', { cacheFS: new Map(), config: jestProjectConfig, configString: JSON.stringify(jestProjectConfig), instrument: false, supportsDynamicImport: false, supportsExportNamespaceFrom: false, supportsStaticESM: false, supportsTopLevelAwait: false, transformerConfig }); cmd = typeof transformResult === 'string' ? transformResult : transformResult.code; } result = (0, _vm().runInThisContext)(cmd); } catch (error) { return callback(isRecoverableError(error) ? new (repl().Recoverable)(error) : error); } return callback(null, result); }; const isRecoverableError = error => { if (!util().types.isNativeError(error)) { return false; } if (error.name === 'SyntaxError') { return ['Unterminated template', 'Missing } in template expression', 'Unexpected end of input', 'missing ) after argument list', 'Unexpected token'].some(exception => error.message.includes(exception)); } return false; }; if (jestProjectConfig.transform) { let transformerPath = null; for (const transform of jestProjectConfig.transform) { if (new RegExp(transform[0]).test('foobar.js')) { transformerPath = transform[1]; transformerConfig = transform[2]; break; } } if (transformerPath != null) { const transformerOrFactory = (0, _jestUtil().interopRequireDefault)(require(transformerPath)).default; if (typeof transformerOrFactory.createTransformer === 'function') { transformer = transformerOrFactory.createTransformer(transformerConfig); } else { transformer = transformerOrFactory; } if (typeof transformer?.process !== 'function') { throw new TypeError('Jest: a transformer must export a `process` function.'); } } } const replInstance = repl().start({ eval: evalCommand, prompt: '\u203A ', useGlobal: true }); replInstance.context.require = moduleName => { if (/([./\\])/.test(moduleName)) { moduleName = path().resolve(process.cwd(), moduleName); } return require(moduleName); }; module.exports = __webpack_exports__; /******/ })() ;