1

I suspect based on other responses (like Jasmine: test a setTimeout function throws an error and How to test a function which has a setTimeout with jasmine?) that there's no good solution here, but I want to figure out how to better deal with the second test below:

describe('Tests', function () {
    it('succeed', function () { expect(true).toBeTruthy(); });
    it('have problems', function(done) {
        setTimeout(function() { console.log(undefined.foo); });
        setTimeout(done, 5);
    });

    it('fail', function() { fail; });
    it('also fail', function() { fail; });
});

Jasmine's current behavior here is to run the first test and then bail out when the rogue exception-causing setTimeout is encountered in the second test; the last two failing specs never run.

My use case doesn't look like this, of course! The asynchronous error is happening somewhere down a call stack, over a river, and through the woods. And it's obviously an error that it happens and isn't caught! But it's unfortunate if such errors will always terminate Jasmine itself, rather than causing a test case failure.

Community
  • 1
  • 1
Rob Simmons
  • 151
  • 4

1 Answers1

0

I believe you want try...catch or promises alongside Jasmine's done.fail().

Promises:

it('gets some huge file', done => {
  getFile('http://movies.com/holymountain.mp4')
    .then(res => {
      expect(res.status).toBe(200)
      done()
    })
    .catch(done.fail)
})

Try/catch

it('gets some huge file', done => {
  try {
    getFile('http://movies.com/holymountain.mp4')
    expect(getFile.status).toBe(200)
    done()
  } catch (e) {
    // console.log(e) // ???
    done.fail()
  }
})

Issue for reference

Mitch Lillie
  • 2,217
  • 18
  • 25