UNPKG

jest-metadata

Version:

🦸‍♂️ Superhero power for your Jest reporters! 🦸‍♀️

65 lines 2.52 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getClientId = exports.getServerId = exports.getSandboxedRealm = exports.detectDuplicateRealms = exports.injectRealmIntoSandbox = exports.isJestWorker = exports.registerServerId = exports.isClient = exports.isServer = void 0; /* eslint-disable @typescript-eslint/no-explicit-any */ const utils_1 = require("../utils"); const _initialServerId = getServerId(); function isServer() { return !_initialServerId; } exports.isServer = isServer; function isClient() { return !!_initialServerId; } exports.isClient = isClient; function registerServerId(id) { process.env.JEST_METADATA_SERVER = id; } exports.registerServerId = registerServerId; function isJestWorker() { return 'JEST_WORKER_ID' in process.env; } exports.isJestWorker = isJestWorker; function injectRealmIntoSandbox(sandbox, realm) { sandbox.__JEST_METADATA__ = realm; if (sandbox !== globalThis) { sandbox.__JEST_METADATA_SANDBOX__ = true; } return realm; } exports.injectRealmIntoSandbox = injectRealmIntoSandbox; /** * Workaround for the fallback mode, when Jest uses jest-environment-node. * Jest blindly copies `globalThis` into the sandbox, so it is not enough to * simply check that __JEST_METADATA_SANDBOX__ is not truthy. * * This is especially bad in Jest's single worker mode, because * reporter's globalThis === testEnvironment's globalThis == sandbox. * * This hook is enabled after the copying happens, and disabled at later stages * when all potentially conflicting packages are loaded. It is not easy to * grasp, but it works. */ function detectDuplicateRealms(enabled) { const globalAny = globalThis; globalAny.__JEST_METADATA_SANDBOX__ = enabled ? false : undefined; } exports.detectDuplicateRealms = detectDuplicateRealms; function getSandboxedRealm() { const globalAny = globalThis; const realm = globalAny.__JEST_METADATA__; if (realm && globalAny.__JEST_METADATA_SANDBOX__ === false) { utils_1.logger.warn('Detected duplicate jest-metadata package in the same process. This may lead to unexpected behavior.'); } return realm; } exports.getSandboxedRealm = getSandboxedRealm; function getServerId() { return process.env.JEST_METADATA_SERVER; } exports.getServerId = getServerId; function getClientId() { return process.env.JEST_METADATA_SERVER + '.' + (process.env.JEST_WORKER_ID || '0'); } exports.getClientId = getClientId; //# sourceMappingURL=detect.js.map