Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Test if a promise is resolved or rejected with Jasmine in Nodejs

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.

like image 981
chepukha Avatar asked Nov 27 '14 06:11

chepukha


People also ask

How do you deal with the promise in Jasmine?

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.

What happens if promise is not resolved or rejected?

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.


1 Answers

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) }) 
like image 186
nxmohamad Avatar answered Oct 02 '22 13:10

nxmohamad