Function mockSession

  • Creates a session that tracks all mocks created before it's restored. If a callback is provided, it restores all mocks created within it.

    Returns number

    The id of the created session.

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

    const setTimeout = globalThis.setTimeout;
    const id = mockSession();

    stub(globalThis, "setTimeout");

    assertNotEquals(globalThis.setTimeout, setTimeout);

    restore(id);

    assertEquals(globalThis.setTimeout, setTimeout);
  • Creates a session that tracks all mocks created before it's restored. If a callback is provided, it restores all mocks created within it.

    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) => Return)

    The function to execute the session.

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

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

    session();

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