can you use tinytest to test a package that uses other packages

288 Views Asked by At

I have some tinytests, simple server unit tests.

Separately they run fine, but if i run them together I get errors on my collections.

What else might cause an error like the below?

I think its related to defining the exports in a JS file and the other classes in coffeescript and some scoping issue is complicating things. "Told you not to use coffeescript" i hear. But then again, it maybe something else!

os.osx.x86_64/dev_bundle/server-lib/node_modules/fibers/future.js:245
W20150418-17:39:20.312(-7)? (STDERR)                        throw(ex);
                              ^
 Error: A method named '/Profiles/insert' is already defined
     at packages/ddp/livedata_server.js:1461:1
     at Function._.each._.forEach (packages/underscore/underscore.js:113:1)
     at [object Object]._.extend.methods (packages/ddp/livedata_server.js:1459:1)
     at [object Object].Mongo.Collection._defineMutationMethods (packages/mongo/collection.js:90
     at new Mongo.Collection (packages/mongo/collection.js:209:1)
     at [object Object].Meteor.Collection (packages/dburles:collection-helpers/collection-helper
     at __coffeescriptShare (packages/local-test:dcsan:mpgames/lib/exports.js:2:1)
     at /private/var/folders/lw/6kdr1_9j3q1ggldr_c798qy80000gn/T/meteor-test-run126tw73/.meteor/

FWIW the app has no problems running, its just the tests that fail.

1

There are 1 best solutions below

3
On

This error means you defined the Profiles collection more than once. My strategy in dealing with this problem has been to:

  1. Use a global definition via api.export for any collections which actually should be defined by a package (e.g. if the posts package defined a Posts collection).

  2. Define all other collections needed by the test with a null collection name (unmanaged) and use a reset like the following before each test:

var resetCollection = function(name) {
  var Collection = this[name];
  if (Collection)
    // if the collection is already defined, remove its documents
    Collection.remove({});
  else
    // define a new unmanaged collection
    this[name] = new Mongo.Collection(null);
};

So if you call resetCollection('Posts') it would only define a new collection if needed and ensure its documents were removed. This way, you'll avoid multiple definitions and you'll start with a clean DB each time.