最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - ES6 Promises in Mocha - Stack Overflow

programmeradmin0浏览0评论

I'm using this polyfill for ES6 promises and Mocha / Chai.

My assertions for the promises are not working. The following is a sample 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 timeout. The assertion failure that was thrown in the then block is caught in the catch block. How can I avoid this and just throw it straight to Mocha?

I could just throw it from the catch function, but then how would I make assertions for the catch block?

I'm using this polyfill for ES6 promises and Mocha / Chai.

My assertions for the promises are not working. The following is a sample 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 timeout. The assertion failure that was thrown in the then block is caught in the catch block. How can I avoid this and just throw it straight to Mocha?

I could just throw it from the catch function, but then how would I make assertions for the catch block?

Share Improve this question edited Mar 2, 2015 at 4:13 thefourtheye 240k53 gold badges465 silver badges500 bronze badges asked Mar 2, 2015 at 4:12 connorbodeconnorbode 3,1592 gold badges30 silver badges32 bronze badges 5
  • 1 Did you check this? – thefourtheye Commented Mar 2, 2015 at 4:15
  • shouldn't your catch be, console.log(err);done(err);? – mido Commented Mar 2, 2015 at 4:42
  • @thefourtheye promise assertions are what i'm looking for. Thanks, that's a good resource. – connorbode Commented Mar 2, 2015 at 16:11
  • @mido22 the issue then is making assertions about the rejections that are made by the promise. But, as thefourtheye said above, Mocha apparently has this feature built in. – connorbode Commented Mar 2, 2015 at 16:13
  • If you're asserting that the promise succeeds and you don't need to assert anything else, you can just return the promise value without calling .then() or .catch(). Does this answer your question? – Nicky McCurdy Commented Mar 7, 2015 at 1:38
Add a ment  | 

3 Answers 3

Reset to default 4

If your Promise has a failure, it will only call your catch callback. As a result, Mocha's done callback is never called, and Mocha never figures out that the Promise failed (so it waits and eventually times out).

You should replace console.log(err); with done(err);. Mocha should automatically display the error message when you pass an error to the done callback.

I ended up solving my problem by using Chai as Promised.

It allows you to make assertions about the resolution and rejections of promises:

  • return promise.should.bee(value)
  • return promise.should.be.rejected

A pattern I use in my Mocha/Chai/es6-promise tests is the following:

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)

})

The last line is odd looking, but calls Mocha's done on success or on error.

One problem is if the last then returns something, I then need to noop()* before both the then and the 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's noop().

Would love to hear any critiques of this pattern.

发布评论

评论列表(0)

  1. 暂无评论