UNPKG

@edge-runtime/jest-environment

Version:

A Jest integration to run assertions in Edge Runtime context.

127 lines 3.44 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const vm_1 = require("@edge-runtime/vm"); const jest_util_1 = require("jest-util"); const fake_timers_1 = require("@jest/fake-timers"); const jest_mock_1 = require("jest-mock"); class EdgeEnvironment { constructor(config) { var _a, _b; const vm = new vm_1.EdgeVM({ extend: (context) => { context.global = context; context.Buffer = Buffer; return context; }, ...(_b = ((_a = config.projectConfig) !== null && _a !== void 0 ? _a : config)) === null || _b === void 0 ? void 0 : _b.testEnvironmentOptions, }); revealPrimitives(vm); this.context = vm.context; const global = (this.global = Object.assign(this.context, config.testEnvironmentOptions)); (0, jest_util_1.installCommonGlobals)(global, config.globals); this.moduleMocker = new jest_mock_1.ModuleMocker(global); this.fakeTimers = new fake_timers_1.LegacyFakeTimers({ config, global, moduleMocker: this.moduleMocker, timerConfig: { idToRef: (id) => id, refToId: (ref) => ref, }, }); this.fakeTimersModern = new fake_timers_1.ModernFakeTimers({ config, global, }); } async setup() { } async teardown() { if (this.fakeTimers != null) { this.fakeTimers.dispose(); } if (this.fakeTimersModern != null) { this.fakeTimersModern.dispose(); } this.context = null; this.fakeTimers = null; this.fakeTimersModern = null; } exportConditions() { return ['edge', 'edge-light']; } getVmContext() { return this.context; } } /** * Jest will access some primitives directly through vm.context so we must * make them available. We do this by redefining the property in the * context object. */ function revealPrimitives(vm) { ; [ 'Array', 'ArrayBuffer', 'Atomics', 'BigInt', 'BigInt64Array', 'BigUint64Array', 'Boolean', 'DataView', 'Date', 'decodeURI', 'decodeURIComponent', 'encodeURI', 'encodeURIComponent', 'Error', 'EvalError', 'Float32Array', 'Float64Array', 'Function', 'Int8Array', 'Int16Array', 'Int32Array', 'Intl', 'isFinite', 'isNaN', 'JSON', 'Map', 'Math', 'Number', 'Object', 'parseFloat', 'parseInt', 'Promise', 'Proxy', 'RangeError', 'ReferenceError', 'Reflect', 'RegExp', 'Set', 'SharedArrayBuffer', 'String', 'Symbol', 'SyntaxError', 'TypeError', 'Uint8Array', 'Uint8ClampedArray', 'Uint16Array', 'Uint32Array', 'URIError', 'WeakMap', 'WeakSet', 'WebAssembly', ].forEach((property) => { vm.evaluate(` Object.defineProperty(this, '${property}', { configurable: false, enumerable: false, value: ${property}, writable: true, }) `); }); } exports.default = EdgeEnvironment; //# sourceMappingURL=index.js.map