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

Adding async versions of mockReturnValue & mockreturnValueOnce #5318

Merged
merged 5 commits into from
Jan 22, 2018

Conversation

m4n3z40
Copy link
Contributor

@m4n3z40 m4n3z40 commented Jan 15, 2018

Summary

I find myself doing the following very often when testing async code:

// ...
const asyncFn = jest.fn().mockReturnValue(Promise.resolve(result)):

// or...

const asyncFn = jest.fn().mockReturnValue(Promise.reject(err)):
// ...

This PR adds four utility methods to MockInstance, automating the creation of async mock functions:

// ...
const asyncFn = jest.fn().mockResolvedValue(result):

// and...

const asyncFn = jest.fn().mockResolvedValueOnce(result):

// and...

const asyncFn = jest.fn().mockRejectedValue(result):

// and...

const asyncFn = jest.fn().mockRejectedValueOnce(result):

// ...

Test plan

NA (Not a bug)


This PR means to gather info on usage first, if people find this useful I'll continue to add the docs to this feature and updating the changelog as needed.

Thanks!

@codecov-io
Copy link

codecov-io commented Jan 15, 2018

Codecov Report

Merging #5318 into master will increase coverage by 0.03%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #5318      +/-   ##
==========================================
+ Coverage   61.29%   61.33%   +0.03%     
==========================================
  Files         205      205              
  Lines        6917     6924       +7     
  Branches        3        3              
==========================================
+ Hits         4240     4247       +7     
  Misses       2676     2676              
  Partials        1        1
Impacted Files Coverage Δ
packages/jest-mock/src/index.js 86.49% <100%> (+0.31%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 36c705b...98ed094. Read the comment docs.

@SimenB
Copy link
Member

SimenB commented Jan 21, 2018

@m4n3z40 Thanks for the PR, and sorry about the slow review! I started writing it, but I just ended up fixing my code nit in order to ensure it worked like I wanted. I rebased your branch and added my change, the assertions read a bit nicer this way 🙂

Copy link
Member

@SimenB SimenB left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please update the changelog and mock documentation.

@m4n3z40
Copy link
Contributor Author

m4n3z40 commented Jan 22, 2018

Awesome. Wasn't aware that all jest features were available in it's own test suite. Very neat, thanks.

Added changes to CHANGELOG and docs.

@m4n3z40
Copy link
Contributor Author

m4n3z40 commented Jan 22, 2018

Just noticed eslint is failing because of the async examples I've added. :(

@cpojer cpojer merged commit 139f976 into jestjs:master Jan 22, 2018
@m4n3z40 m4n3z40 deleted the feat/async-mock-fns branch January 24, 2018 02:49
@akira28
Copy link

akira28 commented Jan 25, 2018

The doc about mockFn.mockRejectedValueOnce(value) says that is sugar for jest.fn().mockReturnValueOnce(Promise.resolve(value));
Shouldn't be the sugar for jest.fn().mockReturnValueOnce(Promise.reject(value)); ?

@SimenB
Copy link
Member

SimenB commented Jan 25, 2018

Fixed in #5373, isn't it?

@akira28
Copy link

akira28 commented Jan 25, 2018

uups, didn't see it :)

@SimenB
Copy link
Member

SimenB commented Jan 25, 2018

No worries, thanks for checking!

@github-actions
Copy link

This pull request 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 this pull request may close these issues.

6 participants