I know how to do it in Mocha but want to know how to do it with Jasmine. I tried this
describe('test promise with jasmine', function() { it('expects a rejected promise', function() { var promise = getRejectedPromise(); // return expect(promise).toBe('rejected'); return expect(promise.inspect().state).toBe('rejected'); }); });
However, the state is always pending
and, of course, the test fails. I couldn't find any example online that I could make it work.
Can someone please help me with this?
Thanks.
Promises. If you can't use async / await or you need more control, you can explicitly return a promise instead. Jasmine considers any object with a then method to be a promise, so you can use either the Javascript runtime's built-in Promise type or a library.
A promise is just an object with properties in Javascript. There's no magic to it. So failing to resolve or reject a promise just fails to ever change the state from "pending" to anything else. This doesn't cause any fundamental problem in Javascript because a promise is just a regular Javascript object.
you can now use expectAsync()
Expecting success:
it('expect result', async () => { ... await expectAsync(someAsyncFunction(goodInput)).toBeResolved(expectedResponse) })
Expecting failure:
it('expect result', async () => { ... await expectAsync(someAsyncFunction(badInput)).toBeRejectedWith(expectedResponse) })
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With