Feature Request: Expose return values of calls to mock functions
See original GitHub issueDo 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
Issue Analytics
- State:
- Created 6 years ago
- Reactions:1
- Comments:5 (3 by maintainers)
Top GitHub Comments
I think that would be a nice feature. PR welcome! 🙂
This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.