question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Feature Request: Expose return values of calls to mock functions

See original GitHub issue

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

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:1
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
SimenBcommented, Mar 6, 2018

I think that would be a nice feature. PR welcome! 🙂

0reactions
github-actions[bot]commented, May 12, 2021

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Mock Functions - Jest
Mock functions allow you to test the links between code by erasing the actual implementation of a function, capturing calls to the function...
Read more >
Mocking calls with Jasmine - Volare Software
Jasmine uses spies to mock asynchronous and synchronous function calls. ... Here, I show setting the return value of a function so we...
Read more >
API Docs | fetch-mock - Wheresrhys
fetch-mock allows mocking http requests made using fetch or a library imitating its api, such as node-fetch or fetch-ponyfill.
Read more >
An Introduction to Mocking in Python
You still get your mock auto-created in exactly the same way as before. What it means though, is that if you use it...
Read more >
Setting up mock servers
A mock server simulates the behavior of a real API server by accepting requests and returning responses. By adding a mock server to...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found