0

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.

dcsan
  • 11,333
  • 15
  • 77
  • 118

1 Answers1

0

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.

David Weldon
  • 63,632
  • 11
  • 148
  • 146
  • I'd prefer to resolve why it thinks Profiles is defined twice. This seems to be only within the test harness. Profiles is in fact defined in a separate package, which is only `use`d once. But it is a required dep so i cant really create a fake null version. I did define it cleanly within an `exports.js` that meteor's package namespacing can deal with happily. – dcsan Apr 30 '15 at 07:52
  • Grep for `new Mongo.Collection`? I'm not sure how to help without seeing your code. – David Weldon May 03 '15 at 21:04
  • I think it is because two tested packages both include a third package, which possibly has its own tests, so that third package's init code runs twice. seems like an awkward limitation, but haven't confirmed that yet. – dcsan May 04 '15 at 16:58