ts-mockito
ts-mockito copied to clipboard
Add defer() for testing deferred promise resolution
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:
- https://github.com/NagRock/ts-mockito/pull/140
- https://github.com/NagRock/ts-mockito/pull/97