Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
239 views
in Technique[技术] by (71.8m points)

javascript - Promises in Mocha Unit Tests

This question is related to Using Promises to test Meteor - Mocha

Like Louis suggested, I have replicated the same issue in a smaller program, so that you can reproduce this. And in this one too Mocha doesn't care about the assert statement. The catch block of the promises gets this error.

/server/main.js

import { Meteor } from 'meteor/meteor';

export const myCollection = new Mongo.Collection('mycollection');

export const addObject = function (id) {
    myCollection.insert({
        name: 'test ' + id
    });
}

Meteor.publish('mycollection', function() {
    return myCollection.find({});
});

/server/main.test.js

/**
 * Created by enigma on 6/9/16.
 */
import { Meteor } from 'meteor/meteor';
import { PublicationCollector } from 'meteor/johanbrook:publication-collector';
import { Promise } from 'meteor/promise';
import { assert } from 'meteor/practicalmeteor:chai';
import { Random } from 'meteor/random';
import { addObject } from '../server/main.js';

if (Meteor.isServer) {
    describe('test mocha promise', function() {
        before(function() {
            addObject(Random.id());
        });
        it('collects  myCollection test', function() {
            const collector = new PublicationCollector({ userId: Random.id()});

            return new Promise(function(resolve) {
                    collector.collect('mycollection', function (collections) {
                        resolve(collections);
                    });
            }).then(function(coll) {
                chai.assert.notEqual(coll, null);
                chai.assert.equal(coll, null);
            }).catch(function(err) {
                console.log('error:', err.stack);
            });
        });
    });
}

console output

=> Meteor server restarted
I20160609-18:31:14.546(-5)? MochaRunner.runServerTests: Starting server side tests with run id GK3WqWY4Ln9u6vmsg
I20160609-18:31:14.598(-5)? error: AssertionError: expected { Object (mycollection) } to equal null
I20160609-18:31:14.598(-5)?     at Function.assert.equal (packages/practicalmeteor_chai.js:2635:10)
I20160609-18:31:14.598(-5)?     at test/main.test.js:25:29
I20160609-18:31:14.598(-5)?     at /Users/enigma/.meteor/packages/promise/.0.6.7.1d67q83++os+web.browser+web.cordova/npm/node_modules/meteor-promise/fiber_pool.js:33:40
W20160609-18:31:14.607(-5)? (STDERR) MochaRunner.runServerTests: failures: 0
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

you need to either throw in the catch or remove the catch, so that mocha gets the error too. currently since you catch the error, the promise mocha gets is resolved.

Below was my old answer, before the question change ps: it looks like I misunderstood what was a publish for meteor so the bellow answer is not really correct


The error you're encountering is because "mycollection" is not published

I is probably because Meteor.publish('mycollection'); is an async function, to the collection is not published yet when you test it.

you should do the publish in a before() before your test

Here is an example of how you can wait the publish to finish in a before


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...