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

mockReturnValueOnce ignores calls with undefined (docs should say so) #3320

Closed
airandfingers opened this issue Apr 18, 2017 · 4 comments · Fixed by #3354
Closed

mockReturnValueOnce ignores calls with undefined (docs should say so) #3320

airandfingers opened this issue Apr 18, 2017 · 4 comments · Fixed by #3354

Comments

@airandfingers
Copy link

I have a mock function that I want to return undefined in certain cases, because that's what it actually returns.

I called myMockFn.mockReturnValueOnce(undefined), and was puzzled when I got the default return value instead of undefined.

I see from the relevant code that undefined values aren't supported; they'll always be overridden by the default value.

I'm find with this—I can work around it—but IMO the docs should mention somewhere that undefined values are overridden by a mock function's default implementation.

@thymikee
Copy link
Collaborator

Are you willing to send a PR with the wording? 🙂

@SimenB
Copy link
Member

SimenB commented Apr 23, 2017

That's a bug IMO. It tries to check if you provided a return value, and thinks you didn't. You should be able to mock an empty return from a function.

What about this (untested)?:

diff --git i/packages/jest-mock/src/index.js w/packages/jest-mock/src/index.js
index 9627e65e..9a7d8997 100644
--- i/packages/jest-mock/src/index.js
+++ w/packages/jest-mock/src/index.js
@@ -270,8 +270,11 @@ class ModuleMockerClass {
         // mockImplementationOnce()/mockImplementation() is called after that.
         // use the set return value.
         if (mockConfig.isReturnValueLastSet) {
-          returnValue = mockConfig.specificReturnValues.shift();
-          if (returnValue === undefined) {
+          const {specificReturnValues} = mockConfig;
+
+          if (specificReturnValues.length > 0) {
+            returnValue = specificReturnValues.shift();
+          } else {
             returnValue = mockConfig.defaultReturnValue;
           }
         }

@SimenB
Copy link
Member

SimenB commented Apr 23, 2017

That didn't work, but sent a PR for it, #3354

@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 13, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants