mockSession

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

function

function mockSession(): number

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

The id of the created session.

Example: Usage

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

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

stub(globalThis, "setTimeout");

assertNotEquals(globalThis.setTimeout, setTimeout);

restore(id);

assertEquals(globalThis.setTimeout, setTimeout);

function

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

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

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

The function to be used for the created session.

Returns

The function to execute the session.

Example: Usage

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

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

session();

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

function

function mockSession<Self, Args extends unknown[], Return>(func?: (this: Self, ...args: Args) => Return): number | ((this: Self, ...args: Args) => Return)

Source