Skip to main content

Mock Functions

Mock functions allow stubbing and/or inspecting functions when testing. This is especially used when unit testing, where you'll likely need to mock unrelated units of your code or code from other packages or libraries.allow

You can create a mock function with lest.fn(). If no implementation is given, the mock function will return nil when called.

lest.Mock

Class representing mocked functions.

mockFn.mock.calls: any[][]

Array of calls, where each call is an array of the arguments that were passed to the mock function.

mockFn.mock.lastCall?: any[]

Array of arguments that the mock function was last called with, or nil if it hasn't been called.

mockFn.mock.results: lest.MockResult[]

Array of results that the mock function has returned or thrown.

mockFn.mock.lastResult?: lest.MockResult

The result that the mock function last returned or threw, or nil if it hasn't been called.

mockFn:mockName(name: string): self

Sets the name that will be used for the mock in test output (or other prints).

mockFn:getMockName(): string

Gets the name that will be used for the mock in test output (or other prints).

mockFn:mockImplementation(fn: fun(...: any): any): self

Mocks the function's implementation. This is the same as passing a function to lest.fn(), and will override any function that was passed there.

If you've pushed a one-time implementation with mockImplementationOnce, then this function will not override it. This is because the persistent implementation is a fallback and stored separately.

mockFn:getMockImplementation(): fun(...: any): any

Gets the mock function's implementation. This is the function that's called whenever the mock is called.

If you've pushed a one-time implementation with mockImplementationOnce, then this function will still return the persistent implementation. This is because the persistent implementation is a fallback and stored separately.

mockFn:mockImplementationOnce(fn: fun(...: any): any): self

Mocks the function's implementation for one call. The one-time implementations will be consumed in the order they were given (first in, first out).

getMockImplementation will still return the persistent implementation regardless of any one-time implementations pushed. This is because the persistent implementation is a fallback and stored separately.

mockFn:mockReturnValue(...: any): self

Mocks the function's return value. This is shorthand for mockFn:mockImplementation(function() return ... end)

See mockImplementation's documentation for more information.

mockFn:mockReturnValueOnce(...: any): self

Mocks the function's return value for a single call. This is shorthand for mockFn:mockImplementationOnce(function() return ... end)

See mockImplementationOnce's documentation for more information.

mockFn:mockReturnThis(...: any): self

Mocks the function's implementation to return self. This is shorthand for mockFn:mockImplementation(function(self) return self end)

This is useful for mocking functions that chain. See mockImplementation's documentation for more information.

mockFn:mockClear()

Clears all information stored in the mockFn.mock table (calls and results).

Note that this replaces mockFn.mock, not its contents. Make sure you're not storing a stale reference to the old table.

To also reset mocked implementations and return values, call mockFn:mockReset().

mockFn:mockReset()

Clears all information stored in the mockFn.mock table (calls and results) and removes all mocked implementations and return values.

Note that this replaces mockFn.mock, not its contents. Make sure you're not storing a stale reference to the old table.

To only reset the calls and results of the mock, use mockFn:mockClear() instead.

lest.MockResult

When a mock function returns or throws, an instance of lest.MockResult is added to the mock's data containing information about the result.

mockResult.type: "return" | "throw"

  • If the mock function ran successfully when it was last called, then this will be "return".
  • If the function threw an error, then this will be "throw".

mockResult.value: any

  • If the result type is "return", then this will be an array of the values returned by the mock function.
  • If the result type is "throw", then this will be the error that was thrown.