chepukha chepukha - 1 year ago 128
Javascript Question

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
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?


Answer Source

To test asynchronous code with jasmine you should use its async syntax, e.g.:

describe('test promise with jasmine', function(done) {
    var promise = getRejectedPromise();

    promise.then(function() {
      // Promise is resolved
      done(new Error('Promise should not be resolved'));
    }, function(reason) {
      // Promise is rejected
      // You could check rejection reason if you want to
      done(); // Success