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

Add defer() for testing deferred promise resolution #141

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

johanblumenberg
Copy link
Contributor

Useful utility to defer promise resolution:

let d = defer<number>();
when(obj.method()).thenReturn(d); // Return a promise that is not resolved yet

d.resolve(1); // Later, the promise is resolved or rejected

Controlling the order in which promises are resolved or rejected is very useful in unit testing, but you should avoid this kind of constructs in production code, and use the Promise constructor instead. So this fits very well into a mocking framework. When choosing between using any of the available libraries or to write the few lines of code in ts-mockito, I think it is better to add the few lines of code, also since it is easier to document how to perform this kind of test if you don't rely on any other library for the documentation.

The test code does not compile, it depends on these PRs:

@NagRock NagRock closed this Jul 6, 2021
@NagRock NagRock reopened this Jul 6, 2021
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

Successfully merging this pull request may close these issues.

None yet

2 participants