Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
UselessPickles opened this issue Mar 6, 2018 · 5 comments · Fixed by #5752
Closed

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

UselessPickles opened this issue Mar 6, 2018 · 5 comments · Fixed by #5752

Comments

@UselessPickles
Copy link
Contributor

UselessPickles commented Mar 6, 2018

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

@SimenB
Copy link
Member

SimenB commented Mar 6, 2018

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

@UselessPickles
Copy link
Contributor Author

I'll work on this in the next few days. I suspect the code change itself will be fairly easy, and the bulk of the effort will be updating unit tests to thoroughly verify it.

@UselessPickles
Copy link
Contributor Author

Can anyone help me out with this error when running yarn test?

D:\DevProjects\github\jest>yarn test
yarn run v1.5.1
$ yarn typecheck && yarn lint && yarn jest
$ flow check --include-warnings
Found 0 errors
$ eslint . --cache --ext js,md

D:\DevProjects\github\jest\integration-tests\transform-linked-modules\ignored\symlink.js
  1:1  error  Parsing error: Unexpected token

> 1 | ../package/index.js
    | ^

✖ 1 problem (1 error, 0 warnings)

It seems completely unrelated to the changes I'm making. Here's the branch with my changes: https://github.com/UselessPickles/jest/tree/mock_return_values

@UselessPickles
Copy link
Contributor Author

Hmmm... there's also several unit tests failing in master. I guess the build is generally a bit broken even before I made any changes.

@github-actions
Copy link

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.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 12, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants