Meteor-Community-Packages / meteor-mocha

A Mocha test driver package for Meteor 1.3+. This package reports server AND client test results in the server console and can be used for running tests on a CI server or locally.
https://packosphere.com/meteortesting/mocha
MIT License
67 stars 40 forks source link

How to setup a global hook for database prefills (fixtures) #110

Open derwaldgeist opened 3 years ago

derwaldgeist commented 3 years ago

I want to prefill my Meteor database once, before all other tests are run. I managed to do this using a global before() handler.

However, the Mocha docs state that this is not the preferred way. Instead, you should use a root hook plugin instead: https://mochajs.org/#root-hook-plugins

Yet, however I try to set this up, the file is ignored. I tried to define it via .mocharc.js (using both CommonJS and ES6 syntax), mochalrc.json, and an entry in package.json.

What is the preferred way to setup such a root hook plugin in Meteor?

Another question in the same context: If I use a global hook, it will work fine for the database. So I tried to use the same mechanism for setting up local collections (Mongo.Collection(null)). However this won't work, because every time Meteor recompiles its files due to changes, the hook won't be re-run, and thus the data I add to these local collections won't be initialized. Is there a way to re-run a global hook on every Meteor rebuild?

Sharealikelicence commented 2 years ago

I get the same problem. grep gets loaded fine from .mocharc.json or .mocharc.js but require does nothing, whether it be a string or an array of strings with the path(s) to a hooks.js or hooks.mjs. Something simple like this in a .mjs file does nothing:

export const mochaHooks = {
  beforeAll() {
    console.log('BEFORE ALL!!!');
  },
  beforeEach() {
    console.log('BEFORE EACH!!!');
  }
};

I've had to fork the repo, add it as a local dependency and manually load the hooks from the mochaInstance.require property to mochaInstance.rootHooks within server.js to get them to work.

Sharealikelicence commented 2 years ago

Another option that I have found, but seems like it goes against the idea of the require keyword is to use a .mocharc.js and specify the root hooks within that like so:

module.exports = {
  rootHooks: {
    beforeAll(done) {
      console.log('BEFORE ALL!!!');
      done();
    },
    beforeEach(done) {
      console.log('BEFORE EACH!!!');
      done();
    }
  }
}

It also doesn't seem to allow importing code from within other parts of the project