UNPKG

jest-firestore

Version:

Run your tests using Jest & Firestore Emulator

56 lines (52 loc) 1.99 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.connectableHostname = connectableHostname; exports.startEmulator = startEmulator; exports.stopEmulator = stopEmulator; var _helpers = require("./helpers"); var _firestoreEmulator = require("firebase-tools/lib/emulator/firestoreEmulator"); var _portUtils = require("firebase-tools/lib/emulator/portUtils"); // @ts-expect-error - no types // @ts-expect-error - no types const debug = require('debug')('jest-firestore:emulator'); async function startEmulator(args) { const start = Date.now(); const emulator = new _firestoreEmulator.FirestoreEmulator({ ...args, port: await (0, _helpers.getFreePort)() }); await emulator.start(); const info = emulator.getInfo(); debug('Emulator info', { info }); await (0, _portUtils.waitForPortUsed)(info.port, connectableHostname(info.host)); debug(`Emulator ready on ${info.port} port; took ${Date.now() - start}ms`); return `${info.host}:${info.port}`; } async function stopEmulator() { const emulator = new _firestoreEmulator.FirestoreEmulator({}); await emulator.stop(); } /** * Return a connectable hostname, replacing wildcard 0.0.0.0 or :: with loopback * addresses 127.0.0.1 / ::1 correspondingly. See below for why this is needed: * https://github.com/firebase/firebase-tools-ui/issues/286 * * This assumes that the consumer (i.e. client SDK, etc.) is located on the same * device as the Emulator hub (i.e. CLI), which may not be true on multi-device * setups, etc. In that case, the customer can work around this by specifying a * non-wildcard IP address (like the IP address on LAN, if accessing via LAN). */ function connectableHostname(hostname) { if (hostname === '0.0.0.0') { hostname = '127.0.0.1'; } else if (hostname === '::' /* unquoted IPv6 wildcard */) { hostname = '::1'; } else if (hostname === '[::]' /* quoted IPv6 wildcard */) { hostname = '[::1]'; } return hostname; }