mockSessionAsync

import { mockSessionAsync } from "jsr:@ende/testing/mock";

function

function mockSessionAsync<Self, Args extends unknown[], Return>(func: (this: Self, ...args: Args) => Promise<Return>): (this: Self, ...args: Args) => Promise<Return>

Creates an async session that tracks all mocks created before the promise resolves.

func: (this: Self, ...args: Args) => Promise<Return>

The function.

Returns

The return value of the function.

Example: Usage

import { mockSessionAsync, restore, stub } from "@ende/testing/mock";
import { assertEquals, assertNotEquals } from "@ende/assert";

const setTimeout = globalThis.setTimeout;
const session = mockSessionAsync(async () => {
  stub(globalThis, "setTimeout");
  assertNotEquals(globalThis.setTimeout, setTimeout);
});

await session();

assertEquals(globalThis.setTimeout, setTimeout); // stub is restored

Source