Function mockSessionAsync

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

    Type Parameters

    • Self

      The self type of the function.

    • Args extends unknown[]

      The arguments type of the function.

    • Return

      The return type of the function.

    Parameters

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

    The return value of the function.

    import { mockSessionAsync, restore, stub } from "@std/testing/mock";
    import { assertEquals, assertNotEquals } from "@std/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