How many times have you been testing something that involves a promise, but you wanted a fake promise, because the important thing is the async workflow, not what the promise does? Have you hastily coded something-up with a setTimeout? Are you annoyed with having to waste time on doing that repeatedly? Be annoyed no more! Ultimate solution is here! And it has 100% test coverage, so it's very reliable :)
If you need a fake promise that resolves in up to N milliseconds and returns a predictable output – Fake Promise does exactly that.
Async/Await:
const fakepromise = require('fakepromise');
const myResult = await fakepromise.promise(2000, "this is returned");
will give you a promise that takes up to 2000 milliseconds to execute and resolves with "this is returned".
Ye olde promises-way:
const fakepromise = require('fakepromise');
const user = {"name" : "micky", role: "mouse"};
fakepromise.promise(2000, user).then( msg => {
console.log(msg);
});
If you are already using Bluebird, the Promise.delay() is almost identical, except it delays by the exact number of milliseconds where we introduce some fun randomness by delaying "up to" the number of milliseconds.