Skip to content

Commit

Permalink
Merge pull request #56 from stevenvachon/master
Browse files Browse the repository at this point in the history
support for more detailed requires
  • Loading branch information
pghalliday committed Mar 1, 2014
2 parents 45fb8bb + 366e7fd commit ddba980
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 9 deletions.
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ mochaTest: {
}
```

In order to make this more user friendly the `require` option can take either a single file or an array of files in case you have other globals you wish to require.
In order to make this more user friendly, the `require` option can take either a single file/function or an array of files/functions in case you have other globals you wish to require.

eg.

Expand All @@ -89,15 +89,17 @@ mochaTest: {
reporter: 'spec',
require: [
'coffee-script/register',
'./globals.js'
'./globals.js',
function(){ testVar1=require('./stuff'); },
function(){ testVar2='other-stuff'; }
]
},
src: ['test/**/*.coffee']
}
}
```

NB. The `require` option can only be used with Javascript files, ie. it is not possible to specify a `./globals.coffee` in the above example.
NB. File references for the `require` option can only be used with Javascript files, ie. it is not possible to specify a `./globals.coffee` in the above example.

### Specifying a Mocha module

Expand Down
12 changes: 8 additions & 4 deletions tasks/lib/MochaWrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,15 @@ function MochaWrapper(params) {
if (params.options && params.options.require) {
var mods = params.options.require instanceof Array ? params.options.require : [params.options.require];
mods.forEach(function(mod) {
var abs = exists(mod) || exists(mod + '.js');
if (abs) {
mod = resolve(mod);
if (typeof mod === 'string') {
var abs = exists(mod) || exists(mod + '.js');
if (abs) {
mod = resolve(mod);
}
require(mod);
} else if (typeof mod === 'function') {
mod();
}
require(mod);
});
}

Expand Down
10 changes: 9 additions & 1 deletion test/scenarios/requireArrayOption/Gruntfile.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/*global testVar4:true, testVar5:true*/

module.exports = function(grunt) {
// Add our custom tasks.
grunt.loadTasks('../../../tasks');
Expand All @@ -7,7 +9,13 @@ module.exports = function(grunt) {
mochaTest: {
options: {
reporter: 'spec',
require: ['require/common1', 'require/common2', 'require/common3']
require: [
'require/common1',
'require/common2',
'require/common3',
function(){ testVar4=require('./require/common4'); },
function(){ testVar5=':)'; }
]
},
all: {
src: ['*.js']
Expand Down
1 change: 1 addition & 0 deletions test/scenarios/requireArrayOption/require/common4.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = ' ';
4 changes: 3 additions & 1 deletion test/scenarios/requireArrayOption/test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*global testVar1:false, testVar2:false, testVar3:false*/
/*global testVar1:false, testVar2:false, testVar3:false, testVar4:false, testVar5:false*/

var expect = require('chai').expect;

Expand All @@ -7,5 +7,7 @@ describe('test', function() {
expect(testVar1).to.equal('hello');
expect(testVar2).to.equal('world');
expect(testVar3).to.equal('!');
expect(testVar4).to.equal(' ');
expect(testVar5).to.equal(':)');
});
});

0 comments on commit ddba980

Please sign in to comment.