I have been having an issue with testing a package that requires having multiple registered users at once. On the client, apart from the currently logged in user, the users’ fields are undefined. I can still view the count of users, and the ids. On the server, directly after creation, all fields are visible.
- Why is this happening?
- Is this from Jasmine or Meteor?
- What can I do to access Meteor users when testing a package?
Below is a runnable example that illustrates this:
package.js
Package.describe({
name: 'user-publication',
version: '0.0.1',
summary: 'Used to show an issue with Meteor user publications',
git: 'git://github.com/andykingking/user-publication-example',
documentation: 'README.md'
});
Package.onUse(function(api) {
api.versionsFrom('1.1.0.3');
});
Package.onTest(function(api) {
api.use([
'sanjo:jasmine@0.18.0',
'user-publication',
'autopublish',
'accounts-password'
]);
api.addFiles('tests/user-publication-tests.js', 'client');
api.addFiles('tests/fixtures.js', 'server');
});
tests/user-publication-tests.js
describe('testing user publications', function(){
beforeEach(function(done){
Meteor.call('createUsers', function(){
done();
});
})
it('should be able to see both users', function(){
var users = Meteor.users.find();
expect(users.count()).toBe(2);
users.forEach(function(user){
expect(user.emails).toBeDefined();
});
});
afterEach(function(done){
Meteor.call('resetDatabase', function(){
done();
});
});
});
tests/fixtures.js
Meteor.methods({
'createUsers': function(){
var users = [
{
email: 'beyonce@jayonce.com',
password: 'drunkInLove'
},
{
email: 'jay@jayonce.com',
password: '99problems'
}
];
users.forEach(function(user){
var id = Accounts.createUser(user);
var createdUser = Meteor.users.findOne(id);
console.log('Created user: ' + JSON.stringify(createdUser));
});
},
'resetDatabase': function(){
console.log('Resetting database...');
var collectionsRemoved = 0;
var db = Meteor.users.find()._mongo.db;
db.collections(function (err, collections) {
var appCollections = _.reject(collections, function (col) {
return col.collectionName.indexOf('velocity') === 0 ||
col.collectionName === 'system.indexes';
});
_.each(appCollections, function (appCollection) {
appCollection.remove(function (e) {
if (e) {
console.error('Failed removing collection', e);
fut.return('fail: ' + e);
}
collectionsRemoved++;
if (appCollections.length === collectionsRemoved) {
console.log('Finished resetting database');
}
});
});
});
}
});
Example server output
I20150827-13:55:39.906(10)? Created user: {"_id":"6eKL43K3Jsowf9Jp3","createdAt":"2015-08-27T03:55:39.895Z","services":{"password":{"bcrypt":"$2a$10$gy2vFwvE2yjPk.ls3MXQTuT047Tm1SAmWj2T.GzShJiR1xmOqOqlO"}},"emails":[{"address":"beyonce@jayonce.com","verified":false}]}
I20150827-13:55:40.010(10)? Created user: {"_id":"YDaT66kNBsy5aC7Yp","createdAt":"2015-08-27T03:55:40.007Z","services":{"password":{"bcrypt":"$2a$10$WJQt60Ocn7ZucT7OVyYvPeklb7I69mChM.y.1.YGyAA.5d3m8ZWKS"}},"emails":[{"address":"jay@jayonce.com","verified":false}]}
I20150827-13:55:40.511(10)? Resetting database...
I20150827-13:55:40.518(10)? Finished resetting database
Repo for example: https://github.com/andykingking/user-publication-example
Similar issue from SO: https://stackoverflow.com/questions/29392283/only-getting-partial-user-publication-in-meteor-jasmine-test