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 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…