UNPKG

@o3r/testing

Version:

The module provides testing (e2e, unit test) utilities to help you build your own E2E pipeline integrating visual testing.

58 lines 2.88 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.adjustPath = adjustPath; var tslib_1 = require("tslib"); var fs = require("node:fs"); var node_module_1 = require("node:module"); var path = require("node:path"); var esbuild_1 = require("esbuild"); var module_from_string_1 = require("module-from-string"); /** * Switch to the needed implementation of core testing, when running e2e tests * transforms ESM into CJS when needed * @param frameworkName Name of the framework used for e2e testing (playwright|protractor) * @param customTransformOptions */ function adjustPath(frameworkName, customTransformOptions) { if (customTransformOptions === void 0) { customTransformOptions = {}; } var modulesCache = {}; // eslint-disable-next-line @typescript-eslint/unbound-method -- No need to bind the method, we are using `apply` var originalRequire = node_module_1.Module.prototype.require; var regex = new RegExp("@o3r/testing/core(?!/".concat(frameworkName, ")(.*)")); node_module_1.Module.prototype.require = function (id) { var newId = id.replace(regex, "@o3r/testing/core/".concat(frameworkName, "$1")); try { return Reflect.apply(originalRequire, this, [newId]); } catch (_a) { var paths = [] .concat(this.paths, this.paths.map(function (i) { return i.replace(/[/\\]node_modules$/, ''); })); var filePath = require.resolve(newId, { paths: paths }); if (!modulesCache[filePath]) { var fileContent = fs.readFileSync(filePath); var cwd = path.resolve(process.cwd(), 'src'); // we use ESBUILD to transform it into CJS var trans = (0, esbuild_1.transformSync)(fileContent.toString(), tslib_1.__assign({ loader: 'js', format: 'cjs', target: 'es2016', sourcesContent: true, sourceRoot: cwd }, customTransformOptions)); /** * requireFromString will execute the file apart from just returning a module * It can throw an exception which will prevent stop the execution * * That main blocker includes IVY-compatible libraries which have * no compiler facade in a global scope. */ try { modulesCache[filePath] = (0, module_from_string_1.requireFromString)(trans.code, { dirname: path.dirname(filePath), useCurrentGlobal: true }); } catch (ex) { // eslint-disable-next-line no-console -- no other logger available console.error(ex); } } return modulesCache[filePath]; } }; } //# sourceMappingURL=path-replacement.js.map