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

Awaiting a testdouble function always resolves #371

Closed
3 tasks done
lumaxis opened this issue May 18, 2018 · 4 comments
Closed
3 tasks done

Awaiting a testdouble function always resolves #371

lumaxis opened this issue May 18, 2018 · 4 comments

Comments

@lumaxis
Copy link
Contributor

lumaxis commented May 18, 2018

Description

I'm trying to verify a function that calls several APIs but otherwise produces no output. I'm passing in the API client objects as parameters so they are easily replaceable with testdoubles.

Issue

When using async/await and awaiting any testdouble function, the function always returns undefined which makes the code continue. I would have expected the function to only ever resolve if I explicitly set that up with td.when(…).thenResolve().

Is this intentional behavior and is there already a proposed way to solve my use case? The only workaround I can think of right now is explicitly throwing an error when my testdouble is called with the wrong parameters.

Environment

  • node -v output: 8.9.4
  • npm -v (or yarn --version) output: 6.0.0
  • npm ls testdouble (or yarn list testdouble) version:

Code-fenced Examples

const td = require('testdouble');

const func = async () => {
  await td.function('my func')();

  console.error('This should never be reached')
}

func();
@searls
Copy link
Member

searls commented May 21, 2018

By default, td.func()() will return undefined, not an unfulfilled promise, so I don't think there'd be anything to await there. (I haven't used async/await much yet, so I don't know what happens when the thing to the right of await doesn't evaluate to a promise.)

You might try this:

const lol = td.when(td.func()).thenReturn(new Promise())

await lol()

??

@neall
Copy link
Member

neall commented May 21, 2018

To answer Justin's implied question, from the MDN page on await:

If the value of the expression following the await operator is not a Promise, it's converted to a resolved Promise.

That's why the default behavior of td.func()() returning undefined is the same when you await as if it had returned a promise resolved with undefined.

So if you want the result of the function to fulfill with a value other than undefined, you can do something like:

const myFunc = td.when(td.func()).thenResolve('my value')

If you want it to reject:

const myFunc = td.when(td.func()).thenReject(Error('boom!'))

But it sounds like you want it to return an un-resolved promise but have control over when it resolves later, so you would need something like:

let myResolve
const myPromise = new Promise((resolve) -> myResolve = resolve)
const myFunc = td.when(td.func()).thenReturn(myPromise)

@searls
Copy link
Member

searls commented May 21, 2018

Closing in favor of #372

@searls searls closed this as completed May 21, 2018
@lumaxis
Copy link
Contributor Author

lumaxis commented May 21, 2018

Thanks @neall, I think that's exactly the behavior I want - even though that would be a bit clunky 🙂
Having something like what Justin suggested in #372 definitely sounds like the best long-term solution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants