histoire
Version:
A fast story book powered by Vite
26 lines (25 loc) • 1.19 kB
JavaScript
import { TEST_REGISTRY_KEY } from '@histoire/shared';
const TEST_FLAG_KEY = '__HST_TEST__';
/**
* Installs `registry` as the ambient test registry (and `testing` as the
* ambient test flag) for the duration of `fn`, then restores the previous
* values. Story setup code registers tests by pushing onto the global, so this
* is the only channel through which a mount phase's registrations are captured.
*
* The globals are shared process-wide, which is why session work is serialized
* (see `createExclusiveQueue`) instead of running concurrently.
* @param registry Array collecting the registrations emitted by `fn`.
* @param testing Value of the ambient `__HST_TEST__` flag during `fn`.
* @param fn The work to run with the registry installed.
*/
export function withRegistry(registry, testing, fn) {
const previousRegistry = globalThis[TEST_REGISTRY_KEY];
const previousFlag = globalThis[TEST_FLAG_KEY];
globalThis[TEST_REGISTRY_KEY] = registry;
globalThis[TEST_FLAG_KEY] = testing;
return Promise.resolve().then(fn).finally(() => {
;
globalThis[TEST_REGISTRY_KEY] = previousRegistry;
globalThis[TEST_FLAG_KEY] = previousFlag;
});
}