-
-
Notifications
You must be signed in to change notification settings - Fork 733
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
Replies as Promises #1557
Comments
Thanks for this! I might be misunderstanding, though it is possible to specify replies using an error-first callback: https://github.com/nock/nock#specifying-replies I think support could be added there for promise-returning functions, without making a breaking change. The code could check the return value, and if it's a promise, await it, and if not, wait for the callback to be called. Does that make sense? Are you interested in taking it on? A contribution would be welcome! |
I believe the correct way to do this would be to replace this line with the following. Line 322 in 900d96d
Promise.resolve(replyResponseBody)
.then(body => continueWithResponseBody(null, body))
.catch(err => continueWithResponseBody(err)); This would allow the callback that accepts two args to return either data for the body directly (current behavior) or a Promise that resolves to the data. A similar change would also be required in the next block that deals with This will mean adding an extra tick to the |
Hmmm, I wonder if we can flip around the implementations the other way, and base them around I feel like that chain of functions would become easier to follow using |
Context
Writing jest mocks for a got client (eg.
got.extend()
), was looking into dynamically loaded fixtures as replies. Found out that promises and async/await functions where not handled properly.Alternatives
Force the code to be synchronous (doable in my case with the
fs
package).Has the feature been requested before?
Did not found it
If the feature request is accepted, would you be willing to submit a PR?
Yes, added unit tests already, but had a look at the code and did not found it trivial to add it. Found that there was no async handling (only setting
this.body
) and did not really found out where thePromise.resolve()
could fit, any guidance/ideas would be great.Example
A few tests:
The text was updated successfully, but these errors were encountered: