Socket
Socket
Sign inDemoInstall

mocha

Package Overview
Dependencies
Maintainers
1
Versions
199
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

mocha - npm Package Compare versions

Comparing version 0.3.6 to 0.4.0

_mocha.js

10

History.md
0.4.0 / 2011-12-14
==================
* Added support for test-specific timeouts via `this.timeout(0)`. Closes #134
* Added guillermo's client-side EventEmitter. Closes #132
* Added progress indicator to the HTML reporter
* Fixed slow browser tests. Closes #135
* Fixed "suite" color for light terminals
* Fixed `require()` leak spotted by [guillermo]
0.3.6 / 2011-12-09

@@ -3,0 +13,0 @@ ==================

167

lib/browser/events.js
/**
* Expose `EventEmitter`.
* Module exports.
*/

@@ -9,25 +9,58 @@

/**
* Slice reference.
* Check if `obj` is an array.
*/
var slice = [].slice;
function isArray(obj) {
return '[object Array]' == {}.toString.call(obj);
}
/**
* EventEmitter.
* Event emitter constructor.
*
* @api public.
*/
function EventEmitter() {
this.callbacks = {};
function EventEmitter(){};
/**
* Adds a listener.
*
* @api public
*/
EventEmitter.prototype.on = function (name, fn) {
if (!this.$events) {
this.$events = {};
}
if (!this.$events[name]) {
this.$events[name] = fn;
} else if (isArray(this.$events[name])) {
this.$events[name].push(fn);
} else {
this.$events[name] = [this.$events[name], fn];
}
return this;
};
EventEmitter.prototype.addListener = EventEmitter.prototype.on;
/**
* Listen on the given `event` with `fn`.
* Adds a volatile listener.
*
* @param {String} event
* @param {Function} fn
* @api public
*/
EventEmitter.prototype.on = function(event, fn){
(this.callbacks[event] = this.callbacks[event] || [])
.push(fn);
EventEmitter.prototype.once = function (name, fn) {
var self = this;
function on () {
self.removeListener(name, on);
fn.apply(this, arguments);
};
on.listener = fn;
this.on(name, on);
return this;

@@ -37,15 +70,32 @@ };

/**
* Emit `event` with the given args.
* Removes a listener.
*
* @param {String} event
* @param {Mixed} ...
* @api public
*/
EventEmitter.prototype.emit = function(event){
var args = slice.call(arguments, 1)
, callbacks = this.callbacks[event];
EventEmitter.prototype.removeListener = function (name, fn) {
if (this.$events && this.$events[name]) {
var list = this.$events[name];
if (callbacks) {
for (var i = 0, len = callbacks.length; i < len; ++i) {
callbacks[i].apply(this, args)
if (isArray(list)) {
var pos = -1;
for (var i = 0, l = list.length; i < l; i++) {
if (list[i] === fn || (list[i].listener && list[i].listener === fn)) {
pos = i;
break;
}
}
if (pos < 0) {
return this;
}
list.splice(pos, 1);
if (!list.length) {
delete this.$events[name];
}
} else if (list === fn || (list.listener && list.listener === fn)) {
delete this.$events[name];
}

@@ -56,1 +106,76 @@ }

};
/**
* Removes all listeners for an event.
*
* @api public
*/
EventEmitter.prototype.removeAllListeners = function (name) {
if (name === undefined) {
this.$events = {};
return this;
}
if (this.$events && this.$events[name]) {
this.$events[name] = null;
}
return this;
};
/**
* Gets all listeners for a certain event.
*
* @api publci
*/
EventEmitter.prototype.listeners = function (name) {
if (!this.$events) {
this.$events = {};
}
if (!this.$events[name]) {
this.$events[name] = [];
}
if (!isArray(this.$events[name])) {
this.$events[name] = [this.$events[name]];
}
return this.$events[name];
};
/**
* Emits an event.
*
* @api public
*/
EventEmitter.prototype.emit = function (name) {
if (!this.$events) {
return false;
}
var handler = this.$events[name];
if (!handler) {
return false;
}
var args = [].slice.call(arguments, 1);
if ('function' == typeof handler) {
handler.apply(this, args);
} else if (isArray(handler)) {
var listeners = handler.slice();
for (var i = 0, l = listeners.length; i < l; i++) {
listeners[i].apply(this, args);
}
} else {
return false;
}
return true;
};

2

lib/mocha.js

@@ -12,3 +12,3 @@

exports.version = '0.3.6';
exports.version = '0.4.0';

@@ -15,0 +15,0 @@ exports.interfaces = require('./interfaces');

@@ -37,3 +37,3 @@

, 'pending': 36
, 'suite': '40'
, 'suite': 0
, 'error title': 0

@@ -40,0 +40,0 @@ , 'error message': 31

@@ -7,3 +7,4 @@

var Base = require('./base')
, utils = require('../utils');
, utils = require('../utils')
, Progress = require('../browser/progress');

@@ -21,2 +22,3 @@ /**

var statsTemplate = '<ul id="stats">'
+ '<li class="progress"><canvas width="50" height="50"></canvas></li>'
+ '<li class="passes">passes: <em>0</em></li>'

@@ -44,5 +46,9 @@ + '<li class="failures">failures: <em>0</em></li>'

, stack = [root]
, stat = $(statsTemplate).appendTo(root);
, stat = $(statsTemplate).appendTo(root)
, canvas = stat.find('canvas').get(0)
, ctx = canvas.getContext('2d')
, progress = new Progress;
if (!root.length) return error('#mocha div missing, add it to your document');
progress.size(50);

@@ -67,2 +73,8 @@ runner.on('suite', function(suite){

runner.on('test end', function(test){
// TODO: add to stats
var percent = stats.tests / total * 100 | 0;
// update progress bar
progress.update(percent).draw(ctx);
// update stats

@@ -69,0 +81,0 @@ var ms = new Date - stats.start;

@@ -39,10 +39,26 @@

runner.on('pending', function(test){
console.log('ok %d %s # SKIP -', n, title(test));
});
runner.on('pass', function(test){
console.log('ok %d %s', n, test.fullTitle());
console.log('ok %d %s', n, title(test));
});
runner.on('fail', function(test, err){
console.log('not ok %d %s', n, test.fullTitle());
console.log('not ok %d %s', n, title(test));
console.log(err.stack.replace(/^/gm, ' '));
});
}
}
/**
* Return a TAP-safe title of `test`
*
* @param {Object} test
* @return {String}
* @api private
*/
function title(test) {
return test.fullTitle().replace(/#/g, '');
}

@@ -49,2 +49,3 @@

this._timeout = ms;
if (this.timer) this.resetTimeout();
return this;

@@ -76,2 +77,20 @@ };

/**
* Reset the timeout.
*
* @api private
*/
Runnable.prototype.resetTimeout = function(){
var self = this
, ms = this.timeout();
this.clearTimeout();
if (ms) {
this.timer = setTimeout(function(){
self.callback(new Error('timeout of ' + ms + 'ms exceeded'));
}, ms);
}
};
/**
* Run the test and invoke `fn(err)`.

@@ -92,5 +111,7 @@ *

if (this.async) {
this.timer = setTimeout(function(){
done(new Error('timeout of ' + ms + 'ms exceeded'));
}, ms);
if (ms) {
this.timer = setTimeout(function(){
done(new Error('timeout of ' + ms + 'ms exceeded'));
}, ms);
}
}

@@ -114,2 +135,5 @@

// for .resetTimeout()
this.callback = done;
// async

@@ -116,0 +140,0 @@ if (this.async) {

@@ -0,2 +1,4 @@

;(function(){
// CommonJS require()

@@ -61,3 +63,3 @@

/**
* Expose `EventEmitter`.
* Module exports.
*/

@@ -68,25 +70,58 @@

/**
* Slice reference.
* Check if `obj` is an array.
*/
var slice = [].slice;
function isArray(obj) {
return '[object Array]' == {}.toString.call(obj);
}
/**
* EventEmitter.
* Event emitter constructor.
*
* @api public.
*/
function EventEmitter() {
this.callbacks = {};
function EventEmitter(){};
/**
* Adds a listener.
*
* @api public
*/
EventEmitter.prototype.on = function (name, fn) {
if (!this.$events) {
this.$events = {};
}
if (!this.$events[name]) {
this.$events[name] = fn;
} else if (isArray(this.$events[name])) {
this.$events[name].push(fn);
} else {
this.$events[name] = [this.$events[name], fn];
}
return this;
};
EventEmitter.prototype.addListener = EventEmitter.prototype.on;
/**
* Listen on the given `event` with `fn`.
* Adds a volatile listener.
*
* @param {String} event
* @param {Function} fn
* @api public
*/
EventEmitter.prototype.on = function(event, fn){
(this.callbacks[event] = this.callbacks[event] || [])
.push(fn);
EventEmitter.prototype.once = function (name, fn) {
var self = this;
function on () {
self.removeListener(name, on);
fn.apply(this, arguments);
};
on.listener = fn;
this.on(name, on);
return this;

@@ -96,15 +131,32 @@ };

/**
* Emit `event` with the given args.
* Removes a listener.
*
* @param {String} event
* @param {Mixed} ...
* @api public
*/
EventEmitter.prototype.emit = function(event){
var args = slice.call(arguments, 1)
, callbacks = this.callbacks[event];
EventEmitter.prototype.removeListener = function (name, fn) {
if (this.$events && this.$events[name]) {
var list = this.$events[name];
if (callbacks) {
for (var i = 0, len = callbacks.length; i < len; ++i) {
callbacks[i].apply(this, args)
if (isArray(list)) {
var pos = -1;
for (var i = 0, l = list.length; i < l; i++) {
if (list[i] === fn || (list[i].listener && list[i].listener === fn)) {
pos = i;
break;
}
}
if (pos < 0) {
return this;
}
list.splice(pos, 1);
if (!list.length) {
delete this.$events[name];
}
} else if (list === fn || (list.listener && list.listener === fn)) {
delete this.$events[name];
}

@@ -116,2 +168,76 @@ }

/**
* Removes all listeners for an event.
*
* @api public
*/
EventEmitter.prototype.removeAllListeners = function (name) {
if (name === undefined) {
this.$events = {};
return this;
}
if (this.$events && this.$events[name]) {
this.$events[name] = null;
}
return this;
};
/**
* Gets all listeners for a certain event.
*
* @api publci
*/
EventEmitter.prototype.listeners = function (name) {
if (!this.$events) {
this.$events = {};
}
if (!this.$events[name]) {
this.$events[name] = [];
}
if (!isArray(this.$events[name])) {
this.$events[name] = [this.$events[name]];
}
return this.$events[name];
};
/**
* Emits an event.
*
* @api public
*/
EventEmitter.prototype.emit = function (name) {
if (!this.$events) {
return false;
}
var handler = this.$events[name];
if (!handler) {
return false;
}
var args = [].slice.call(arguments, 1);
if ('function' == typeof handler) {
handler.apply(this, args);
} else if (isArray(handler)) {
var listeners = handler.slice();
for (var i = 0, l = listeners.length; i < l; i++) {
listeners[i].apply(this, args);
}
} else {
return false;
}
return true;
};
}); // module: browser/events.js

@@ -123,2 +249,131 @@

require.register("browser/progress.js", function(module, exports, require){
/**
* Expose `Progress`.
*/
module.exports = Progress;
/**
* Initialize a new `Progress` indicator.
*/
function Progress() {
this.percent = 0;
this.size(0);
this.fontSize(12);
this.font('helvetica, arial, sans-serif');
}
/**
* Set progress size to `n`.
*
* @param {Number} n
* @return {Progress} for chaining
* @api public
*/
Progress.prototype.size = function(n){
this._size = n;
return this;
};
/**
* Set text to `str`.
*
* @param {String} str
* @return {Progress} for chaining
* @api public
*/
Progress.prototype.text = function(str){
this._text = str;
return this;
};
/**
* Set font size to `n`.
*
* @param {Number} n
* @return {Progress} for chaining
* @api public
*/
Progress.prototype.fontSize = function(n){
this._fontSize = n;
return this;
};
/**
* Set font `family`.
*
* @param {String} family
* @return {Progress} for chaining
*/
Progress.prototype.font = function(family){
this._font = family;
return this;
};
/**
* Update percentage to `n`.
*
* @param {Number} n
* @return {Progress} for chaining
*/
Progress.prototype.update = function(n){
this.percent = n;
return this;
};
/**
* Draw on `ctx`.
*
* @param {CanvasRenderingContext2d} ctx
* @return {Progress} for chaining
*/
Progress.prototype.draw = function(ctx){
var percent = Math.min(this.percent, 100)
, size = this._size
, half = size / 2
, x = half
, y = half
, rad = half - 1
, fontSize = this._fontSize;
ctx.font = fontSize + 'px ' + this._font;
var angle = Math.PI * 2 * (percent / 100);
ctx.clearRect(0, 0, size, size);
// outer circle
ctx.strokeStyle = '#9f9f9f';
ctx.beginPath();
ctx.arc(x, y, rad, 0, angle, false);
ctx.stroke();
// inner circle
ctx.strokeStyle = '#eee';
ctx.beginPath();
ctx.arc(x, y, rad - 1, 0, angle, true);
ctx.stroke();
// text
var text = this._text || (percent | 0) + '%'
, w = ctx.measureText(text).width;
ctx.fillText(
text
, x - w / 2 + 1
, y + fontSize / 2 - 1);
return this;
};
}); // module: browser/progress.js
require.register("browser/tty.js", function(module, exports, require){

@@ -441,3 +696,3 @@

exports.version = '0.3.6';
exports.version = '0.4.0';

@@ -491,3 +746,3 @@ exports.interfaces = require('./interfaces');

, 'pending': 36
, 'suite': '40'
, 'suite': 0
, 'error title': 0

@@ -833,3 +1088,4 @@ , 'error message': 31

var Base = require('./base')
, utils = require('../utils');
, utils = require('../utils')
, Progress = require('../browser/progress');

@@ -847,2 +1103,3 @@ /**

var statsTemplate = '<ul id="stats">'
+ '<li class="progress"><canvas width="50" height="50"></canvas></li>'
+ '<li class="passes">passes: <em>0</em></li>'

@@ -870,5 +1127,9 @@ + '<li class="failures">failures: <em>0</em></li>'

, stack = [root]
, stat = $(statsTemplate).appendTo(root);
, stat = $(statsTemplate).appendTo(root)
, canvas = stat.find('canvas').get(0)
, ctx = canvas.getContext('2d')
, progress = new Progress;
if (!root.length) return error('#mocha div missing, add it to your document');
progress.size(50);

@@ -893,2 +1154,8 @@ runner.on('suite', function(suite){

runner.on('test end', function(test){
// TODO: add to stats
var percent = stats.tests / total * 100 | 0;
// update progress bar
progress.update(percent).draw(ctx);
// update stats

@@ -1492,11 +1759,28 @@ var ms = new Date - stats.start;

runner.on('pending', function(test){
console.log('ok %d %s # SKIP -', n, title(test));
});
runner.on('pass', function(test){
console.log('ok %d %s', n, test.fullTitle());
console.log('ok %d %s', n, title(test));
});
runner.on('fail', function(test, err){
console.log('not ok %d %s', n, test.fullTitle());
console.log('not ok %d %s', n, title(test));
console.log(err.stack.replace(/^/gm, ' '));
});
}
/**
* Return a TAP-safe title of `test`
*
* @param {Object} test
* @return {String}
* @api private
*/
function title(test) {
return test.fullTitle().replace(/#/g, '');
}
}); // module: reporters/tap.js

@@ -1555,2 +1839,3 @@

this._timeout = ms;
if (this.timer) this.resetTimeout();
return this;

@@ -1582,2 +1867,20 @@ };

/**
* Reset the timeout.
*
* @api private
*/
Runnable.prototype.resetTimeout = function(){
var self = this
, ms = this.timeout();
this.clearTimeout();
if (ms) {
this.timer = setTimeout(function(){
self.callback(new Error('timeout of ' + ms + 'ms exceeded'));
}, ms);
}
};
/**
* Run the test and invoke `fn(err)`.

@@ -1598,5 +1901,7 @@ *

if (this.async) {
this.timer = setTimeout(function(){
done(new Error('timeout of ' + ms + 'ms exceeded'));
}, ms);
if (ms) {
this.timer = setTimeout(function(){
done(new Error('timeout of ' + ms + 'ms exceeded'));
}, ms);
}
}

@@ -1620,2 +1925,5 @@

// for .resetTimeout()
this.callback = done;
// async

@@ -2370,5 +2678,3 @@ if (this.async) {

process.nextTick = function(fn){
setTimeout(fn, 0);
};
process.nextTick = function(fn){ fn(); };

@@ -2424,1 +2730,2 @@ process.removeListener = function(ev){

})();
})();
{
"name": "mocha"
, "version": "0.3.6"
, "version": "0.4.0"
, "description": "Test framework inspired by JSpec, Expresso, & Qunit"

@@ -5,0 +5,0 @@ , "keywords": ["test", "bdd", "tdd", "tap"]

@@ -1,2 +0,1 @@

[![Build Status](https://secure.travis-ci.org/visionmedia/mocha.png)](http://travis-ci.org/visionmedia/mocha)

@@ -7,26 +6,1 @@

Mocha is a simple, flexible, fun JavaScript test framework for node.js and the browser. For more information view the [documentation](http://visionmedia.github.com/mocha).
## License
(The MIT License)
Copyright (c) 2011 TJ Holowaychuk &lt;tj@vision-media.ca&gt;
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
'Software'), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

@@ -83,3 +83,3 @@

});
fs.writeFile('mocha.js', buf, function(err){
fs.writeFile('_mocha.js', buf, function(err){
if (err) throw err;

@@ -86,0 +86,0 @@ console.log(' \033[90m create : \033[0m\033[36m%s\033[0m', 'mocha.js');

@@ -16,5 +16,3 @@

process.nextTick = function(fn){
setTimeout(fn, 0);
};
process.nextTick = function(fn){ fn(); };

@@ -21,0 +19,0 @@ process.removeListener = function(ev){

describe('GET', function(){
beforeEach(function(){
// console.log('before each');
describe('one', function(){
before(function(){
console.log('before');
})
describe('/foo', function(){
it('should foo', function(){
// console.log('foo');
describe('two', function(){
it('should three', function(){
})
})
})
describe('GET', function(){
describe('/bar', function(){
it('should bar', function(){
// console.log('bar');
describe('three', function(){
it('should four', function(){
})
})
})

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc