dayuloli
dayuloli

Reputation: 17031

Mocha - How to test for unsettled promise?

I am testing a function that returns a promise. I want to assert that, in certain conditions, the returned promise would never settle (doesn't resolve nor reject).

How can I test this with Mocha?


If I run the following:

describe('under certain conditions', function () {
  let promise;
  beforeEach(function () {
    promise = new Promise((resolve, reject) => {});
  });
  it('should hang forever', function () {
    return promise;
  });
});

I get the following error:

Error: Timeout of 2000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves

Upvotes: 4

Views: 1441

Answers (4)

robertklep
robertklep

Reputation: 203514

Let's start by stating that practically speaking, it's not possible to validate that the promise never settles: at some point you have to decide that it has taken too long for the promise to settle, and assume that it will never settle after that point.

Here's a solution that would place that point at 5 seconds:

it('should hang forever', function() {
  // Disable Mocha timeout for this test.
  this.timeout(0);

  // Wait for either a timeout, or the promise-under-test to settle. If the
  // promise that settles first is not the timeout, fail the test.
  return Promise.race([
    new Promise(resolve => setTimeout(resolve, 5000, 'timeout')),
    promise.then(
      () => { throw Error('unexpectedly resolved') },
      () => { throw Error('unexpectedly rejected') }
    )
  ]);
});

Upvotes: 3

d4nyll
d4nyll

Reputation: 12657

robertklep's answer works, but you'd have to wait for 5 seconds before the test completes. For unit tests, 5 seconds is simply too long.

As you've suggested, you can integrate the lolex library into robertklep's solution, to avoid the wait.

(I am also using a Symbol instead of the string 'timeout', in case your promise resolves, by coincidence, also resolves with the string 'timeout')

import { install } from 'lolex';

describe('A promise', function () {
  let clock;
  before(function () { clock = install() });
  after(function () { clock.uninstall() });

  describe('under certain conditions', function () {
    const resolvedIndicator = Symbol('resolvedIndicator');
    const forever = 600000; // Defining 'forever' as 10 minutes
    let promise;
    beforeEach(function () {
      promise = Promise.race([
        new Promise(() => {}), // Hanging promise
        new Promise(resolve => setTimeout(resolve, forever, resolvedIndicator)),
      ]);
    });
    it('should hang forever', function () {
      clock.tick(forever);
      return promise.then((val) => {
        if (val !== resolvedIndicator) {
          throw Error('Promise should not have resolved');
        }
      }, () => {
        throw Error('Promise should not have rejected');
      });
    });
  });
});

Upvotes: 3

FK82
FK82

Reputation: 5075

You can introduce a race between the never resolving promise and a reference promise with a suitable timeout using Promise.race (MDN):

const p1 = new Promise((resolve, reject) => { });

const p2 = new Promise(function(resolve, reject) {
    setTimeout(resolve, 5 * 1000, 'promise2');
});


Promise.race([p1, p2])
.then(value => { 
  console.log(value); 
});

Upvotes: 1

ImGroot
ImGroot

Reputation: 836

Try this:

describe('under certain conditions', function () {

    let promise;
    beforeEach(function () {
        promise = new Promise((resolve, reject) => {
        // promise.reject();
      });
    });

    it('should hang forever', function (done) {
        const onRejectOrResolve = () => {
            done(new Error('test was supposed to hang'));
        };
        promise
        .then(onRejectOrResolve)
        .catch(onRejectOrResolve);
        setTimeout(() => {
            done();
        }, 1000);
    });

  });

Upvotes: 1

Related Questions