Coverage

46%
1036
484
552

mocha.js

32%
61
20
41
LineHitsSource
1
2/*!
3 * mocha
4 * Copyright(c) 2011 TJ Holowaychuk <tj@vision-media.ca>
5 * MIT Licensed
6 */
7
8/**
9 * Module dependencies.
10 */
11
121var path = require('path');
13
14/**
15 * Expose `Mocha`.
16 */
17
181exports = module.exports = Mocha;
19
20/**
21 * Library version.
22 */
23
241exports.version = '0.14.1';
25
26/**
27 * Expose internals.
28 */
29
301exports.utils = require('./utils');
311exports.interfaces = require('./interfaces');
321exports.reporters = require('./reporters');
331exports.Runnable = require('./runnable');
341exports.Context = require('./context');
351exports.Runner = require('./runner');
361exports.Suite = require('./suite');
371exports.Hook = require('./hook');
381exports.Test = require('./test');
39
40/**
41 * Growl images.
42 */
43
441var images = {
45 fail: __dirname + '/../images/error.png'
46 , pass: __dirname + '/../images/ok.png'
47};
48
49/**
50 * Setup mocha with `options`.
51 *
52 * Options:
53 *
54 * - `ui` name "bdd", "tdd", "exports" etc
55 * - `reporter` reporter instance, defaults to `mocha.reporters.Dot`
56 * - `globals` array of accepted globals
57 * - `timeout` timeout in milliseconds
58 * - `ignoreLeaks` ignore global leaks
59 *
60 * @param {Object} options
61 * @api public
62 */
63
641function Mocha(options) {
650 options = options || {};
660 this.files = [];
670 this.options = options;
680 this.suite = new exports.Suite('', new exports.Context);
690 this.ui(options.ui);
700 this.reporter(options.reporter);
710 if (options.timeout) this.suite.timeout(options.timeout);
72}
73
74/**
75 * Add test `file`.
76 *
77 * @param {String} file
78 * @api public
79 */
80
811Mocha.prototype.addFile = function(file){
820 this.files.push(file);
830 return this;
84};
85
86/**
87 * Set reporter to `name`, defaults to "dot".
88 *
89 * @param {String} name
90 * @api public
91 */
92
931Mocha.prototype.reporter = function(name){
940 name = name || 'dot';
950 this._reporter = require('./reporters/' + name);
960 if (!this._reporter) throw new Error('invalid reporter "' + name + '"');
970 return this;
98};
99
100/**
101 * Set test UI `name`, defaults to "bdd".
102 *
103 * @param {String} bdd
104 * @api public
105 */
106
1071Mocha.prototype.ui = function(name){
1080 name = name || 'bdd';
1090 this._ui = exports.interfaces[name];
1100 if (!this._ui) throw new Error('invalid interface "' + name + '"');
1110 this._ui = this._ui(this.suite);
1120 return this;
113};
114
115/**
116 * Load registered files.
117 *
118 * @api private
119 */
120
1211Mocha.prototype.loadFiles = function(){
1220 var suite = this.suite;
1230 this.files.forEach(function(file){
1240 file = path.resolve(file);
1250 suite.emit('pre-require', global, file);
1260 suite.emit('require', require(file), file);
1270 suite.emit('post-require', global, file);
128 });
129};
130
131/**
132 * Enable growl support.
133 *
134 * @api private
135 */
136
1371Mocha.prototype.growl = function(runner, reporter) {
1380 var notify = require('growl');
139
1400 runner.on('end', function(){
1410 var stats = reporter.stats;
1420 if (stats.failures) {
1430 var msg = stats.failures + ' of ' + runner.total + ' tests failed';
1440 notify(msg, { title: 'Failed', image: images.fail });
145 } else {
1460 notify(stats.passes + ' tests passed in ' + stats.duration + 'ms', {
147 title: 'Passed'
148 , image: images.pass
149 });
150 }
151 });
152};
153
154/**
155 * Run tests and invoke `fn()` when complete.
156 *
157 * @param {Function} fn
158 * @return {Runner}
159 * @api public
160 */
161
1621Mocha.prototype.run = function(fn){
1630 this.loadFiles();
1640 var suite = this.suite;
1650 var options = this.options;
1660 var runner = new exports.Runner(suite);
1670 var reporter = new this._reporter(runner);
1680 runner.ignoreLeaks = options.ignoreLeaks;
1690 if (options.grep) runner.grep(options.grep);
1700 if (options.globals) runner.globals(options.globals);
1710 if (options.growl) this.growl(runner, reporter);
1720 return runner.run(fn);
173};

utils.js

57%
47
27
20
LineHitsSource
1
2/**
3 * Module dependencies.
4 */
5
61var fs = require('fs')
7 , path = require('path')
8 , join = path.join
9 , debug = require('debug')('watch');
10
11/**
12 * Ignored directories.
13 */
14
151var ignore = ['node_modules', '.git'];
16
17/**
18 * Escape special characters in the given string of html.
19 *
20 * @param {String} html
21 * @return {String}
22 * @api private
23 */
24
251exports.escape = function(html) {
260 return String(html)
27 .replace(/&/g, '&amp;')
28 .replace(/"/g, '&quot;')
29 .replace(/</g, '&lt;')
30 .replace(/>/g, '&gt;');
31};
32
33/**
34 * Array#forEach (<=IE8)
35 *
36 * @param {Array} array
37 * @param {Function} fn
38 * @param {Object} scope
39 * @api private
40 */
41
421exports.forEach = function(arr, fn, scope) {
4313 for (var i = 0, l = arr.length; i < l; i++)
44364 fn.call(scope, arr[i], i);
45};
46
47/**
48 * Array#indexOf (<=IE8)
49 *
50 * @parma {Array} arr
51 * @param {Object} obj to find index of
52 * @param {Number} start
53 * @api private
54 */
55
561exports.indexOf = function (arr, obj, start) {
575479 for (var i = start || 0, l = arr.length; i < l; i++) {
5889586 if (arr[i] === obj)
595476 return i;
60 }
613 return -1;
62};
63
64/**
65 * Array#reduce (<=IE8)
66 *
67 * @param {Array} array
68 * @param {Function} fn
69 * @param {Object} initial value
70 * @param {Object} scope
71 * @api private
72 */
73
741exports.reduce = function(arr, fn, val, scope) {
7590 var rval = val;
76
7790 for (var i = 0, l = arr.length; i < l; i++) {
7877 rval = fn.call(scope, rval, arr[i], i, arr);
79 }
80
8190 return rval;
82};
83
84/**
85 * Array#filter (<=IE8)
86 *
87 * @param {Array} array
88 * @param {Function} fn
89 * @param {Object} scope
90 * @api private
91 */
92
931exports.filter = function(arr, fn, scope) {
94173 var ret = [];
95
96173 for (var i = 0, l = arr.length; i < l; i++) {
975479 var val = arr[i];
985479 if (fn.call(scope, val, i, arr))
993 ret.push(val);
100 }
101
102173 return ret;
103};
104
105/**
106 * Object.keys (<=IE8)
107 *
108 * @param {Object} obj
109 * @return {Array} keys
110 * @api private
111 */
112
1131exports.keys = Object.keys || function(obj) {
1140 var keys = []
115 , has = Object.prototype.hasOwnProperty // for `window` on <=IE8
116
1170 for (var key in obj) {
1180 if (has.call(obj, key)) {
1190 keys.push(key);
120 }
121 }
122
1230 return keys;
124};
125
126/**
127 * Watch the given `files` for changes
128 * and invoke `fn(file)` on modification.
129 *
130 * @param {Array} files
131 * @param {Function} fn
132 * @api private
133 */
134
1351exports.watch = function(files, fn){
1360 var options = { interval: 100 };
1370 files.forEach(function(file){
1380 debug('file %s', file);
1390 fs.watchFile(file, options, function(curr, prev){
1400 if (prev.mtime < curr.mtime) fn(file);
141 });
142 });
143};
144
145/**
146 * Ignored files.
147 */
148
1491function ignored(path){
1500 return !~ignore.indexOf(path);
151}
152
153/**
154 * Lookup files in the given `dir`.
155 *
156 * @return {Array}
157 * @api private
158 */
159
1601exports.files = function(dir, ret){
1610 ret = ret || [];
162
1630 fs.readdirSync(dir)
164 .filter(ignored)
165 .forEach(function(path){
1660 path = join(dir, path);
1670 if (fs.statSync(path).isDirectory()) {
1680 exports.files(path, ret);
1690 } else if (path.match(/\.(js|coffee)$/)) {
1700 ret.push(path);
171 }
172 });
173
1740 return ret;
175};

interfaces/index.js

100%
4
4
0
LineHitsSource
1
21exports.bdd = require('./bdd');
31exports.tdd = require('./tdd');
41exports.qunit = require('./qunit');
51exports.exports = require('./exports');

interfaces/bdd.js

100%
21
21
0
LineHitsSource
1
2/**
3 * Module dependencies.
4 */
5
61var Suite = require('../suite')
7 , Test = require('../test');
8
9/**
10 * BDD-style interface:
11 *
12 * describe('Array', function(){
13 * describe('#indexOf()', function(){
14 * it('should return -1 when not present', function(){
15 *
16 * });
17 *
18 * it('should return the index when present', function(){
19 *
20 * });
21 * });
22 * });
23 *
24 */
25
261module.exports = function(suite){
271 var suites = [suite];
28
291 suite.on('pre-require', function(context){
30
31 // noop variants
32
3319 context.xdescribe = function(){};
3419 context.xit = function(){};
35
36 /**
37 * Execute before running tests.
38 */
39
4019 context.before = function(fn){
414 suites[0].beforeAll(fn);
42 };
43
44 /**
45 * Execute after running tests.
46 */
47
4819 context.after = function(fn){
495 suites[0].afterAll(fn);
50 };
51
52 /**
53 * Execute before each test case.
54 */
55
5619 context.beforeEach = function(fn){
5723 suites[0].beforeEach(fn);
58 };
59
60 /**
61 * Execute after each test case.
62 */
63
6419 context.afterEach = function(fn){
657 suites[0].afterEach(fn);
66 };
67
68 /**
69 * Describe a "suite" with the given `title`
70 * and callback `fn` containing nested suites
71 * and/or tests.
72 */
73
7419 context.describe = function(title, fn){
7577 var suite = Suite.create(suites[0], title);
7677 suites.unshift(suite);
7777 fn();
7877 suites.shift();
79 };
80
81 /**
82 * Describe a specification or test-case
83 * with the given `title` and callback `fn`
84 * acting as a thunk.
85 */
86
8719 context.it = function(title, fn){
8880 suites[0].addTest(new Test(title, fn));
89 };
90 });
91};

suite.js

100%
93
93
0
LineHitsSource
1
2/**
3 * Module dependencies.
4 */
5
61var EventEmitter = require('events').EventEmitter
7 , debug = require('debug')('suite')
8 , utils = require('./utils')
9 , Hook = require('./hook');
10
11/**
12 * Expose `Suite`.
13 */
14
151exports = module.exports = Suite;
16
17/**
18 * Create a new `Suite` with the given `title`
19 * and parent `Suite`. When a suite with the
20 * same title is already present, that suite
21 * is returned to provide nicer reporter
22 * and more flexible meta-testing.
23 *
24 * @param {Suite} parent
25 * @param {String} title
26 * @return {Suite}
27 * @api public
28 */
29
301exports.create = function(parent, title){
3177 var suite = new Suite(title, parent.ctx);
3277 suite.parent = parent;
3377 title = suite.fullTitle();
3477 parent.addSuite(suite);
3577 return suite;
36};
37
38/**
39 * Initialize a new `Suite` with the given
40 * `title` and `ctx`.
41 *
42 * @param {String} title
43 * @param {Context} ctx
44 * @api private
45 */
46
471function Suite(title, ctx) {
48125 this.title = title;
49125 this.ctx = ctx;
50125 this.suites = [];
51125 this.tests = [];
52125 this._beforeEach = [];
53125 this._beforeAll = [];
54125 this._afterEach = [];
55125 this._afterAll = [];
56125 this.root = !title;
57125 this._timeout = 2000;
58125 this._bail = false;
59}
60
61/**
62 * Inherit from `EventEmitter.prototype`.
63 */
64
651Suite.prototype.__proto__ = EventEmitter.prototype;
66
67/**
68 * Return a clone of this `Suite`.
69 *
70 * @return {Suite}
71 * @api private
72 */
73
741Suite.prototype.clone = function(){
759 var suite = new Suite(this.title);
769 debug('clone');
779 suite.ctx = this.ctx;
789 suite.timeout(this.timeout());
799 suite.bail(this.bail());
809 return suite;
81};
82
83/**
84 * Set timeout `ms` or short-hand such as "2s".
85 *
86 * @param {Number|String} ms
87 * @return {Suite|Number} for chaining
88 * @api private
89 */
90
911Suite.prototype.timeout = function(ms){
92533 if (0 == arguments.length) return this._timeout;
9395 if (String(ms).match(/s$/)) ms = parseFloat(ms) * 1000;
9495 debug('timeout %d', ms);
9595 this._timeout = parseInt(ms, 10);
9695 return this;
97};
98
99/**
100 * Sets whether to bail after first error.
101 *
102 * @parma {Boolean} bail
103 * @return {Suite|Number} for chaining
104 * @api private
105 */
106
1071Suite.prototype.bail = function(bail){
108278 if (0 == arguments.length) return this._bail;
10992 debug('bail %s', bail);
11092 this._bail = bail;
11192 return this;
112};
113
114/**
115 * Run `fn(test[, done])` before running tests.
116 *
117 * @param {Function} fn
118 * @return {Suite} for chaining
119 * @api private
120 */
121
1221Suite.prototype.beforeAll = function(fn){
1235 var hook = new Hook('"before all" hook', fn);
1245 hook.parent = this;
1255 hook.timeout(this.timeout());
1265 hook.ctx = this.ctx;
1275 this._beforeAll.push(hook);
1285 this.emit('beforeAll', hook);
1295 return this;
130};
131
132/**
133 * Run `fn(test[, done])` after running tests.
134 *
135 * @param {Function} fn
136 * @return {Suite} for chaining
137 * @api private
138 */
139
1401Suite.prototype.afterAll = function(fn){
1416 var hook = new Hook('"after all" hook', fn);
1426 hook.parent = this;
1436 hook.timeout(this.timeout());
1446 hook.ctx = this.ctx;
1456 this._afterAll.push(hook);
1466 this.emit('afterAll', hook);
1476 return this;
148};
149
150/**
151 * Run `fn(test[, done])` before each test case.
152 *
153 * @param {Function} fn
154 * @return {Suite} for chaining
155 * @api private
156 */
157
1581Suite.prototype.beforeEach = function(fn){
15924 var hook = new Hook('"before each" hook', fn);
16024 hook.parent = this;
16124 hook.timeout(this.timeout());
16224 hook.ctx = this.ctx;
16324 this._beforeEach.push(hook);
16424 this.emit('beforeEach', hook);
16524 return this;
166};
167
168/**
169 * Run `fn(test[, done])` after each test case.
170 *
171 * @param {Function} fn
172 * @return {Suite} for chaining
173 * @api private
174 */
175
1761Suite.prototype.afterEach = function(fn){
1778 var hook = new Hook('"after each" hook', fn);
1788 hook.parent = this;
1798 hook.timeout(this.timeout());
1808 hook.ctx = this.ctx;
1818 this._afterEach.push(hook);
1828 this.emit('afterEach', hook);
1838 return this;
184};
185
186/**
187 * Add a test `suite`.
188 *
189 * @param {Suite} suite
190 * @return {Suite} for chaining
191 * @api private
192 */
193
1941Suite.prototype.addSuite = function(suite){
19581 suite.parent = this;
19681 suite.timeout(this.timeout());
19781 suite.bail(this.bail());
19881 this.suites.push(suite);
19981 this.emit('suite', suite);
20081 return this;
201};
202
203/**
204 * Add a `test` to this suite.
205 *
206 * @param {Test} test
207 * @return {Suite} for chaining
208 * @api private
209 */
210
2111Suite.prototype.addTest = function(test){
21282 test.parent = this;
21382 test.timeout(this.timeout());
21482 test.ctx = this.ctx;
21582 this.tests.push(test);
21682 this.emit('test', test);
21782 return this;
218};
219
220/**
221 * Return the full title generated by recursively
222 * concatenating the parent's full title.
223 *
224 * @return {String}
225 * @api public
226 */
227
2281Suite.prototype.fullTitle = function(){
229758 if (this.parent) {
230521 var full = this.parent.fullTitle();
231808 if (full) return full + ' ' + this.title;
232 }
233471 return this.title;
234};
235
236/**
237 * Return the total number of tests.
238 *
239 * @return {Number}
240 * @api public
241 */
242
2431Suite.prototype.total = function(){
24490 return utils.reduce(this.suites, function(sum, suite){
24577 return sum + suite.total();
246 }, 0) + this.tests.length;
247};

hook.js

100%
6
6
0
LineHitsSource
1
2/**
3 * Module dependencies.
4 */
5
61var Runnable = require('./runnable');
7
8/**
9 * Expose `Hook`.
10 */
11
121module.exports = Hook;
13
14/**
15 * Initialize a new `Hook` with the given `title` and callback `fn`.
16 *
17 * @param {String} title
18 * @param {Function} fn
19 * @api private
20 */
21
221function Hook(title, fn) {
2343 Runnable.call(this, title, fn);
2443 this.type = 'hook';
25}
26
27/**
28 * Inherit from `Runnable.prototype`.
29 */
30
311Hook.prototype.__proto__ = Runnable.prototype;

runnable.js

93%
59
55
4
LineHitsSource
1
2/**
3 * Module dependencies.
4 */
5
61var EventEmitter = require('events').EventEmitter
7 , debug = require('debug')('runnable');
8
9/**
10 * Expose `Runnable`.
11 */
12
131module.exports = Runnable;
14
15/**
16 * Initialize a new `Runnable` with the given `title` and callback `fn`.
17 *
18 * @param {String} title
19 * @param {Function} fn
20 * @api private
21 */
22
231function Runnable(title, fn) {
24137 this.title = title;
25137 this.fn = fn;
26137 this.async = fn && fn.length;
27137 this.sync = ! this.async;
28137 this._timeout = 2000;
29137 this.timedOut = false;
30}
31
32/**
33 * Inherit from `EventEmitter.prototype`.
34 */
35
361Runnable.prototype.__proto__ = EventEmitter.prototype;
37
38/**
39 * Set & get timeout `ms`.
40 *
41 * @param {Number} ms
42 * @return {Runnable|Number} ms or self
43 * @api private
44 */
45
461Runnable.prototype.timeout = function(ms){
47483 if (0 == arguments.length) return this._timeout;
48127 debug('timeout %d', ms);
49127 this._timeout = ms;
50128 if (this.timer) this.resetTimeout();
51127 return this;
52};
53
54/**
55 * Return the full title generated by recursively
56 * concatenating the parent's full title.
57 *
58 * @return {String}
59 * @api public
60 */
61
621Runnable.prototype.fullTitle = function(){
6380 return this.parent.fullTitle() + ' ' + this.title;
64};
65
66/**
67 * Clear the timeout.
68 *
69 * @api private
70 */
71
721Runnable.prototype.clearTimeout = function(){
7346 clearTimeout(this.timer);
74};
75
76/**
77 * Reset the timeout.
78 *
79 * @api private
80 */
81
821Runnable.prototype.resetTimeout = function(){
831 var self = this
84 , ms = this.timeout();
85
861 this.clearTimeout();
871 if (ms) {
881 this.timer = setTimeout(function(){
890 self.callback(new Error('timeout of ' + ms + 'ms exceeded'));
900 self.timedOut = true;
91 }, ms);
92 }
93};
94
95/**
96 * Run the test and invoke `fn(err)`.
97 *
98 * @param {Function} fn
99 * @api private
100 */
101
1021Runnable.prototype.run = function(fn){
103176 var self = this
104 , ms = this.timeout()
105 , start = new Date
106 , ctx = this.ctx
107 , finished
108 , emitted;
109
110 // timeout
111176 if (this.async) {
11245 if (ms) {
11345 this.timer = setTimeout(function(){
1140 done(new Error('timeout of ' + ms + 'ms exceeded'));
1150 self.timedOut = true;
116 }, ms);
117 }
118 }
119
120 // called multiple times
121176 function multiple() {
12212 if (emitted) return;
1232 emitted = true;
1242 self.emit('error', new Error('done() called multiple times'));
125 }
126
127 // finished
128176 function done(err) {
12952 if (self.timedOut) return;
13059 if (finished) return multiple();
13145 self.clearTimeout();
13245 self.duration = new Date - start;
13345 finished = true;
13445 fn(err);
135 }
136
137 // for .resetTimeout()
138176 this.callback = done;
139
140 // async
141176 if (this.async) {
14245 try {
14345 this.fn.call(ctx, function(err){
14454 if (err instanceof Error) return done(err);
14548 if (null != err) return done(new Error('done() invoked with non-Error: ' + err));
14648 done();
147 });
148 } catch (err) {
1491 done(err);
150 }
15145 return;
152 }
153
154 // sync
155131 try {
156261 if (!this.pending) this.fn.call(ctx);
157130 this.duration = new Date - start;
158130 fn();
159 } catch (err) {
1601 fn(err);
161 }
162};

test.js

80%
10
8
2
LineHitsSource
1
2/**
3 * Module dependencies.
4 */
5
61var Runnable = require('./runnable');
7
8/**
9 * Expose `Test`.
10 */
11
121module.exports = Test;
13
14/**
15 * Initialize a new `Test` with the given `title` and callback `fn`.
16 *
17 * @param {String} title
18 * @param {Function} fn
19 * @api private
20 */
21
221function Test(title, fn) {
2382 Runnable.call(this, title, fn);
2482 this.pending = !fn;
2582 this.type = 'test';
26}
27
28/**
29 * Inherit from `Runnable.prototype`.
30 */
31
321Test.prototype.__proto__ = Runnable.prototype;
33
34/**
35 * Inspect the context void of private properties.
36 *
37 * @return {String}
38 * @api private
39 */
40
411Test.prototype.inspect = function(){
420 return JSON.stringify(this, function(key, val){
430 return '_' == key[0]
44 ? undefined
45 : 'parent' == key
46 ? '#<Suite>'
47 : val;
48 }, 2);
49};

interfaces/tdd.js

10%
19
2
17
LineHitsSource
1
2/**
3 * Module dependencies.
4 */
5
61var Suite = require('../suite')
7 , Test = require('../test');
8
9/**
10 * TDD-style interface:
11 *
12 * suite('Array', function(){
13 * suite('#indexOf()', function(){
14 * suiteSetup(function(){
15 *
16 * });
17 *
18 * test('should return -1 when not present', function(){
19 *
20 * });
21 *
22 * test('should return the index when present', function(){
23 *
24 * });
25 *
26 * suiteTeardown(function(){
27 *
28 * });
29 * });
30 * });
31 *
32 */
33
341module.exports = function(suite){
350 var suites = [suite];
36
370 suite.on('pre-require', function(context){
38
39 /**
40 * Execute before each test case.
41 */
42
430 context.setup = function(fn){
440 suites[0].beforeEach(fn);
45 };
46
47 /**
48 * Execute after each test case.
49 */
50
510 context.teardown = function(fn){
520 suites[0].afterEach(fn);
53 };
54
55 /**
56 * Execute before the suite.
57 */
58
590 context.suiteSetup = function(fn){
600 suites[0].beforeAll(fn);
61 };
62
63 /**
64 * Execute after the suite.
65 */
66
670 context.suiteTeardown = function(fn){
680 suites[0].afterAll(fn);
69 };
70
71 /**
72 * Describe a "suite" with the given `title`
73 * and callback `fn` containing nested suites
74 * and/or tests.
75 */
76
770 context.suite = function(title, fn){
780 var suite = Suite.create(suites[0], title);
790 suites.unshift(suite);
800 fn();
810 suites.shift();
82 };
83
84 /**
85 * Describe a specification or test-case
86 * with the given `title` and callback `fn`
87 * acting as a thunk.
88 */
89
900 context.test = function(title, fn){
910 suites[0].addTest(new Test(title, fn));
92 };
93 });
94};

interfaces/qunit.js

11%
18
2
16
LineHitsSource
1
2/**
3 * Module dependencies.
4 */
5
61var Suite = require('../suite')
7 , Test = require('../test');
8
9/**
10 * QUnit-style interface:
11 *
12 * suite('Array');
13 *
14 * test('#length', function(){
15 * var arr = [1,2,3];
16 * ok(arr.length == 3);
17 * });
18 *
19 * test('#indexOf()', function(){
20 * var arr = [1,2,3];
21 * ok(arr.indexOf(1) == 0);
22 * ok(arr.indexOf(2) == 1);
23 * ok(arr.indexOf(3) == 2);
24 * });
25 *
26 * suite('String');
27 *
28 * test('#length', function(){
29 * ok('foo'.length == 3);
30 * });
31 *
32 */
33
341module.exports = function(suite){
350 var suites = [suite];
36
370 suite.on('pre-require', function(context){
38
39 /**
40 * Execute before running tests.
41 */
42
430 context.before = function(fn){
440 suites[0].beforeAll(fn);
45 };
46
47 /**
48 * Execute after running tests.
49 */
50
510 context.after = function(fn){
520 suites[0].afterAll(fn);
53 };
54
55 /**
56 * Execute before each test case.
57 */
58
590 context.beforeEach = function(fn){
600 suites[0].beforeEach(fn);
61 };
62
63 /**
64 * Execute after each test case.
65 */
66
670 context.afterEach = function(fn){
680 suites[0].afterEach(fn);
69 };
70
71 /**
72 * Describe a "suite" with the given `title`.
73 */
74
750 context.suite = function(title){
760 if (suites.length > 1) suites.shift();
770 var suite = Suite.create(suites[0], title);
780 suites.unshift(suite);
79 };
80
81 /**
82 * Describe a specification or test-case
83 * with the given `title` and callback `fn`
84 * acting as a thunk.
85 */
86
870 context.test = function(title, fn){
880 suites[0].addTest(new Test(title, fn));
89 };
90 });
91};

interfaces/exports.js

8%
23
2
21
LineHitsSource
1
2/**
3 * Module dependencies.
4 */
5
61var Suite = require('../suite')
7 , Test = require('../test');
8
9/**
10 * TDD-style interface:
11 *
12 * exports.Array = {
13 * '#indexOf()': {
14 * 'should return -1 when the value is not present': function(){
15 *
16 * },
17 *
18 * 'should return the correct index when the value is present': function(){
19 *
20 * }
21 * }
22 * };
23 *
24 */
25
261module.exports = function(suite){
270 var suites = [suite];
28
290 suite.on('require', visit);
30
310 function visit(obj) {
320 var suite;
330 for (var key in obj) {
340 if ('function' == typeof obj[key]) {
350 var fn = obj[key];
360 switch (key) {
37 case 'before':
380 suites[0].beforeAll(fn);
390 break;
40 case 'after':
410 suites[0].afterAll(fn);
420 break;
43 case 'beforeEach':
440 suites[0].beforeEach(fn);
450 break;
46 case 'afterEach':
470 suites[0].afterEach(fn);
480 break;
49 default:
500 suites[0].addTest(new Test(key, fn));
51 }
52 } else {
530 var suite = Suite.create(suites[0], key);
540 suites.unshift(suite);
550 visit(obj[key]);
560 suites.shift();
57 }
58 }
59 }
60};

reporters/index.js

100%
14
14
0
LineHitsSource
1
21exports.Base = require('./base');
31exports.Dot = require('./dot');
41exports.Doc = require('./doc');
51exports.TAP = require('./tap');
61exports.JSON = require('./json');
71exports.HTML = require('./html');
81exports.List = require('./list');
91exports.Spec = require('./spec');
101exports.Progress = require('./progress');
111exports.Landing = require('./landing');
121exports.JSONCov = require('./json-cov');
131exports.HTMLCov = require('./html-cov');
141exports.JSONStream = require('./json-stream');
151exports.XUnit = require('./xunit')

reporters/base.js

17%
84
15
69
LineHitsSource
1
2/**
3 * Module dependencies.
4 */
5
61var tty = require('tty')
7 , diff = require('diff');
8
9/**
10 * Check if both stdio streams are associated with a tty.
11 */
12
131var isatty = tty.isatty(1) && tty.isatty(2);
14
15/**
16 * Expose `Base`.
17 */
18
191exports = module.exports = Base;
20
21/**
22 * Enable coloring by default.
23 */
24
251exports.useColors = isatty;
26
27/**
28 * Default color map.
29 */
30
311exports.colors = {
32 'pass': 90
33 , 'fail': 31
34 , 'bright pass': 92
35 , 'bright fail': 91
36 , 'bright yellow': 93
37 , 'pending': 36
38 , 'suite': 0
39 , 'error title': 0
40 , 'error message': 31
41 , 'error stack': 90
42 , 'checkmark': 32
43 , 'fast': 90
44 , 'medium': 33
45 , 'slow': 31
46 , 'green': 32
47 , 'light': 90
48 , 'diff gutter': 90
49 , 'diff added': 42
50 , 'diff removed': 41
51};
52
53/**
54 * Color `str` with the given `type`,
55 * allowing colors to be disabled,
56 * as well as user-defined color
57 * schemes.
58 *
59 * @param {String} type
60 * @param {String} str
61 * @return {String}
62 * @api private
63 */
64
651var color = exports.color = function(type, str) {
660 if (!exports.useColors) return str;
670 return '\033[' + exports.colors[type] + 'm' + str + '\033[0m';
68};
69
70/**
71 * Expose term window size, with some
72 * defaults for when stderr is not a tty.
73 */
74
751exports.window = {
76 width: isatty
77 ? process.stdout.getWindowSize
78 ? process.stdout.getWindowSize(1)[0]
79 : tty.getWindowSize()[1]
80 : 75
81};
82
83/**
84 * Expose some basic cursor interactions
85 * that are common among reporters.
86 */
87
881exports.cursor = {
89 hide: function(){
900 process.stdout.write('\033[?25l');
91 },
92
93 show: function(){
940 process.stdout.write('\033[?25h');
95 },
96
97 deleteLine: function(){
980 process.stdout.write('\033[2K');
99 },
100
101 beginningOfLine: function(){
1020 process.stdout.write('\033[0G');
103 },
104
105 CR: function(){
1060 exports.cursor.deleteLine();
1070 exports.cursor.beginningOfLine();
108 }
109};
110
111/**
112 * A test is considered slow if it
113 * exceeds the following value in milliseconds.
114 */
115
1161exports.slow = 75;
117
118/**
119 * Outut the given `failures` as a list.
120 *
121 * @param {Array} failures
122 * @api public
123 */
124
1251exports.list = function(failures){
1260 console.error();
1270 failures.forEach(function(test, i){
128 // format
1290 var fmt = color('error title', ' %s) %s:\n')
130 + color('error message', ' %s')
131 + color('error stack', '\n%s\n');
132
133 // msg
1340 var err = test.err
135 , message = err.message || ''
136 , stack = err.stack || message
137 , index = stack.indexOf(message) + message.length
138 , msg = stack.slice(0, index)
139 , actual = err.actual
140 , expected = err.expected;
141
142 // actual / expected diff
1430 if ('string' == typeof actual && 'string' == typeof expected) {
1440 var len = Math.max(actual.length, expected.length);
145
1460 if (len < 20) msg = errorDiff(err, 'Chars');
1470 else msg = errorDiff(err, 'Words');
148
149 // linenos
1500 var lines = msg.split('\n');
1510 if (lines.length > 4) {
1520 var width = String(lines.length).length;
1530 msg = lines.map(function(str, i){
1540 return pad(++i, width) + ' |' + ' ' + str;
155 }).join('\n');
156 }
157
158 // legend
1590 msg = '\n'
160 + color('diff removed', 'actual')
161 + ' '
162 + color('diff added', 'expected')
163 + '\n\n'
164 + msg
165 + '\n';
166
167 // indent
1680 msg = msg.replace(/^/gm, ' ');
169
1700 fmt = color('error title', ' %s) %s:\n%s')
171 + color('error stack', '\n%s\n');
172 }
173
174 // indent stack trace without msg
1750 stack = stack.slice(index ? index + 1 : index)
176 .replace(/^/gm, ' ');
177
1780 console.error(fmt, (i + 1), test.fullTitle(), msg, stack);
179 });
180};
181
182/**
183 * Initialize a new `Base` reporter.
184 *
185 * All other reporters generally
186 * inherit from this reporter, providing
187 * stats such as test duration, number
188 * of tests passed / failed etc.
189 *
190 * @param {Runner} runner
191 * @api public
192 */
193
1941function Base(runner) {
1950 var self = this
196 , stats = this.stats = { suites: 0, tests: 0, passes: 0, failures: 0 }
197 , failures = this.failures = [];
198
1990 if (!runner) return;
2000 this.runner = runner;
201
2020 runner.on('start', function(){
2030 stats.start = new Date;
204 });
205
2060 runner.on('suite', function(suite){
2070 stats.suites = stats.suites || 0;
2080 suite.root || stats.suites++;
209 });
210
2110 runner.on('test end', function(test){
2120 stats.tests = stats.tests || 0;
2130 stats.tests++;
214 });
215
2160 runner.on('pass', function(test){
2170 stats.passes = stats.passes || 0;
218
2190 var medium = exports.slow / 2;
2200 test.speed = test.duration > exports.slow
221 ? 'slow'
222 : test.duration > medium
223 ? 'medium'
224 : 'fast';
225
2260 stats.passes++;
227 });
228
2290 runner.on('fail', function(test, err){
2300 stats.failures = stats.failures || 0;
2310 stats.failures++;
2320 test.err = err;
2330 failures.push(test);
234 });
235
2360 runner.on('end', function(){
2370 stats.end = new Date;
2380 stats.duration = new Date - stats.start;
239 });
240}
241
242/**
243 * Output common epilogue used by many of
244 * the bundled reporters.
245 *
246 * @api public
247 */
248
2491Base.prototype.epilogue = function(){
2500 var stats = this.stats
251 , fmt;
252
2530 console.log();
254
255 // failure
2560 if (stats.failures) {
2570 fmt = color('bright fail', ' &#226;&#156;&#150;')
258 + color('fail', ' %d of %d tests failed')
259 + color('light', ':')
260
2610 console.error(fmt, stats.failures, this.runner.total);
2620 Base.list(this.failures);
2630 console.error();
2640 return;
265 }
266
267 // pass
2680 fmt = color('bright pass', ' &#226;&#156;&#148;')
269 + color('green', ' %d tests complete')
270 + color('light', ' (%dms)');
271
2720 console.log(fmt, stats.tests || 0, stats.duration);
2730 console.log();
274};
275
276/**
277 * Pad the given `str` to `len`.
278 *
279 * @param {String} str
280 * @param {String} len
281 * @return {String}
282 * @api private
283 */
284
2851function pad(str, len) {
2860 str = String(str);
2870 return Array(len - str.length + 1).join(' ') + str;
288}
289
290/**
291 * Return a character diff for `err`.
292 *
293 * @param {Error} err
294 * @return {String}
295 * @api private
296 */
297
2981function errorDiff(err, type) {
2990 return diff['diff' + type](err.actual, err.expected).map(function(str){
3000 if (str.added) return colorLines('diff added', str.value);
3010 if (str.removed) return colorLines('diff removed', str.value);
3020 return str.value;
303 }).join('');
304}
305
306/**
307 * Color lines for `str`, using the color `name`.
308 *
309 * @param {String} name
310 * @param {String} str
311 * @return {String}
312 * @api private
313 */
314
3151function colorLines(name, str) {
3160 return str.split('\n').map(function(str){
3170 return color(name, str);
318 }).join('\n');
319}

reporters/dot.js

19%
21
4
17
LineHitsSource
1
2/**
3 * Module dependencies.
4 */
5
61var Base = require('./base')
7 , color = Base.color;
8
9/**
10 * Expose `Dot`.
11 */
12
131exports = module.exports = Dot;
14
15/**
16 * Initialize a new `Dot` matrix test reporter.
17 *
18 * @param {Runner} runner
19 * @api public
20 */
21
221function Dot(runner) {
230 Base.call(this, runner);
24
250 var self = this
26 , stats = this.stats
27 , width = Base.window.width * .75 | 0
28 , n = 0;
29
300 runner.on('start', function(){
310 process.stdout.write('\n ');
32 });
33
340 runner.on('pending', function(test){
350 process.stdout.write(color('pending', '.'));
36 });
37
380 runner.on('pass', function(test){
390 if (++n % width == 0) process.stdout.write('\n ');
400 if ('slow' == test.speed) {
410 process.stdout.write(color('bright yellow', '.'));
42 } else {
430 process.stdout.write(color(test.speed, '.'));
44 }
45 });
46
470 runner.on('fail', function(test, err){
480 if (++n % width == 0) process.stdout.write('\n ');
490 process.stdout.write(color('fail', '.'));
50 });
51
520 runner.on('end', function(){
530 console.log();
540 self.epilogue();
55 });
56}
57
58/**
59 * Inherit from `Base.prototype`.
60 */
61
621Dot.prototype.__proto__ = Base.prototype;

reporters/doc.js

13%
29
4
25
LineHitsSource
1
2/**
3 * Module dependencies.
4 */
5
61var Base = require('./base')
7 , utils = require('../utils');
8
9/**
10 * Expose `Doc`.
11 */
12
131exports = module.exports = Doc;
14
15/**
16 * Initialize a new `Doc` reporter.
17 *
18 * @param {Runner} runner
19 * @api public
20 */
21
221function Doc(runner) {
230 Base.call(this, runner);
24
250 var self = this
26 , stats = this.stats
27 , total = runner.total
28 , indents = 2;
29
300 function indent() {
310 return Array(indents).join(' ');
32 }
33
340 runner.on('suite', function(suite){
350 if (suite.root) return;
360 ++indents;
370 console.log('%s<section class="suite">', indent());
380 ++indents;
390 console.log('%s<h1>%s</h1>', indent(), suite.title);
400 console.log('%s<dl>', indent());
41 });
42
430 runner.on('suite end', function(suite){
440 if (suite.root) return;
450 console.log('%s</dl>', indent());
460 --indents;
470 console.log('%s</section>', indent());
480 --indents;
49 });
50
510 runner.on('pass', function(test){
520 console.log('%s <dt>%s</dt>', indent(), test.title);
530 var code = utils.escape(clean(test.fn.toString()));
540 console.log('%s <dd><pre><code>%s</code></pre></dd>', indent(), code);
55 });
56}
57
58/**
59 * Strip the function definition from `str`,
60 * and re-indent for pre whitespace.
61 */
62
631function clean(str) {
640 str = str
65 .replace(/^function *\(.*\) *{/, '')
66 .replace(/\s+\}$/, '');
67
680 var spaces = str.match(/^\n?( *)/)[1].length
69 , re = new RegExp('^ {' + spaces + '}', 'gm');
70
710 str = str.replace(re, '');
72
730 return str;
74}

reporters/tap.js

22%
18
4
14
LineHitsSource
1
2/**
3 * Module dependencies.
4 */
5
61var Base = require('./base')
7 , cursor = Base.cursor
8 , color = Base.color;
9
10/**
11 * Expose `TAP`.
12 */
13
141exports = module.exports = TAP;
15
16/**
17 * Initialize a new `TAP` reporter.
18 *
19 * @param {Runner} runner
20 * @api public
21 */
22
231function TAP(runner) {
240 Base.call(this, runner);
25
260 var self = this
27 , stats = this.stats
28 , total = runner.total
29 , n = 1;
30
310 runner.on('start', function(){
320 console.log('%d..%d', 1, total);
33 });
34
350 runner.on('test end', function(){
360 ++n;
37 });
38
390 runner.on('pending', function(test){
400 console.log('ok %d %s # SKIP -', n, title(test));
41 });
42
430 runner.on('pass', function(test){
440 console.log('ok %d %s', n, title(test));
45 });
46
470 runner.on('fail', function(test, err){
480 console.log('not ok %d %s', n, title(test));
490 console.log(err.stack.replace(/^/gm, ' '));
50 });
51}
52
53/**
54 * Return a TAP-safe title of `test`
55 *
56 * @param {Object} test
57 * @return {String}
58 * @api private
59 */
60
611function title(test) {
620 return test.fullTitle().replace(/#/g, '');
63}

reporters/json.js

23%
17
4
13
LineHitsSource
1
2/**
3 * Module dependencies.
4 */
5
61var Base = require('./base')
7 , cursor = Base.cursor
8 , color = Base.color;
9
10/**
11 * Expose `JSON`.
12 */
13
141exports = module.exports = JSONReporter;
15
16/**
17 * Initialize a new `JSON` reporter.
18 *
19 * @param {Runner} runner
20 * @api public
21 */
22
231function JSONReporter(runner) {
240 var self = this;
250 Base.call(this, runner);
26
270 var tests = []
28 , failures = []
29 , passes = [];
30
310 runner.on('test end', function(test){
320 tests.push(test);
33 });
34
350 runner.on('pass', function(test){
360 passes.push(test);
37 });
38
390 runner.on('fail', function(test){
400 failures.push(test);
41 });
42
430 runner.on('end', function(){
440 var obj = {
45 stats: self.stats
46 , tests: tests.map(clean)
47 , failures: failures.map(clean)
48 , passes: passes.map(clean)
49 };
50
510 process.stdout.write(JSON.stringify(obj, null, 2));
52 });
53}
54
55/**
56 * Return a plain-object representation of `test`
57 * free of cyclic properties etc.
58 *
59 * @param {Object} test
60 * @return {Object}
61 * @api private
62 */
63
641function clean(test) {
650 return {
66 title: test.title
67 , fullTitle: test.fullTitle()
68 , duration: test.duration
69 }
70}

reporters/html.js

12%
70
9
61
LineHitsSource
1
2/**
3 * Module dependencies.
4 */
5
61var Base = require('./base')
7 , utils = require('../utils')
8 , Progress = require('../browser/progress')
9 , escape = utils.escape;
10
11/**
12 * Expose `Doc`.
13 */
14
151exports = module.exports = HTML;
16
17/**
18 * Stats template.
19 */
20
211var statsTemplate = '<ul id="stats">'
22 + '<li class="progress"><canvas width="40" height="40"></canvas></li>'
23 + '<li class="passes">passes: <em>0</em></li>'
24 + '<li class="failures">failures: <em>0</em></li>'
25 + '<li class="duration">duration: <em>0</em>s</li>'
26 + '</ul>';
27
28/**
29 * Initialize a new `Doc` reporter.
30 *
31 * @param {Runner} runner
32 * @api public
33 */
34
351function HTML(runner) {
360 Base.call(this, runner);
37
380 var self = this
39 , stats = this.stats
40 , total = runner.total
41 , root = document.getElementById('mocha')
42 , stat = fragment(statsTemplate)
43 , items = stat.getElementsByTagName('li')
44 , passes = items[1].getElementsByTagName('em')[0]
45 , failures = items[2].getElementsByTagName('em')[0]
46 , duration = items[3].getElementsByTagName('em')[0]
47 , canvas = stat.getElementsByTagName('canvas')[0]
48 , stack = [root]
49 , progress
50 , ctx
51
520 if (canvas.getContext) {
530 ctx = canvas.getContext('2d');
540 progress = new Progress;
55 }
56
570 if (!root) return error('#mocha div missing, add it to your document');
58
590 root.appendChild(stat);
60
610 if (progress) progress.size(40);
62
630 runner.on('suite', function(suite){
640 if (suite.root) return;
65
66 // suite
670 var el = fragment('<div class="suite"><h1>%s</h1></div>', suite.title);
68
69 // container
700 stack[0].appendChild(el);
710 stack.unshift(document.createElement('div'));
720 el.appendChild(stack[0]);
73 });
74
750 runner.on('suite end', function(suite){
760 if (suite.root) return;
770 stack.shift();
78 });
79
800 runner.on('fail', function(test, err){
810 if (err.uncaught) runner.emit('test end', test);
82 });
83
840 runner.on('test end', function(test){
85 // TODO: add to stats
860 var percent = stats.tests / total * 100 | 0;
870 if (progress) progress.update(percent).draw(ctx);
88
89 // update stats
900 var ms = new Date - stats.start;
910 text(passes, stats.passes);
920 text(failures, stats.failures);
930 text(duration, (ms / 1000).toFixed(2));
94
95 // test
960 if ('passed' == test.state) {
970 var el = fragment('<div class="test pass"><h2>%e</h2></div>', test.title);
980 } else if (test.pending) {
990 var el = fragment('<div class="test pass pending"><h2>%e</h2></div>', test.title);
100 } else {
1010 var el = fragment('<div class="test fail"><h2>%e</h2></div>', test.title);
1020 var str = test.err.stack || test.err;
103
104 // FF / Opera do not add the message
1050 if (!~str.indexOf(test.err.message)) {
1060 str = test.err.message + '\n' + str;
107 }
108
109 // <=IE7 stringifies to [Object Error]. Since it can be overloaded, we
110 // check for the result of the stringifying.
1110 if ('[object Error]' == str) str = test.err.message;
112
1130 el.appendChild(fragment('<pre class="error">%e</pre>', str));
114 }
115
116 // toggle code
1170 var h2 = el.getElementsByTagName('h2')[0];
118
1190 on(h2, 'click', function(){
1200 pre.style.display = 'none' == pre.style.display
121 ? 'block'
122 : 'none';
123 });
124
125 // code
126 // TODO: defer
1270 if (!test.pending) {
1280 var pre = fragment('<pre><code>%e</code></pre>', clean(test.fn.toString()));
1290 el.appendChild(pre);
1300 pre.style.display = 'none';
131 }
132
1330 stack[0].appendChild(el);
134 });
135}
136
137/**
138 * Display error `msg`.
139 */
140
1411function error(msg) {
1420 document.body.appendChild(fragment('<div id="error">%s</div>', msg));
143}
144
145/**
146 * Return a DOM fragment from `html`.
147 */
148
1491function fragment(html) {
1500 var args = arguments
151 , div = document.createElement('div')
152 , i = 1;
153
1540 div.innerHTML = html.replace(/%([se])/g, function(_, type){
1550 switch (type) {
1560 case 's': return String(args[i++]);
1570 case 'e': return escape(args[i++]);
158 }
159 });
160
1610 return div.firstChild;
162}
163
164/**
165 * Set `el` text to `str`.
166 */
167
1681function text(el, str) {
1690 if (el.textContent) {
1700 el.textContent = str;
171 } else {
1720 el.innerText = str;
173 }
174}
175
176/**
177 * Listen on `event` with callback `fn`.
178 */
179
1801function on(el, event, fn) {
1810 if (el.addEventListener) {
1820 el.addEventListener(event, fn, false);
183 } else {
1840 el.attachEvent('on' + event, fn);
185 }
186}
187
188/**
189 * Strip the function definition from `str`,
190 * and re-indent for pre whitespace.
191 */
192
1931function clean(str) {
1940 str = str
195 .replace(/^function *\(.*\) *{/, '')
196 .replace(/\s+\}$/, '');
197
1980 var spaces = str.match(/^\n?( *)/)[1].length
199 , re = new RegExp('^ {' + spaces + '}', 'gm');
200
2010 str = str
202 .replace(re, '')
203 .replace(/^\s+/, '');
204
2050 return str;
206}

browser/progress.js

21%
37
8
29
LineHitsSource
1
2/**
3 * Expose `Progress`.
4 */
5
61module.exports = Progress;
7
8/**
9 * Initialize a new `Progress` indicator.
10 */
11
121function Progress() {
130 this.percent = 0;
140 this.size(0);
150 this.fontSize(11);
160 this.font('helvetica, arial, sans-serif');
17}
18
19/**
20 * Set progress size to `n`.
21 *
22 * @param {Number} n
23 * @return {Progress} for chaining
24 * @api public
25 */
26
271Progress.prototype.size = function(n){
280 this._size = n;
290 return this;
30};
31
32/**
33 * Set text to `str`.
34 *
35 * @param {String} str
36 * @return {Progress} for chaining
37 * @api public
38 */
39
401Progress.prototype.text = function(str){
410 this._text = str;
420 return this;
43};
44
45/**
46 * Set font size to `n`.
47 *
48 * @param {Number} n
49 * @return {Progress} for chaining
50 * @api public
51 */
52
531Progress.prototype.fontSize = function(n){
540 this._fontSize = n;
550 return this;
56};
57
58/**
59 * Set font `family`.
60 *
61 * @param {String} family
62 * @return {Progress} for chaining
63 */
64
651Progress.prototype.font = function(family){
660 this._font = family;
670 return this;
68};
69
70/**
71 * Update percentage to `n`.
72 *
73 * @param {Number} n
74 * @return {Progress} for chaining
75 */
76
771Progress.prototype.update = function(n){
780 this.percent = n;
790 return this;
80};
81
82/**
83 * Draw on `ctx`.
84 *
85 * @param {CanvasRenderingContext2d} ctx
86 * @return {Progress} for chaining
87 */
88
891Progress.prototype.draw = function(ctx){
900 var percent = Math.min(this.percent, 100)
91 , size = this._size
92 , half = size / 2
93 , x = half
94 , y = half
95 , rad = half - 1
96 , fontSize = this._fontSize;
97
980 ctx.font = fontSize + 'px ' + this._font;
99
1000 var angle = Math.PI * 2 * (percent / 100);
1010 ctx.clearRect(0, 0, size, size);
102
103 // outer circle
1040 ctx.strokeStyle = '#9f9f9f';
1050 ctx.beginPath();
1060 ctx.arc(x, y, rad, 0, angle, false);
1070 ctx.stroke();
108
109 // inner circle
1100 ctx.strokeStyle = '#eee';
1110 ctx.beginPath();
1120 ctx.arc(x, y, rad - 1, 0, angle, true);
1130 ctx.stroke();
114
115 // text
1160 var text = this._text || (percent | 0) + '%'
117 , w = ctx.measureText(text).width;
118
1190 ctx.fillText(
120 text
121 , x - w / 2 + 1
122 , y + fontSize / 2 - 1);
123
1240 return this;
125};

reporters/list.js

19%
21
4
17
LineHitsSource
1
2/**
3 * Module dependencies.
4 */
5
61var Base = require('./base')
7 , cursor = Base.cursor
8 , color = Base.color;
9
10/**
11 * Expose `List`.
12 */
13
141exports = module.exports = List;
15
16/**
17 * Initialize a new `List` test reporter.
18 *
19 * @param {Runner} runner
20 * @api public
21 */
22
231function List(runner) {
240 Base.call(this, runner);
25
260 var self = this
27 , stats = this.stats
28 , n = 0;
29
300 runner.on('start', function(){
310 console.log();
32 });
33
340 runner.on('test', function(test){
350 process.stdout.write(color('pass', ' ' + test.fullTitle() + ': '));
36 });
37
380 runner.on('pending', function(test){
390 var fmt = color('checkmark', ' -')
40 + color('pending', ' %s');
410 console.log(fmt, test.fullTitle());
42 });
43
440 runner.on('pass', function(test){
450 var fmt = color('checkmark', ' &#226;&#156;&#147;')
46 + color('pass', ' %s: ')
47 + color(test.speed, '%dms');
480 cursor.CR();
490 console.log(fmt, test.fullTitle(), test.duration);
50 });
51
520 runner.on('fail', function(test, err){
530 cursor.CR();
540 console.log(color('fail', ' %d) %s'), ++n, test.fullTitle());
55 });
56
570 runner.on('end', self.epilogue.bind(self));
58}
59
60/**
61 * Inherit from `Base.prototype`.
62 */
63
641List.prototype.__proto__ = Base.prototype;

reporters/spec.js

12%
33
4
29
LineHitsSource
1
2/**
3 * Module dependencies.
4 */
5
61var Base = require('./base')
7 , cursor = Base.cursor
8 , color = Base.color;
9
10/**
11 * Expose `Spec`.
12 */
13
141exports = module.exports = Spec;
15
16/**
17 * Initialize a new `Spec` test reporter.
18 *
19 * @param {Runner} runner
20 * @api public
21 */
22
231function Spec(runner) {
240 Base.call(this, runner);
25
260 var self = this
27 , stats = this.stats
28 , indents = 0
29 , n = 0;
30
310 function indent() {
320 return Array(indents).join(' ')
33 }
34
350 runner.on('start', function(){
360 console.log();
37 });
38
390 runner.on('suite', function(suite){
400 ++indents;
410 console.log(color('suite', '%s%s'), indent(), suite.title);
42 });
43
440 runner.on('suite end', function(suite){
450 --indents;
460 if (1 == indents) console.log();
47 });
48
490 runner.on('test', function(test){
500 process.stdout.write(indent() + color('pass', ' &#226;&#151;&#166; ' + test.title + ': '));
51 });
52
530 runner.on('pending', function(test){
540 var fmt = indent() + color('pending', ' - %s');
550 console.log(fmt, test.title);
56 });
57
580 runner.on('pass', function(test){
590 if ('fast' == test.speed) {
600 var fmt = indent()
61 + color('checkmark', ' &#226;&#156;&#147;')
62 + color('pass', ' %s ');
630 cursor.CR();
640 console.log(fmt, test.title);
65 } else {
660 var fmt = indent()
67 + color('checkmark', ' &#226;&#156;&#147;')
68 + color('pass', ' %s ')
69 + color(test.speed, '(%dms)');
700 cursor.CR();
710 console.log(fmt, test.title, test.duration);
72 }
73 });
74
750 runner.on('fail', function(test, err){
760 cursor.CR();
770 console.log(indent() + color('fail', ' %d) %s'), ++n, test.title);
78 });
79
800 runner.on('end', self.epilogue.bind(self));
81}
82
83/**
84 * Inherit from `Base.prototype`.
85 */
86
871Spec.prototype.__proto__ = Base.prototype;

reporters/progress.js

17%
29
5
24
LineHitsSource
1
2/**
3 * Module dependencies.
4 */
5
61var Base = require('./base')
7 , cursor = Base.cursor
8 , color = Base.color;
9
10/**
11 * Expose `Progress`.
12 */
13
141exports = module.exports = Progress;
15
16/**
17 * General progress bar color.
18 */
19
201Base.colors.progress = 90;
21
22/**
23 * Initialize a new `Progress` bar test reporter.
24 *
25 * @param {Runner} runner
26 * @param {Object} options
27 * @api public
28 */
29
301function Progress(runner, options) {
310 Base.call(this, runner);
32
330 var self = this
34 , options = options || {}
35 , stats = this.stats
36 , width = Base.window.width * .50 | 0
37 , total = runner.total
38 , complete = 0
39 , max = Math.max;
40
41 // default chars
420 options.open = options.open || '[';
430 options.complete = options.complete || '&#226;&#150;&#172;';
440 options.incomplete = options.incomplete || '&#226;&#139;&#133;';
450 options.close = options.close || ']';
460 options.verbose = false;
47
48 // tests started
490 runner.on('start', function(){
500 console.log();
510 cursor.hide();
52 });
53
54 // tests complete
550 runner.on('test end', function(){
560 var incomplete = total - complete
57 , percent = complete++ / total
58 , n = width * percent | 0
59 , i = width - n;
60
610 cursor.CR();
620 process.stdout.write('\033[J');
630 process.stdout.write(color('progress', ' ' + options.open));
640 process.stdout.write(Array(n).join(options.complete));
650 process.stdout.write(Array(i).join(options.incomplete));
660 process.stdout.write(color('progress', options.close));
670 if (options.verbose) {
680 process.stdout.write(color('progress', ' ' + complete + ' of ' + total));
69 }
70 });
71
72 // tests are complete, output some stats
73 // and the failures if any
740 runner.on('end', function(){
750 cursor.show();
760 console.log();
770 self.epilogue();
78 });
79}
80
81/**
82 * Inherit from `Base.prototype`.
83 */
84
851Progress.prototype.__proto__ = Base.prototype;

reporters/landing.js

21%
32
7
25
LineHitsSource
1
2/**
3 * Module dependencies.
4 */
5
61var Base = require('./base')
7 , cursor = Base.cursor
8 , color = Base.color;
9
10/**
11 * Expose `Landing`.
12 */
13
141exports = module.exports = Landing;
15
16/**
17 * Airplane color.
18 */
19
201Base.colors.plane = 0;
21
22/**
23 * Airplane crash color.
24 */
25
261Base.colors['plane crash'] = 31;
27
28/**
29 * Runway color.
30 */
31
321Base.colors.runway = 90;
33
34/**
35 * Initialize a new `Landing` reporter.
36 *
37 * @param {Runner} runner
38 * @api public
39 */
40
411function Landing(runner) {
420 Base.call(this, runner);
43
440 var self = this
45 , stats = this.stats
46 , width = Base.window.width * .75 | 0
47 , total = runner.total
48 , stream = process.stdout
49 , plane = color('plane', '&#226;&#156;&#136;')
50 , crashed = -1
51 , n = 0;
52
530 function runway() {
540 var buf = Array(width).join('-');
550 return ' ' + color('runway', buf);
56 }
57
580 runner.on('start', function(){
590 stream.write('\n ');
600 cursor.hide();
61 });
62
630 runner.on('test end', function(test){
64 // check if the plane crashed
650 var col = -1 == crashed
66 ? width * ++n / total | 0
67 : crashed;
68
69 // show the crash
700 if ('failed' == test.state) {
710 plane = color('plane crash', '&#226;&#156;&#136;');
720 crashed = col;
73 }
74
75 // render landing strip
760 stream.write('\033[4F\n\n');
770 stream.write(runway());
780 stream.write('\n ');
790 stream.write(color('runway', Array(col).join('&#226;&#139;&#133;')));
800 stream.write(plane)
810 stream.write(color('runway', Array(width - col).join('&#226;&#139;&#133;') + '\n'));
820 stream.write(runway());
830 stream.write('\033[0m');
84 });
85
860 runner.on('end', function(){
870 cursor.show();
880 console.log();
890 self.epilogue();
90 });
91}
92
93/**
94 * Inherit from `Base.prototype`.
95 */
96
971Landing.prototype.__proto__ = Base.prototype;

reporters/json-cov.js

14%
48
7
41
LineHitsSource
1
2/**
3 * Module dependencies.
4 */
5
61var Base = require('./base');
7
8/**
9 * Expose `JSONCov`.
10 */
11
121exports = module.exports = JSONCov;
13
14/**
15 * Initialize a new `JsCoverage` reporter.
16 *
17 * @param {Runner} runner
18 * @param {Boolean} output
19 * @api public
20 */
21
221function JSONCov(runner, output) {
230 var self = this
24 , output = 1 == arguments.length ? true : output;
25
260 Base.call(this, runner);
27
280 var tests = []
29 , failures = []
30 , passes = [];
31
320 runner.on('test end', function(test){
330 tests.push(test);
34 });
35
360 runner.on('pass', function(test){
370 passes.push(test);
38 });
39
400 runner.on('fail', function(test){
410 failures.push(test);
42 });
43
440 runner.on('end', function(){
450 var cov = global._$jscoverage || {};
460 var result = self.cov = map(cov);
470 result.stats = self.stats;
480 result.tests = tests.map(clean);
490 result.failures = failures.map(clean);
500 result.passes = passes.map(clean);
510 if (!output) return;
520 process.stdout.write(JSON.stringify(result, null, 2 ));
53 });
54}
55
56/**
57 * Map jscoverage data to a JSON structure
58 * suitable for reporting.
59 *
60 * @param {Object} cov
61 * @return {Object}
62 * @api private
63 */
64
651function map(cov) {
660 var ret = {
67 instrumentation: 'node-jscoverage'
68 , sloc: 0
69 , hits: 0
70 , misses: 0
71 , coverage: 0
72 , files: []
73 };
74
750 for (var filename in cov) {
760 var data = coverage(filename, cov[filename]);
770 ret.files.push(data);
780 ret.hits += data.hits;
790 ret.misses += data.misses;
800 ret.sloc += data.sloc;
81 }
82
830 if (ret.sloc > 0) {
840 ret.coverage = (ret.hits / ret.sloc) * 100;
85 }
86
870 return ret;
881};
89
90/**
91 * Map jscoverage data for a single source file
92 * to a JSON structure suitable for reporting.
93 *
94 * @param {String} filename name of the source file
95 * @param {Object} data jscoverage coverage data
96 * @return {Object}
97 * @api private
98 */
99
1001function coverage(filename, data) {
1010 var ret = {
102 filename: filename,
103 coverage: 0,
104 hits: 0,
105 misses: 0,
106 sloc: 0,
107 source: {}
108 };
109
1100 data.source.forEach(function(line, num){
1110 num++;
112
1130 if (data[num] === 0) {
1140 ret.misses++;
1150 ret.sloc++;
1160 } else if (data[num] !== undefined) {
1170 ret.hits++;
1180 ret.sloc++;
119 }
120
1210 ret.source[num] = {
122 source: line
123 , coverage: data[num] === undefined
124 ? ''
125 : data[num]
126 };
127 });
128
1290 ret.coverage = ret.hits / ret.sloc * 100;
130
1310 return ret;
132}
133
134/**
135 * Return a plain-object representation of `test`
136 * free of cyclic properties etc.
137 *
138 * @param {Object} test
139 * @return {Object}
140 * @api private
141 */
142
1431function clean(test) {
1440 return {
145 title: test.title
146 , fullTitle: test.fullTitle()
147 , duration: test.duration
148 }
149}

reporters/html-cov.js

33%
12
4
8
LineHitsSource
1
2/**
3 * Module dependencies.
4 */
5
61var JSONCov = require('./json-cov')
7 , fs = require('fs');
8
9/**
10 * Expose `HTMLCov`.
11 */
12
131exports = module.exports = HTMLCov;
14
15/**
16 * Initialize a new `JsCoverage` reporter.
17 *
18 * @param {Runner} runner
19 * @api public
20 */
21
221function HTMLCov(runner) {
230 var jade = require('jade')
24 , file = __dirname + '/templates/coverage.jade'
25 , str = fs.readFileSync(file, 'utf8')
26 , fn = jade.compile(str, { filename: file })
27 , self = this;
28
290 JSONCov.call(this, runner, false);
30
310 runner.on('end', function(){
320 process.stdout.write(fn({
33 cov: self.cov
34 , coverageClass: coverageClass
35 }));
36 });
37}
38
391function coverageClass(n) {
400 if (n >= 75) return 'high';
410 if (n >= 50) return 'medium';
420 if (n >= 25) return 'low';
430 return 'terrible';
44}

reporters/json-stream.js

26%
15
4
11
LineHitsSource
1
2/**
3 * Module dependencies.
4 */
5
61var Base = require('./base')
7 , color = Base.color;
8
9/**
10 * Expose `List`.
11 */
12
131exports = module.exports = List;
14
15/**
16 * Initialize a new `List` test reporter.
17 *
18 * @param {Runner} runner
19 * @api public
20 */
21
221function List(runner) {
230 Base.call(this, runner);
24
250 var self = this
26 , stats = this.stats
27 , total = runner.total;
28
290 runner.on('start', function(){
300 console.log(JSON.stringify(['start', { total: total }]));
31 });
32
330 runner.on('pass', function(test){
340 console.log(JSON.stringify(['pass', clean(test)]));
35 });
36
370 runner.on('fail', function(test, err){
380 console.log(JSON.stringify(['fail', clean(test)]));
39 });
40
410 runner.on('end', function(){
420 process.stdout.write(JSON.stringify(['end', self.stats]));
43 });
44}
45
46/**
47 * Return a plain-object representation of `test`
48 * free of cyclic properties etc.
49 *
50 * @param {Object} test
51 * @return {Object}
52 * @api private
53 */
54
551function clean(test) {
560 return {
57 title: test.title
58 , fullTitle: test.fullTitle()
59 , duration: test.duration
60 }
61}

reporters/xunit.js

23%
30
7
23
LineHitsSource
1
2/**
3 * Module dependencies.
4 */
5
61var Base = require('./base')
7 , utils = require('../utils')
8 , escape = utils.escape;
9
10/**
11 * Expose `XUnit`.
12 */
13
141exports = module.exports = XUnit;
15
16/**
17 * Initialize a new `XUnit` reporter.
18 *
19 * @param {Runner} runner
20 * @api public
21 */
22
231function XUnit(runner) {
240 Base.call(this, runner);
250 var stats = this.stats
26 , tests = []
27 , self = this;
28
290 runner.on('test end', function(test){
300 tests.push(test);
31 });
32
330 runner.on('end', function(){
340 console.log(tag('testsuite', {
35 name: 'Mocha Tests'
36 , tests: stats.tests
37 , failures: stats.failures
38 , errors: stats.failures
39 , skip: stats.tests - stats.failures - stats.passes
40 , timestamp: (new Date).toUTCString()
41 , time: stats.duration / 1000
42 }, false));
43
440 tests.forEach(test);
450 console.log('</testsuite>');
46 });
47}
48
49/**
50 * Inherit from `Base.prototype`.
51 */
52
531XUnit.prototype.__proto__ = Base.prototype;
54
55/**
56 * Output tag for the given `test.`
57 */
58
591function test(test) {
600 var attrs = {
61 classname: test.parent.fullTitle()
62 , name: test.title
63 , time: test.duration / 1000
64 };
65
660 if ('failed' == test.state) {
670 var err = test.err;
680 attrs.message = escape(err.message);
690 console.log(tag('testcase', attrs, false, tag('failure', attrs, false, cdata(err.stack))));
700 } else if (test.pending) {
710 console.log(tag('testcase', attrs, false, tag('skipped', {}, true)));
72 } else {
730 console.log(tag('testcase', attrs, true) );
74 }
75}
76
77/**
78 * HTML tag helper.
79 */
80
811function tag(name, attrs, close, content) {
820 var end = close ? '/>' : '>'
83 , pairs = []
84 , tag;
85
860 for (var key in attrs) {
870 pairs.push(key + '="' + escape(attrs[key]) + '"');
88 }
89
900 tag = '<' + name + (pairs.length ? ' ' + pairs.join(' ') : '') + end;
910 if (content) tag += content + '</' + name + end;
920 return tag;
93}
94
95/**
96 * Return cdata escaped CDATA `str`.
97 */
98
991function cdata(str) {
1000 return '<![CDATA[' + escape(str) + ']]>';
101}

context.js

81%
11
9
2
LineHitsSource
1
2/**
3 * Expose `Context`.
4 */
5
61module.exports = Context;
7
8/**
9 * Initialize a new `Context`.
10 *
11 * @api private
12 */
13
141function Context(){}
15
16/**
17 * Set the context `Test` to `test`.
18 *
19 * @param {Test} test
20 * @return {Context}
21 * @api private
22 */
23
241Context.prototype.test = function(test){
25168 this._test = test;
26168 return this;
27};
28
29/**
30 * Set test timeout `ms`.
31 *
32 * @param {Number} ms
33 * @return {Context} self
34 * @api private
35 */
36
371Context.prototype.timeout = function(ms){
381 this._test.timeout(ms);
391 return this;
40};
41
42/**
43 * Inspect the context void of `._test`.
44 *
45 * @return {String}
46 * @api private
47 */
48
491Context.prototype.inspect = function(){
500 return JSON.stringify(this, function(key, val){
510 return '_test' == key
52 ? undefined
53 : val;
54 }, 2);
55};

runner.js

85%
154
131
23
LineHitsSource
1
2/**
3 * Module dependencies.
4 */
5
61var EventEmitter = require('events').EventEmitter
7 , debug = require('debug')('runner')
8 , Test = require('./test')
9 , utils = require('./utils')
10 , noop = function(){};
11
12/**
13 * Expose `Runner`.
14 */
15
161module.exports = Runner;
17
18/**
19 * Initialize a `Runner` for the given `suite`.
20 *
21 * Events:
22 *
23 * - `start` execution started
24 * - `end` execution complete
25 * - `suite` (suite) test suite execution started
26 * - `suite end` (suite) all tests (and sub-suites) have finished
27 * - `test` (test) test execution started
28 * - `test end` (test) test completed
29 * - `hook` (hook) hook execution started
30 * - `hook end` (hook) hook complete
31 * - `pass` (test) test passed
32 * - `fail` (test, err) test failed
33 *
34 * @api public
35 */
36
371function Runner(suite) {
3811 var self = this;
3911 this._globals = [];
4011 this.suite = suite;
4111 this.total = suite.total();
4211 this.failures = 0;
4391 this.on('test end', function(test){ self.checkGlobals(test); });
44100 this.on('hook end', function(hook){ self.checkGlobals(hook); });
4511 this.grep(/.*/);
4611 this.globals(utils.keys(global).concat(['errno']));
47}
48
49/**
50 * Inherit from `EventEmitter.prototype`.
51 */
52
531Runner.prototype.__proto__ = EventEmitter.prototype;
54
55/**
56 * Run tests with full titles matching `re`.
57 *
58 * @param {RegExp} re
59 * @return {Runner} for chaining
60 * @api public
61 */
62
631Runner.prototype.grep = function(re){
6411 debug('grep %s', re);
6511 this._grep = re;
6611 return this;
67};
68
69/**
70 * Allow the given `arr` of globals.
71 *
72 * @param {Array} arr
73 * @return {Runner} for chaining
74 * @api public
75 */
76
771Runner.prototype.globals = function(arr){
7819 if (0 == arguments.length) return this._globals;
7913 debug('globals %j', arr);
8013 utils.forEach(arr, function(arr){
81364 this._globals.push(arr);
82 }, this);
8313 return this;
84};
85
86/**
87 * Check for global variable leaks.
88 *
89 * @api private
90 */
91
921Runner.prototype.checkGlobals = function(test){
93173 if (this.ignoreLeaks) return;
94
95173 var leaks = utils.filter(utils.keys(global), function(key){
965479 return !~utils.indexOf(this._globals, key) && (!global.navigator || 'onerror' !== key);
97 }, this);
98
99173 this._globals = this._globals.concat(leaks);
100
101173 if (leaks.length > 1) {
1021 this.fail(test, new Error('global leaks detected: ' + leaks.join(', ') + ''));
103172 } else if (leaks.length) {
1041 this.fail(test, new Error('global leak detected: ' + leaks[0]));
105 }
106};
107
108/**
109 * Fail the given `test`.
110 *
111 * @param {Test} test
112 * @param {Error} err
113 * @api private
114 */
115
1161Runner.prototype.fail = function(test, err){
11710 ++this.failures;
11810 test.state = 'failed';
11910 this.emit('fail', test, err);
120};
121
122/**
123 * Fail the given `hook` with `err`.
124 *
125 * Hook failures (currently) hard-end due
126 * to that fact that a failing hook will
127 * surely cause subsequent tests to fail,
128 * causing jumbled reporting.
129 *
130 * @param {Hook} hook
131 * @param {Error} err
132 * @api private
133 */
134
1351Runner.prototype.failHook = function(hook, err){
1364 this.fail(hook, err);
1374 this.emit('end');
138};
139
140/**
141 * Run hook `name` callbacks and then invoke `fn()`.
142 *
143 * @param {String} name
144 * @param {Function} function
145 * @api private
146 */
147
1481Runner.prototype.hook = function(name, fn){
149672 var suite = this.suite
150 , hooks = suite['_' + name]
151 , ms = suite._timeout
152 , self = this
153 , timer;
154
155672 function next(i) {
156761 var hook = hooks[i];
1571433 if (!hook) return fn();
15889 self.currentRunnable = hook;
15989 hook.ctx.test(self.test);
160
16189 self.emit('hook', hook);
162
16389 hook.on('error', function(err){
1640 self.failHook(hook, err);
165 });
166
16789 hook.run(function(err){
16889 hook.removeAllListeners('error');
16989 if (err) return self.failHook(hook, err);
17089 self.emit('hook end', hook);
17189 next(++i);
172 });
173 }
174
175672 process.nextTick(function(){
176672 next(0);
177 });
178};
179
180/**
181 * Run hook `name` for the given array of `suites`
182 * in order, and callback `fn(err)`.
183 *
184 * @param {String} name
185 * @param {Array} suites
186 * @param {Function} fn
187 * @api private
188 */
189
1901Runner.prototype.hooks = function(name, suites, fn){
191158 var self = this
192 , orig = this.suite;
193
194158 function next(suite) {
195674 self.suite = suite;
196
197674 if (!suite) {
198158 self.suite = orig;
199158 return fn();
200 }
201
202516 self.hook(name, function(err){
203516 if (err) {
2040 self.suite = orig;
2050 return fn(err);
206 }
207
208516 next(suites.pop());
209 });
210 }
211
212158 next(suites.pop());
213};
214
215/**
216 * Run hooks from the top level down.
217 *
218 * @param {String} name
219 * @param {Function} fn
220 * @api private
221 */
222
2231Runner.prototype.hookUp = function(name, fn){
22479 var suites = [this.suite].concat(this.parents()).reverse();
22579 this.hooks(name, suites, fn);
226};
227
228/**
229 * Run hooks from the bottom up.
230 *
231 * @param {String} name
232 * @param {Function} fn
233 * @api private
234 */
235
2361Runner.prototype.hookDown = function(name, fn){
23779 var suites = [this.suite].concat(this.parents());
23879 this.hooks(name, suites, fn);
239};
240
241/**
242 * Return an array of parent Suites from
243 * closest to furthest.
244 *
245 * @return {Array}
246 * @api private
247 */
248
2491Runner.prototype.parents = function(){
250158 var suite = this.suite
251 , suites = [];
252516 while (suite = suite.parent) suites.push(suite);
253158 return suites;
254};
255
256/**
257 * Run the current test and callback `fn(err)`.
258 *
259 * @param {Function} fn
260 * @api private
261 */
262
2631Runner.prototype.runTest = function(fn){
26479 var test = this.test
265 , self = this;
266
26779 try {
26879 test.ctx.test(test);
26979 test.on('error', function(err){
2700 self.fail(test, err);
271 });
27279 test.run(fn);
273 } catch (err) {
2740 fn(err);
275 }
276};
277
278/**
279 * Run tests in the given `suite` and invoke
280 * the callback `fn()` when complete.
281 *
282 * @param {Suite} suite
283 * @param {Function} fn
284 * @api private
285 */
286
2871Runner.prototype.runTests = function(suite, fn){
28878 var self = this
289 , tests = suite.tests
290 , test;
291
29278 function next(err) {
293 // if we bail after first err
294158 if (self.failures && suite._bail) return fn();
295
296 // next test
297158 test = tests.shift();
298
299 // all done
300236 if (!test) return fn();
301
302 // grep
30380 if (!self._grep.test(test.fullTitle())) return next();
304
305 // pending
30680 if (test.pending) {
3071 self.emit('pending', test);
3081 self.emit('test end', test);
3091 return next();
310 }
311
312 // execute test and hook(s)
31379 self.emit('test', self.test = test);
31479 self.hookDown('beforeEach', function(){
31579 self.currentRunnable = self.test;
31679 self.runTest(function(err){
31779 test = self.test;
318
31979 if (err) {
3200 self.fail(test, err);
3210 self.emit('test end', test);
3220 return self.hookUp('afterEach', next);
323 }
324
32579 test.state = 'passed';
32679 self.emit('pass', test);
32779 self.emit('test end', test);
32879 self.hookUp('afterEach', next);
329 });
330 });
331 }
332
33378 this.next = next;
33478 next();
335};
336
337/**
338 * Run the given `suite` and invoke the
339 * callback `fn()` when complete.
340 *
341 * @param {Suite} suite
342 * @param {Function} fn
343 * @api private
344 */
345
3461Runner.prototype.runSuite = function(suite, fn){
34778 var self = this
348 , i = 0;
349
35078 debug('run suite %s', suite.fullTitle());
35178 this.emit('suite', this.suite = suite);
352
35378 function next() {
354155 var curr = suite.suites[i++];
355233 if (!curr) return done();
35677 self.runSuite(curr, next);
357 }
358
35978 function done() {
36078 self.suite = suite;
36178 self.hook('afterAll', function(){
36278 self.emit('suite end', suite);
36378 fn();
364 });
365 }
366
36778 this.hook('beforeAll', function(){
36878 self.runTests(suite, next);
369 });
370};
371
372/**
373 * Handle uncaught exceptions.
374 *
375 * @param {Error} err
376 * @api private
377 */
378
3791Runner.prototype.uncaught = function(err){
3800 debug('uncaught exception');
3810 var runnable = this.currentRunnable;
3820 if ('failed' == runnable.state) return;
3830 runnable.clearTimeout();
3840 err.uncaught = true;
3850 this.fail(runnable, err);
386
387 // recover from test
3880 if ('test' == runnable.type) {
3890 this.emit('test end', runnable);
3900 this.hookUp('afterEach', this.next);
3910 return;
392 }
393
394 // bail on hooks
3950 this.emit('end');
396};
397
398/**
399 * Run the root suite and invoke `fn(failures)`
400 * on completion.
401 *
402 * @param {Function} fn
403 * @return {Runner} for chaining
404 * @api public
405 */
406
4071Runner.prototype.run = function(fn){
4081 var self = this
409 , fn = fn || function(){};
410
4111 debug('start');
412
413 // callback
4141 this.on('end', function(){
4150 debug('end');
4160 process.removeListener('uncaughtException', this.uncaught);
4170 fn(self.failures);
418 });
419
420 // run suites
4211 this.emit('start');
4221 this.runSuite(this.suite, function(){
4231 debug('finished running');
4241 self.emit('end');
425 });
426
427 // uncaught exception
4281 process.on('uncaughtException', function(err){
4290 self.uncaught(err);
430 });
431
4321 return this;
433};