Skip to content

Feature Request: Expose return values of calls to mock functions #5738

@UselessPickles

Description

@UselessPickles

Do you want to request a feature or report a bug?
Feature

What is the current behavior?
There is currently no way to access the return values that were returned by calls to mock functions.

What is the expected behavior?
When using jest.mock() to simply wrap a method for the purpose of "spying" on calls to the method (check if it's called, inspect the params it was called with, etc), it would also be very useful to have access to the list of return values that have been returned by calls to the mock function. This would be equivalent to sinon's spy.returnValues: http://sinonjs.org/releases/v2.0.0/spies/

The basic design of this would involve adding "returnValues" to MockFunctionState as such:

type MockFunctionState = {
  instances: Array<any>,
  calls: Array<Array<any>>,
  returnValues: Array<any>,
  timestamps: Array<number>,
};

And updating _makeComponent to push the final returned value onto the new returnValues array.

(see https://github.com/facebook/jest/blob/master/packages/jest-mock/src/index.js)

If there are no major "gotchas" to this plan, then I would volunteer to create a PR with these changes.

Please provide your exact Jest configuration and mention your Jest, node,
yarn/npm version and operating system.

Irrelevant

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions