Skip to content

Add defer() for testing deferred promise resolution#141

Open
johanblumenberg wants to merge 1 commit intoNagRock:masterfrom
johanblumenberg:defer
Open

Add defer() for testing deferred promise resolution#141
johanblumenberg wants to merge 1 commit intoNagRock:masterfrom
johanblumenberg:defer

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.

2 participants