lundi 19 janvier 2015

Using the --globals variable with MochaJS testing suite

I am trying to do a variant of end-to-end testing on a sailjs system using mocha. What I want to do is to simulate the action program flow by doing things like creating a user, and then performing other action with that user.


I'd like to be able to separate my tests into separate files that run in order in relation to different operations, such as "register new user" etc. To do this I need to be able to pass values between testing files.


Mocha contains an option setting --globals <value1, value2, etc>. Here is the description from the docs:



--globals allow the given comma-delimited global [names]



However, I've been unable to get this to work. Here's what I've tried. I have a bootstrap.test.js file that does basic before and after operations, starting and stopping sails:



var Sails = require('sails'),
sails;

before(function(done) {
Sails.lift({
log: {
level: 'error'
}
}, function(err, server) {
sails = server;
if (err) return done(err);
// here you can load fixtures, etc.
done(err, sails);
});
});

after(function(done) {
// here you can clear fixtures, etc.
sails.lower(done);
});


Then let's say I have two test files a.js and b.js that will run consecutively and for testing purposes contain very little:


a.js:



var user = 'some user';


b.js:



console.log( user );


If I then run mocha --globals, I get the error:



ReferenceError: user is not defined



What am I doing wrong here? I have been unable to find anywhere on the web a description of how this would be used.


Aucun commentaire:

Enregistrer un commentaire