3

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.

RichardTowers
  • 4,682
  • 1
  • 26
  • 43

2 Answers2

3

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

RichardTowers
  • 4,682
  • 1
  • 26
  • 43
0

if I understood correctly, Q-promise dont have should, i recommend you try this

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")
    ]).then(done);
});

also you can use require mocha-as-promised, like this:

require("mocha-as-promised")();

it("should all be well", function (done) {
    return Q.all([
            promiseA.then(function(someData){
                //here standart chai validation;
            }),
            promiseB.then(function(someData){
                //here standart chai validation;
            });
    ]).then(done);
});

Ok, are you add next line to your code?

var chai = require("chai");
var chaiAsPromised = require("chai-as-promised");

chai.use(chaiAsPromised);
siavolt
  • 6,869
  • 5
  • 24
  • 27
  • Unfortunately this doesn't pass the test state through to the framework correctly. I've worked out where `should` comes from now; I'll answer my own question I guess... – RichardTowers Mar 04 '15 at 13:11