Testing Raw Collection undefined

I am new to testing and attempting to insert a new document into a collection that gets the id by incrementing a raw collection.

The collection is declared globally inside /imports/api/counters/Counters.js:

CountersMeteor = new Mongo.Collection('counters');

Counters = CountersMeteor.rawCollection();

The test is defined in /tests/main.js. Here is the part that is failing:

if (Meteor.isServer) {
  import { Counters } from '/imports/api/counters/Counters.js';

  describe('insertDocument', function() {
    it('inserts a document', function(done) {
      const context = { userId: Random.id() };
      const args = { name: 'foo' };
      const result = Meteor.call('myDocumentsCollection.insert');
      const checkDocumentInsert = MyDocumentsCollection.findOne();
      assert.equal(checkDocumentInsert.length, 1);

The method is defined on Meteor.methods:

'myDocumentsCollection.insert': function(document) {
  const performIncrement = function(collection, callback) {
      _id: 'documentHrId'
    }, [], {
      $inc: {
        seq: 1
    }, {
      'new': true
    }, callback);

  const nextAutoincrement = function(collection) {
    return Meteor.wrapAsync(performIncrement)(collection).value;

  let nextValue = nextAutoincrement(Counters);

  return MyDocumentsCollection.insert({
      'createdAt': new Date(),
      'createdBy': Meteor.userId(),

If I don’t import Counters, it is undefined even though it’s a global collection. Why is this?

I receive the error TypeError: Cannot read property 'seq' of null because the value of let nextValue = nextAutoincrement(Counters); is null. Is this something to do with WrapAsync? My autoincrement function does what I expect when I run it normally. How can I test it?

When running tests, Meteor doesn’t auto-load the whole application unless it is run with the --full-app flag. So it’s likely not running the /imports/api/counters/Counters.js file at all, and Counters is undefined as a result.
This is one of the reasons why it’s recommended to be explicit about dependencies instead of relying on globals.

See the guide for more info: