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

93 lines 3.69 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.startServer = void 0; const node_1 = require("@metamask/snaps-utils/node"); const utils_1 = require("@metamask/utils"); const express_1 = __importStar(require("express/index.js")); const fs_1 = require("fs"); const path_1 = require("path"); const logger_1 = require("./logger.cjs"); /** * Check that: * * - The root directory exists. * - The root directory contains a `snap.manifest.json` file. * - The file path in the manifest exists. * * @param root - The root directory. * @throws If any of the checks fail. */ async function assertRoot(root) { if (!root) { throw new Error('You must specify a root directory.'); } if (!(await (0, node_1.isDirectory)(root, false))) { throw new Error(`Root directory "${root}" is not a directory.`); } const manifestPath = (0, path_1.resolve)(root, 'snap.manifest.json'); const manifest = await fs_1.promises .readFile(manifestPath, 'utf8') .then(JSON.parse); (0, node_1.assertIsSnapManifest)(manifest); const filePath = (0, path_1.resolve)(root, manifest.source.location.npm.filePath); if (!(await (0, node_1.isFile)(filePath))) { throw new Error(`File "${filePath}" does not exist, or is not a file. Did you forget to build your snap?`); } } /** * Start an HTTP server on `localhost` with a random port. This is used to serve * the static files for the environment. * * @param options - The options to use. * @param options.port - The port to use for the server. * @param options.root - The root directory to serve from the server. * @returns The HTTP server. */ async function startServer(options) { await assertRoot(options.root); const log = (0, utils_1.createModuleLogger)(logger_1.rootLogger, 'server'); const app = (0, express_1.default)(); app.use((_request, response, next) => { response.header('Access-Control-Allow-Origin', '*'); response.header('Access-Control-Allow-Credentials', 'true'); response.header('Access-Control-Allow-Methods', 'GET, OPTIONS'); response.header('Access-Control-Allow-Headers', 'Content-Type'); next(); }); app.use((0, express_1.static)((0, path_1.resolve)(process.cwd(), options.root))); return await new Promise((resolve, reject) => { const server = app.listen(options.port, (error) => { if (error) { log(error); reject(error); return; } resolve(server); }); }); } exports.startServer = startServer; //# sourceMappingURL=server.cjs.map