ES6 Promises in Mocha

I use this polyfill for ES6 promises and Mocha / Chai.

My statements for promises do not work. The following is an example test:

it('should fail', function(done) {
    new Promise(function(resolve, reject) {
        resolve(false);
    }).then(function(result) {
        assert.equal(result, true);
        done();
    }).catch(function(err) {
        console.log(err);
    });
});

When I run this test, it fails due to a timeout. The confirmation error that was selected in the then block falls into the catch block. How can I avoid this and just throw it directly to Moke?

I could just throw it out of the catch function, but how can I make statements for a catch block?

+4
source share
3 answers

I decided to solve my problem using Chai as Promised .

promises:

  • return promise.should.become(value)
  • return promise.should.be.rejected
+3

, . Mocha , , , Promise ( ).

console.log(err); done(err);. Mocha , .

+4

, Mocha/Chai/es6-prom, :

it('should do something', function () {

    aPromiseReturningMethod(arg1, arg2)
    .then(function (response) {
        expect(response.someField).to.equal("Some value")
    })
    .then(function () {
        return anotherPromiseReturningMethod(arg1, arg2)
    })
    .then(function (response) {
        expect(response.something).to.equal("something")
    })
    .then(done).catch(done)

})

, Mocha .

, -, noop() * then catch:

it('should do something', function () {

    aPromiseReturningMethod(arg1, arg2)
    .then(function (response) {
        expect(response.someField).to.equal("Some value")
    })
    .then(function () {
        return anotherPromiseReturningMethod(arg1, arg2)
    })
    .then(_.noop).then(done).catch(done)

})

* Lodash noop().

I would like to hear any criticisms of this pattern.

+2
source

All Articles