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

jest.spyOn() provide a way to inspect the return value #3821

Closed
evan-scott-zocdoc opened this issue Jun 14, 2017 · 5 comments · Fixed by #5879
Closed

jest.spyOn() provide a way to inspect the return value #3821

evan-scott-zocdoc opened this issue Jun 14, 2017 · 5 comments · Fixed by #5879

Comments

@evan-scott-zocdoc
Copy link

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

What is the current behavior?
when something is spied upon, you can see invocation arguments, but not what the return value was

If the current behavior is a bug, please provide the steps to reproduce and either a repl.it demo through https://repl.it/languages/jest or a minimal repository on GitHub that we can yarn install and yarn test.

What is the expected behavior?
something like:

jest.spyOn(window, 'getComputedStyle');

// something

expect(window.getComputedStyle).toHaveReturned(
  expect.objectContaining({
    overflow: 'hidden',
  });
);

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

@maierson
Copy link

What is the current way of inspecting return values from spied methods in jest (other than sticking with sinon) ? This is pretty important. I started migrating our tests to jest a few days ago and just ran into it. I'm looking for the equivalent to this (couldn't find with google):

const spy = sinon.spy(instance, "method");
instance.method();
expect(spy.returnValues[0]).to.be.........

@SimenB
Copy link
Member

SimenB commented Jun 22, 2017

I don't think it's currently possible, at least not mentioned in the docs: https://facebook.github.io/jest/docs/mock-function-api.html

@maierson
Copy link

I thought so. Thank you.

@tfiechowski
Copy link

tfiechowski commented Feb 11, 2021

If anyone lands here in 2021: it's also possible with mockFn.mock.results. For example:

const apiSpy = jest.spyOn(api, 'list');
const getApiResult = (): Promise<any> => apiSpy.mock.results[0].value;

/* execution/component rendering */

await waitFor(async () =>
  expect(await getApiResult()).toEqual(/* expected result */),
);

@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 10, 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.

5 participants