3

If the promise itself is rejected as in the 'does fail' test, then the test fails as I would expect. If an assertion fails/error is thrown/promise is rejected in the "then" of the promise I get logging saying ERROR: 'Unhandled promise rejection' and the test passes anyway. How do I get it to fail on rejection rather than log that the rejection was unhandled?

import { expect } from 'chai';

describe.only('What?', () => {
  const e = new Error('NOPE');

  it('does fail', () => Promise.reject(e));

  it('should fail when rejected', () => {
    const promise = new Promise(r => r());

    promise.then(() => Promise.reject(e));

    return promise;
  });

  it('should fail when thrown, then caught then rejected', () => {
    const promise = new Promise(r => r());

    promise
      .then(() => { throw e; })
      .catch(() => Promise.reject('huh'));

    return promise;
  });

  it('should fail/reject when thrown, then caught then rethrown', () => {
    const promise = new Promise(r => r());

    promise
      .then(() => { throw e; })
      .catch(er => { throw er; });

    return promise;
  });

  it(`doesn't matter if I expect`, () => {
    const promise = new Promise(r => r());

    promise.then(() => {
      expect(1).to.eq(2);
    });

    return promise;
  });
});

Which then reports...

START:
  What?
    ✖ does fail
    ✔ should fail when rejected
ERROR: 'Unhandled promise rejection', Error{stack: undefined}
    ✔ should fail when thrown, then caught then rejected
ERROR: 'Unhandled promise rejection', 'huh'
    ✔ should fail/reject when thrown, then caught then rethrown
ERROR: 'Unhandled promise rejection', Error{stack: undefined, line: 47567, sourceURL: 'http://localhost:9876/base/test/test_index.js?7f696b0b50c0a51c7a2fa5278582072b20241a3b'}
    ✔ doesn't matter if I expect
ERROR: 'Unhandled promise rejection', AssertionError{message: 'expected 1 to equal 2', showDiff: true, actual: 1, expected: 2, stack: 'AssertionError@http://localhost:9876/base/test/test_index.js?7f696b0b50c0a51c7a2fa5278582072b20241a3b:39222:25
Ryan Swanson
  • 33
  • 1
  • 5
  • I don't see any usage of done() – chchrist Aug 30 '16 at 17:06
  • is the code above from mocha or jasmine? – chchrist Aug 30 '16 at 17:07
  • 1
    ... and any `expect`. – Hamlet Hakobyan Aug 30 '16 at 17:07
  • This is using mocha, and expects, asserts, shoulds all behave the same. I reduced the code down to the simplest, I could so you could see just throwing or rejecting a promise is the issue not a particular assertion library. Also, I shouldn't be using done with promises, just returning the promise and letting the framework pass/fail on resolution/rejection. – Ryan Swanson Aug 30 '16 at 17:12
  • when working with async functions in karma, mocha, etc, you can usually add a `done` callback to the parameters and call it in your function to complete the test with the relevant results. I often have tests that end like `.then(done).catch(done)` which allows the done callback to receive an error if there is one and pass if there isn't. Here's an example with Jasmine http://jasmine.github.io/2.4/introduction.html#section-Asynchronous_Support – deltree Aug 30 '16 at 18:10

1 Answers1

4

You have to return the return value of the then(...), not the first promise you create.

Antoine Leclair
  • 17,540
  • 3
  • 26
  • 18