mercredi 8 juin 2016

mocha, nodejs: how to bootstrap a promise, before all tests

i have a mocha config file, that will be required by mocha run -r mocha.conf.js
if i put something like:

//load server
before(() => require('./runServerPromise')
  .then((server) => {
    global.server = server;
  }));

in the config, it does not work. (Probably mocha is not here yet) if i just put global.server = require('./runServerPromise') in the config, it will not wait for the promise.

if i put such code in bootstrap.spec.js file, it works, because i load all *.spec.js for tests.
But i need to initialize things that needs the server in before blocks

e.g. i should write:

describe('my test', () => {
  let need1;
  let need2;
  let need3;

  before(initializeAllVars);

  function initializeAllVars() {
    need1 = getNeed1();
    need2 = getNeed2();
    need3 = getNeed3();
  }
});

i would like to initialize them in describeblock.
for this i need to make sure, the bootstrap is loaded before all tests.

is it possible? If yes, how?

Refactor all tools to be promise-like and waiting for server, makes the code too complicated.

info: I use grunt with mocha_istanbul for building and running, maybe there is some tool/option.
node 4.4.x

Aucun commentaire:

Enregistrer un commentaire