UNPKG

@metamask/snaps-jest

Version:

A Jest preset for end-to-end testing MetaMask Snaps, including a Jest environment, and a set of Jest matchers

83 lines 3.34 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.SnapsEnvironment = void 0; const snaps_simulation_1 = require("@metamask/snaps-simulation"); const utils_1 = require("@metamask/utils"); const jest_environment_node_1 = __importDefault(require("jest-environment-node")); const internals_1 = require("./internals/index.cjs"); const options_1 = require("./options.cjs"); const log = (0, utils_1.createModuleLogger)(internals_1.rootLogger, 'environment'); class SnapsEnvironment extends jest_environment_node_1.default { #options; #server; #instance; /** * Constructor. * * @param options - The environment options. * @param context - The environment context. */ constructor(options, context) { super(options, context); this.#options = (0, options_1.getOptions)(options.projectConfig.testEnvironmentOptions); } /** * Set up the environment. This starts the built-in HTTP server, and creates a * new browser instance. */ async setup() { await super.setup(); if (this.#options.server.enabled) { log('Starting server.'); this.#server = await (0, internals_1.startServer)(this.#options.server); } this.global.snapsEnvironment = this; } /** * Tear down the environment. This closes the browser, and stops the built-in * HTTP server. */ async teardown() { await this.#instance?.executionService.terminateAllSnaps(); this.#server?.close(); await super.teardown(); } /** * Install a Snap in the environment. This will terminate any previously * installed Snaps, and run the Snap code in a new execution service. * * @param snapId - The ID of the Snap to install. * @param options - The options to use when installing the Snap. * @param options.executionService - The execution service to use. * @param options.executionServiceOptions - The options to use when creating the * execution service, if any. This should only include options specific to the * provided execution service. * @param options.options - The simulation options. * @template Service - The type of the execution service. * @returns The installed Snap. */ async installSnap(snapId = this.snapId, options = {}) { await this.#instance?.executionService.terminateAllSnaps(); this.#instance = await (0, snaps_simulation_1.installSnap)(snapId, options); return this.#instance; } /** * Get the snap ID for the current environment, which is used if no snap ID is * passed to {@link installSnap}. This assumes that the built-in server is * running. * * @returns The snap ID. * @throws If the server is not running. */ get snapId() { (0, utils_1.assert)(this.#server, 'You must specify a snap ID, because the built-in server is not running.'); const { port } = this.#server.address(); return `local:http://localhost:${port}`; } } exports.SnapsEnvironment = SnapsEnvironment; exports.default = SnapsEnvironment; //# sourceMappingURL=environment.cjs.map