Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does one use Q.all with chai-as-promised?

The chai-as-promised docs have the following example of dealing with multiple promises in the same test:

it("should all be well", function (done) {
    Q.all([
        promiseA.should.become("happy"),
        promiseB.should.eventually.have.property("fun times"),
        promiseC.should.be.rejectedWith(TypeError, "only joyful types are allowed")
    ]).should.notify(done);
});

I assume that the Q here has come from npm install q and var Q = require('q');.

Where does .should come from?

When I try this should is undefined and I get TypeError: Cannot call method 'notify' of undefined.

Is there some monkey patching of Q that's supposed to happen first? Or am I using the wrong version of something?

I'm using cucumber with protractor. As I understand it they don't support returning promises yet so the user has to handle the call to done.

like image 204
RichardTowers Avatar asked Dec 29 '25 18:12

RichardTowers


1 Answers

Answering my own question:

.should comes from the "should" assertions style - http://chaijs.com/guide/styles/#should. You need to run:

chai.should();

after var Q = require('q'); but before Q.all([]).should.notify...:

var Q = require('q');
var chai = require('chai');
var chaiAsPromised = require('chai-as-promised');

// ***************
chai.should();
// ***************

chai.use(chaiAsPromised);

it("should all be well", function (done) {
    Q.all([
        promiseA.should.become("happy"),
        promiseB.should.eventually.have.property("fun times"),
        promiseC.should.be.rejectedWith(TypeError, "only joyful types are allowed")
    ]).should.notify(done);
});

As per the docs:

This will pass any failures of the individual promise assertions up to the test framework

like image 94
RichardTowers Avatar answered Dec 31 '25 06:12

RichardTowers