-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
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
Comments
Are you willing to send a PR with the wording? 🙂 |
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;
}
} |
That didn't work, but sent a PR for it, #3354 |
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. |
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 ofundefined
.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.
The text was updated successfully, but these errors were encountered: