UNPKG

jest-firestore

Version:

Run your tests using Jest & Firestore Emulator

54 lines (52 loc) 2.43 kB
"use strict"; var _jestEnvironmentNode = require("jest-environment-node"); var _helpers = require("./helpers"); var _emulator = require("./emulator"); const debug = require('debug')('jest-firestore:environment'); let runningEmulator; let refCount = 0; let i = 0; module.exports = class FirestoreEnvironment extends _jestEnvironmentNode.TestEnvironment { constructor(config, context) { super(config, context); this.config = config; this.shouldUseSharedDBForAllJestWorkers = (0, _helpers.shouldUseSharedDBForAllJestWorkers)(config.globalConfig.rootDir); } async setup() { debug(`Setup Firestore Test Environment. PID: ${process.pid}`); refCount++; if (process.env.FIRESTORE_EMULATOR_HOST) { // environment can receive ENV variables declared in the globalSetup script. // So, here we could have FIRESTORE_EMULATOR_HOST set either by the globalSetup script // or passed from parent environment debug(`FIRESTORE_EMULATOR_HOST is set to ${process.env.FIRESTORE_EMULATOR_HOST}, bypassing emulator start`); } else if (!this.shouldUseSharedDBForAllJestWorkers) { // environment might be created in reused worker, so we need to check if emulator is already running if (!runningEmulator) { const options = (0, _helpers.getFirestoreEmulatorOptions)(this.config.globalConfig.rootDir); debug(`Starting Firestore Emulator`); runningEmulator = await (0, _emulator.startEmulator)(options); } this.global.process.env.FIRESTORE_EMULATOR_HOST = runningEmulator; } const databaseName = this.shouldUseSharedDBForAllJestWorkers ? `db-${process.pid}-${i++}` : '(default)'; // non stable api, considering to delete it this.global.process.env.FIRESTORE_TESTING_DB = databaseName; debug(`Set testing database to ${databaseName}`); await super.setup(); } async teardown() { const debug = require('debug')('jest-firestore:environment:teardown'); refCount--; if (runningEmulator) { debug(`Found running emulator on ${runningEmulator} jest worker PID: ${process.pid}`); if (refCount === 0) { debug(`Stopping Firestore Emulator on ${runningEmulator}`); await (0, _emulator.stopEmulator)(); } else { debug(`There is more environments still running (${refCount}) on a jest worker PID: ${process.pid}, skipping emulator stop`); } } await super.teardown(); } };