Testing collections on the client with help from the meteor guide


#1

Hello, I was reading through the meteor guide and saw mention of a stub-collections package that is used in the sample todos app. It’s only mentioned in passing and there is the following note:

that’s encoded in the stub-collections package (currently a local package in the Todos example application).

Are there plans to publish this package on NPM or atmosphere? Is this package intended to let me import my collection on the client so that I can add fixture data and run tests on it without throwing an error due to a disallowed client side insert?

I tried out the stub-collections code in my project but would get an error that the collection was already defined.

I came up with an alternative solution for testing collections on the client, but I’d like some feedback on it.

The way I setup my collections is to extend a base collection object that in turn extends the Mongo.Collection object. I do a test in my constructor to see if a client side test is being run, and if so, I initialize the mongo.collection without the name parameter to create a local, non-server backed collection.

This seems to work but I don’t like the idea of having code in an application file that exists only to facilitate testing. If anyone has thoughts on whether this may be bad practice I’d be interested to hear them.

import { SimpleSchema } from 'meteor/aldeed:simple-schema';
import { Mongo } from 'meteor/mongo';
import { Meteor } from 'meteor/meteor';
import has from 'lodash/has';

class Collection extends Mongo.Collection {

	constructor(name, options) {

		if(!name) throw 'Collection name not set';

		options = options || {};

		if(has(options, 'documentClass')) {
		
			let docClass = options.documentClass;

			options.transform = (doc) => { return new docClass(doc) };

			delete options.documentClass;

		}

		if(Meteor.isClient && Meteor.isTest)
		{
			
			super(null, options); // create local non-server backed collection for testing purposes

		} else {
			
			super(name, options); // create normal server backed collection

		}

		if(this.schema())
		{

			this.attachSchema(
				new SimpleSchema(this.schema())
			);

		}		

	}

	schema() {
		
		return false;

	}

}

export { Collection, Collection as default }

#2

Yes - see:

Also check out the stub-collections README for an explanation of what it takes care of. I really hope to get to splitting these out soon …