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.11 kB
function $importDefault(module) { if (module?.__esModule) { return module.default; } return module; } import { installSnap } from "@metamask/snaps-simulation"; import { assert, createModuleLogger } from "@metamask/utils"; import $NodeEnvironment from "jest-environment-node"; const NodeEnvironment = $importDefault($NodeEnvironment); import { rootLogger, startServer } from "./internals/index.mjs"; import { getOptions } from "./options.mjs"; const log = createModuleLogger(rootLogger, 'environment'); export class SnapsEnvironment extends NodeEnvironment { #options; #server; #instance; /** * Constructor. * * @param options - The environment options. * @param context - The environment context. */ constructor(options, context) { super(options, context); this.#options = 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 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 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() { 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}`; } } export default SnapsEnvironment; //# sourceMappingURL=environment.mjs.map