-
Notifications
You must be signed in to change notification settings - Fork 142
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
Verifying stubs that return promises without warnings? #245
Comments
You can ignore warnings. The setting will persist throughout the whole test. const td = require("testdouble");
td.config({ ignoreWarnings: true });
td.reset() // only resets state, not configurations
// `ignoreWarnings` is still set to true |
This comes up often enough that we should probably figure something out. I continue to not relate well to this need because I don't have it myself, but I can definitely buy that there should be a way to alter the test double such that it returns a null-resolved Promise as opposed to a |
The tricky thing will be to figure out how to do this without muddying up the API. There currently isn't a way to configure a td function. Perhaps the best thing to do would be—as opposed to changing the public API—to instead internally identify a |
An idea I just had - could we have either a flag on the td.func() constructor (something like Or is that muddying up the api? |
I don't think the setting you're looking for is
|
That reads fine to me, I'm not wedded to |
I think @christav's approach is better here. I feel like the td.func('some name', {defaultReturn: 'lol')}) It'd better to just have the Having the async equivalents might be easier to do as you don't have to modify the api |
Well, the reason tdjs currently doesn't offer this at all is because I agree with you, but enough people have stumbled over promise-happy APIs that I think addressing it makes sense. As for:
So would an |
I'm a little confused by your statement, "not all async functions return promises". |
The point I'm trying to argue is that if you want the promise to resolve to something other than |
Right, but colloquially, a function that takes a callback and returns nothing is also referred to as "async". I don't want to confuse anyone by adding a special keyword that means "async keyword function but not callback API function" |
Ah okay, I accidentally assumed you were referring to functions that were using the async keyword and not just asynchronous functions in general. I personally think using the word async could be fine as We could make the default return value parameter an enum of sorts, that defaults to a normal function. So Ex: // a function that returns undefined
td.func('some name');
// a function that returns a resolved promise
td.func('some name', {type: td.func.PROMISE});
// a function that calls the last argument passed
td.func('some name', {type: td.func.CALLBACK}); This way the api is expandable but there are limited and well defined "entry points" |
@searls hate to be a bother, but any thoughts? I'd be happy to assist with implementation once the interface is set |
I would support a PR that created a More broadly, since we have so many ways to create test doubles ( |
Closing in favor of #372 |
If I'm stubbing out a function that returns a promise, I need to set it up with a
td.when
. Then I usually do atd.verify
to check that it's getting passed the right stuff.When I call
td.verify
I get the warning print about "both stubbed and verified".In this case I don't actually care about the stub or the return value, I just need it to return a promise (of null) instead of just null.
Would it be possible to define a helper or a flag or something so that when you do a
td.func
you can tell it it's an async function and return a promise for null by default instead of just null? Then we cantd.verify
without warnings, just in an async context.The text was updated successfully, but these errors were encountered: