chaijs / chai-as-promised

Extends Chai with assertions about promises.
MIT License
1.42k stars 112 forks source link

Does not work well with bluebird. #271

Open mrdulin opened 3 years ago

mrdulin commented 3 years ago

Related issue: https://github.com/domenic/chai-as-promised/issues/169

chai-as-promised does not work with Promise.delay API of bluebird.

Mocha will give me timeout error:

  test
    1) should work

  0 passing (5s)
  1 failing

  1) test
       should work:
     Error: Timeout of 5000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves. (/Users/ldu020/workspace/github.com/mrdulin/expressjs-research/src/stackoverflow/35381245-todo/index.test.js)

Here is a minimal, reproducibe example:

const sinon = require('sinon');
// const Promise = require('bluebird');
// global.Promise = require('bluebird');
const chai = require('chai');
const chaiAsPromised = require('chai-as-promised');
chai.use(chaiAsPromised);

chai.should();

Promise.delay = function (ms, value) {
  return new Promise((resolve) => setTimeout(() => resolve(value), ms));
};

describe('test', function () {
  var clock;

  beforeEach(function () {
    clock = sinon.useFakeTimers();
  });

  afterEach(function () {
    clock.restore();
  });

  it('should work', async function () {
    var promise = Promise.delay(10000, 'foo');
    clock.tick(10000);
    await promise.should.eventually.deep.equal('foo');
  });
});

If I create my own Promise.delay method with native promise, it works fine with chai-as-promised and sinon.useFakeTimers().

But, if I use the Promise.delay method of bluebird, it won't work. No matter whether you use const Promise = require('bluebird'); or global.Promise = require('bluebird');, both of them do not work

Environment information: