Promises in Mocha Unit Tests

£可爱£侵袭症+ 提交于 2019-12-25 04:45:47

问题


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

回答1:


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




回答2:


I read this in a discussion, this works for me, although some are discouraging use of 'done' callback with promises.

it('collects  myCollection test', function(done) {
        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);
            done();
        }).catch(function(err) {
            done(err);
        });
    });



回答3:


I use PublicationCollector like this:

it('should publish 2 documents', async () => {
    const collector = new PublicationCollector({ 'userId': Random.id() });

    const testPromise = new Promise((resolve, reject) => {
        collector.collect('myDocuments',
            (collections) => {
                resolve(collections.myDocuments.length);
            });
    });

    const result = await testPromise;

    assert.equal(result, 1);
});

Adapted from here.

This test fails relatively gracefully.



来源:https://stackoverflow.com/questions/37735838/promises-in-mocha-unit-tests

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!