cafe_mocha.js
4.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/*
* grunt-cafe-mocha
* https://github.com/jdavis/grunt-cafe-mocha
*
* Copyright (c) 2013 Josh Davis
* Licensed under the MIT license.
*/
'use strict';
var Mocha = require('mocha'),
path = require('path'),
fs = require('fs'),
Base = Mocha.reporters.Base,
cwd = process.cwd(),
exists = fs.existsSync || path.existsSync,
handler = {};
module.exports = function(grunt) {
// Add local node_modules to path
module.paths.push(cwd, path.join(cwd, 'node_modules'));
grunt.registerMultiTask('cafemocha', 'Run server-side Mocha tests', function() {
var options = this.options({
asyncOnly: false,
bail: false,
/// colors: undefined, shouldn't be defined
coverage: false,
globals: [],
grep: false,
growl: false,
ignoreLeaks: false,
invert: false,
reporter: 'list',
require: [],
slow: 75,
timeout: 2000,
ui: 'bdd',
});
// Mocha runner
var mocha = new Mocha();
// Async function to ensure Grunt finishes
var async = this.async();
// Original write function
var _stdout = process.stdout.write;
// Coverage output file
var output;
// Setup some settings
mocha.ui(options.ui);
mocha.reporter(options.reporter);
mocha.suite.bail(options.bail);
// Optional settings
if (options.timeout) mocha.suite.timeout(options.timeout);
if (options.grep) mocha.grep(new RegExp(options.grep));
if (options.growl) mocha.growl();
if (options.invert) mocha.invert();
if (options.ignoreLeaks) mocha.ignoreLeaks();
if (options.asyncOnly) mocha.asyncOnly();
if (options.slow) mocha.slow();
if (options.colors === true) Base.useColors = true;
else if (options.colors === false) Base.useColors = false;
mocha.globals(options.globals);
// Check for coverage env option, else just set true
// do this before any of the mocha requires
if (options.coverage) {
if (options.coverage.env) {
process.env[options.coverage.env] = 1;
} else {
process.env['COV'] = 1;
}
}
for(var option in options) {
if (options.hasOwnProperty(option)) {
if (option in handler) {
handler[option].call(this, options[option]);
}
}
}
this.files.forEach(function (f) {
f.src.filter(function (file) {
mocha.addFile(file);
});
});
if (options.reporter === 'json-cov' || options.reporter === 'html-cov') {
if (!options.coverage) return grunt.fail.warn('Coverage option not set.');
}
if (options.coverage) {
// Check for coverage output file, else use default
if (options.coverage.output) {
output = fs.createWriteStream(options.coverage.output, {flags: 'w'});
} else {
output = fs.createWriteStream('coverage.html', {flags: 'w'});
}
process.stdout.write = function (chunk, encoding, cb) {
output.write(chunk, encoding, cb);
};
}
mocha.run(function (failures) {
var finish = function () {
// Restore default process.stdout.write
process.stdout.write = _stdout;
if (failures) {
grunt.fail.warn('Mocha tests failed.');
}
return async();
};
if (output) {
// Close output
output.end(finish);
} else {
finish()
}
});
});
};
handler.require = function (f) {
var files = [].concat(f);
files.forEach(function (file) {
// Check for relative/absolute path
if (exists(file) || exists(file + '.js')) {
// Append our cwd to import it
require(path.join(cwd, file));
} else {
// Might just be a node_module
require(file);
}
});
};