/** * Where `mockServerFn` and `mockMiddleware` keep their mocks, and how long a * mock lasts. * * A mock belongs to the test that registered it, or to the test file when it * was registered outside of a test (at the top of the file or in * `beforeAll`). A test's mocks end with the test. * * Which test is running comes from two places: * * - The async context of the test, where the test runner provides one (Node, * and with it jsdom and happy-dom). A call made by code the test started * then gets that test's mocks, also while other tests run concurrently. * - Without async context (browser mode), the test that started last. That is * right for tests that run one after another. Concurrent tests there pass * their test to `mockServerFn` through the test context, and a call gets the * mock that was registered last. */ /** A running test, as the test runner identifies it. */ export type MockOwner = object /** Set when the test runner has async context. */ let getAsyncOwner: (() => MockOwner | undefined) | undefined /** The tests that have started and not finished, oldest first. */ const runningOwners = new Set() const registries = new Set>() /** @internal Called by the setup file when the runner has async context. */ export function setAsyncOwnerSource(source: () => MockOwner | undefined) { getAsyncOwner = source } /** @internal Called by the setup file before a test runs. */ export function startTest(owner: MockOwner): void { runningOwners.add(owner) } /** @internal Called by the setup file after a test and its hooks have run. */ export function endTest(owner: MockOwner): void { runningOwners.delete(owner) for (const registry of registries) { registry.removeOwner(owner) } } /** The test that code runs for, if it runs for one. */ function currentOwner(): MockOwner | undefined { if (getAsyncOwner) { const owner = getAsyncOwner() return owner && runningOwners.has(owner) ? owner : undefined } // (without async context: the test that started last) let last: MockOwner | undefined for (const running of runningOwners) { last = running } return last } /** @internal The mocks of one kind, by what they replace. */ export class MockRegistry { private fileMocks = new Map() private testMocks = new Map>() constructor() { registries.add(this as MockRegistry) } /** Registers for `owner`, or for the test that runs now, or for the file. */ set(key: TKey, value: TValue, owner = currentOwner()): void { if (!owner) { this.fileMocks.set(key, value) return } // (re-inserted, so that the latest registration comes last) const mocks = this.testMocks.get(owner) ?? new Map() this.testMocks.delete(owner) this.testMocks.set(owner, mocks.set(key, value)) } get(key: TKey): TValue | undefined { const owner = getAsyncOwner?.() if (owner) { const own = this.testMocks.get(owner)?.get(key) if (own !== undefined) { return own } } else { // Without async context, or from work that lost it, a call cannot say // which test made it: the mock a running test registered last answers. let latest: TValue | undefined for (const mocks of this.testMocks.values()) { latest = mocks.get(key) ?? latest } if (latest !== undefined) { return latest } } return this.fileMocks.get(key) } /** Removes the mocks of the test that runs now, or of the file. */ clear(): void { const owner = currentOwner() if (owner) { this.testMocks.delete(owner) } else { this.fileMocks.clear() } } removeOwner(owner: MockOwner): void { this.testMocks.delete(owner) } }