Skip to main content
Module

x/unitest/mod.ts>MockObject

🃏 Deno-first universal unit testing framework
Latest
interface MockObject
Re-export
import { type MockObject } from "https://deno.land/x/unitest@v1.0.0-beta.82/mod.ts";

Type Parameters

optional
A extends readonly unknown[] = any[]
optional
R = unknown

Call Signatures

(...args: A): R

Properties

mock: Pick<MockSpec, "calls" | "results" | "callOrderNumbers">

Methods

defaultImplementation(implementation: (...args: A) => R): MockObject<A, R>

Sets the mock function as default. The set function will be called when the mock object is called.

import { expect, fn, test } from "https://deno.land/x/unitest@$VERSION/mod.ts";

test("should define implementation as default", () => {
  const mockObject = fn().defaultImplementation(() => true);
  expect(mockObject()).toBe(true);
});
defaultReturnValue(value: R): MockObject<A, R>

Sets default as return value. The set value will be return when the mock object is called.

import { expect, fn, test } from "https://deno.land/x/unitest@$VERSION/mod.ts";

test("should define return value as default", () => {
  const mockObject = fn(() => 1).defaultReturnValue(0);
  expect(mockObject()).toBe(0);
});
defaultResolvedValue(value: R): MockObject<A, Promise<R>>

Sets default as resolved value. The set value will be Promised and return when the mock object is called.

import { expect, fn, test } from "https://deno.land/x/unitest@$VERSION/mod.ts";

test("should define resolved value as default", () => {
  const mockObject = fn().defaultResolvedValue(1);
  expect(mockObject()).toEqual(Promise.resolve(1));
});
defaultRejectedValue(value: R): MockObject<A, Promise<R>>

Sets default as rejected value. The set value will be Promised and return when the mock object is called.

import { expect, fn, test } from "https://deno.land/x/unitest@$VERSION/mod.ts";

test("should define rejected value as default", () => {
  const mockObject = fn().defaultRejectedValue(Error("error"));
  expect(mockObject() as Promise<never>).rejects.toEqual(Error("error"));
});
onceImplementation(implementation: (...args: A) => R): MockObject<A, R>

Sets a mock function to be called only once. This takes precedence over the default mock function. If there is more than one once implementation, they will be called in the order of registration.

import { expect, fn, test } from "https://deno.land/x/unitest@$VERSION/mod.ts";

test("should define implementation as only once", () => {
  const mockObject = fn(() => 0).onceImplementation(() => 1);
  expect(mockObject()).toBe(1);
  expect(mockObject()).toBe(0);
});
onceReturnValue(value: R): MockObject<A, R>

Sets a mock function what return specific value to be called only once. This takes precedence over the default mock function. Follow the FIFO.

import { expect, fn, test } from "https://deno.land/x/unitest@$VERSION/mod.ts";

test("should define return value as only once", () => {
  const mockObject = fn(() => 1).onceReturnValue(0);
  expect(mockObject()).toBe(0);
  expect(mockObject()).toBe(1);
});
onceResolvedValue(value: R): MockObject<A, unknown>

Sets a mock function what return specific Promise value to be called only once. This takes precedence over the default mock function. Follow the FIFO.

import { expect, fn, test } from "https://deno.land/x/unitest@$VERSION/mod.ts";

test("should define resolved value as only once", () => {
  const mockObject = fn().onceResolvedValue(2).defaultReturnValue(1);
  expect(mockObject()).toEqual(Promise.resolve(2));
  expect(mockObject()).toBe(1);
});
onceRejectedValue(value: R): MockObject<A, unknown>

Sets a mock function what return specific Promise.reject value to be called only once. This takes precedence over the default mock function. Follow the FIFO.

import { expect, fn, test } from "https://deno.land/x/unitest@$VERSION/mod.ts";

test("should define rejected value as only once", async () => {
  const mockObject = fn().onceRejectedValue(Error("test"));
  await expect(mockObject() as Promise<never>).rejects.toEqual(Error("test"));
  expect(mockObject()).not.toBeDefined();
});
mockClear(): MockObject<A, R>

Resets stored in the mockObject.mock. Often this is useful when you want to clean up a mocks usage data between two assertions.

import { expect, fn, test } from "https://deno.land/x/unitest@$VERSION/mod.ts";

test("should clear mock", () => {
  const mockObject = fn(() => 1);
  mockObject();

  expect(mockObject).toHaveReturnedWith(1);
  mockObject.mockClear();
  expect(mockObject).not.toHaveReturnedWith(1);
});
reset(): MockObject<A, R>

Resets stored in the mockObject.mock and also removes any mocked return values or implementations. This is useful when you want to completely reset a mock back to its initial state.

import { expect, fn, test } from "https://deno.land/x/unitest@$VERSION/mod.ts";

test("should clear mock and all registered once implementations and default", () => {
  const mockObject = fn(() => 1);
  mockObject();

  expect(mockObject).toHaveReturnedWith(1);

  mockObject.reset();
  expect(mockObject).not.toHaveBeenCalled();

  mockObject();
  expect(mockObject).toHaveReturnedWith(undefined);
});