close
logo
Rstest
Guide
Config
API
English
简体中文
Guide
Config
API
English
简体中文
logo
Rstest
Overview

Test API

Expect
Test
Describe
Hooks

Rstest Utility

Mock modules
Mock functions
MockInstance
Fake timers
Utilities
📝 Edit this page on GitHub
Previous PageMock modules
Next PageMockInstance

#Mock functions

Rstest provides some utility functions to help you mock functions powered by tinyspy.

#rstest.fn

  • Alias: rs.fn
  • Type:
export interface Mock<T extends Function> extends MockInstance<T> {
  (...args: Parameters<T>): ReturnType<T>;
}

export type MockFn = <T extends Function>(fn?: T) => Mock<T>;

Creates a spy on a function.

const sayHi = rstest.fn((name: string) => `hi ${name}`);

const res = sayHi('bob');

expect(res).toBe('hi bob');

expect(sayHi).toHaveBeenCalledTimes(1);

#rstest.spyOn

  • Alias: rs.spyOn
  • Type:
export type SpyFn = (
  obj: Record<string, any>,
  methodName: string,
  accessType?: 'get' | 'set',
) => MockInstance;

Creates a spy on a method of an object.

const sayHi = () => 'hi';
const hi = {
  sayHi,
};

const spy = rstest.spyOn(hi, 'sayHi');

expect(hi.sayHi()).toBe('hi');

expect(spy).toHaveBeenCalled();

#rstest.isMockFunction

  • Alias: rs.isMockFunction
  • Type: (fn: any) => fn is MockInstance

Determines if the given function is a mocked function.

#rstest.clearAllMocks

  • Alias: rs.clearAllMocks
  • Type: () => Rstest

Clears the mock.calls, mock.instances, mock.contexts and mock.results properties of all mocks.

#rstest.resetAllMocks

  • Alias: rs.resetAllMocks
  • Type: () => Rstest

Clears all mocks properties and reset each mock's implementation to its original.

#rstest.restoreAllMocks

  • Alias: rs.restoreAllMocks
  • Type: () => Rstest

Reset all mocks and restore original descriptors of spied-on objects.

#More

  • Mock Matchers
  • MockInstance API