Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
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.0.1-alpha5 to 0.0.1-alpha6

.travis.yml

13

History.md
0.0.1-alpha6 / 2011-11-19
==================
* Added travis CI support (needs enabling when public)
* Added preliminary browser support
* Added `make mocha.css` target. Closes #45
* Added stack trace to TAP errors. Closes #52
* Renamed tearDown to teardown. Closes #49
* Fixed: cascading hooksc. Closes #30
* Fixed some colors for non-tty
* Fixed errors thrown in sync test-cases due to nextTick
* Fixed Base.window.width... again give precedence to 0.6.x
0.0.1-alpha5 / 2011-11-17

@@ -3,0 +16,0 @@ ==================

2

lib/interfaces/bdd.js

@@ -29,3 +29,3 @@

suite.on('pre-require', function(context, file){
suite.on('pre-require', function(context){

@@ -32,0 +32,0 @@ /**

@@ -29,3 +29,3 @@

suite.on('pre-require', function(context, file){
suite.on('pre-require', function(context){

@@ -44,3 +44,3 @@ /**

context.tearDown = function(fn){
context.teardown = function(fn){
suites[0].afterEach(fn);

@@ -47,0 +47,0 @@ };

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

exports.version = '0.0.1-alpha5';
exports.version = '0.0.1-alpha6';

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

@@ -33,2 +33,5 @@

, 'fail': 31
, 'bright pass': 92
, 'bright fail': 91
, 'bright yellow': 93
, 'pending': 36

@@ -43,2 +46,4 @@ , 'suite': '40'

, 'slow': 31
, 'green': 32
, 'light': 90
};

@@ -70,5 +75,5 @@

width: isatty
? tty.getWindowSize
? tty.getWindowSize()[1]
: process.stdout.getWindowSize(1)[0]
? process.stdout.getWindowSize
? process.stdout.getWindowSize(1)[0]
: tty.getWindowSize()[1]
: 75

@@ -144,2 +149,4 @@ };

if (!runner) return;
runner.on('start', function(){

@@ -193,3 +200,4 @@ stats.start = new Date;

Base.prototype.epilogue = function(){
var stats = this.stats;
var stats = this.stats
, fmt;

@@ -200,5 +208,7 @@ console.log();

if (stats.failures) {
console.error(
' \033[91m✖\033[31m %d of %d tests failed\033[90m:\033[0m'
, stats.failures, stats.tests);
fmt = color('bright fail', ' ✖')
+ color('fail', ' %d of %d tests failed')
+ color('light', ':')
console.error(fmt, stats.failures, stats.tests);
Base.list(this.failures);

@@ -213,7 +223,7 @@ console.error();

// pass
console.log(
' \033[92m✔\033[32m %d tests completed\033[90m (%dms)\033[0m'
, stats.tests || 0
, stats.duration);
fmt = color('bright pass', ' ✔')
+ color('green', ' %d tests complete')
+ color('light', ' (%dms)');
console.log(fmt, stats.tests || 0, stats.duration);
console.log();

@@ -220,0 +230,0 @@ process.nextTick(function(){

@@ -51,5 +51,5 @@

runner.on('pass', function(test){
console.log('%s<dt>%s</dt>', indent(), test.title);
console.log('%s <dt>%s</dt>', indent(), test.title);
var code = clean(test.fn.toString());
console.log('%s<dd><pre><code>%s</code></pre></dd>', indent(), code);
console.log('%s <dd><pre><code>%s</code></pre></dd>', indent(), code);
});

@@ -56,0 +56,0 @@

@@ -41,3 +41,3 @@

if ('slow' == test.speed) {
process.stdout.write('\033[93m.\033[0m');
process.stdout.write(color('bright yellow', '.'));
} else {

@@ -44,0 +44,0 @@ process.stdout.write(color(test.speed, '.'));

@@ -7,2 +7,3 @@

exports.JSON = require('./json');
exports.HTML = require('./html');
exports.List = require('./list');

@@ -9,0 +10,0 @@ exports.Progress = require('./progress');

@@ -32,3 +32,3 @@

runner.on('start', function(){
console.log(' %d..%d', 1, total);
console.log('%d..%d', 1, total);
});

@@ -41,7 +41,8 @@

runner.on('pass', function(test){
console.log(' ok %d %s', n, test.fullTitle());
console.log('ok %d %s', n, test.fullTitle());
});
runner.on('fail', function(test){
console.log(' not ok %d %s', n, test.fullTitle());
runner.on('fail', function(test, err){
console.log('not ok %d %s', n, test.fullTitle());
console.log(err.stack.replace(/^/gm, ' '));
});

@@ -48,0 +49,0 @@

@@ -34,6 +34,7 @@

function Runner(suite) {
var self = this;
this.suite = suite;
this.total = suite.total();
this.globals = Object.keys(global).concat(['errno']);
this.on('test end', this.checkGlobals.bind(this));
this.on('test end', function(){ self.checkGlobals(); });
this.grep(/.*/);

@@ -159,2 +160,78 @@ }

/**
* Run hook `name` for the given array of `suites`
* in order, and callback `fn(err)`.
*
* @param {String} name
* @param {Array} suites
* @param {Function} fn
* @api private
*/
Runner.prototype.hooks = function(name, suites, fn){
var self = this
, orig = this.suite;
function next(suite) {
self.suite = suite;
if (!suite) {
self.suite = orig;
return fn();
}
self.hook(name, function(err){
if (err) {
self.suite = orig;
return fn(err);
}
next(suites.pop());
});
}
next(suites.pop());
};
/**
* Run hooks from the top level down.
*
* @param {String} name
* @param {Function} fn
* @api private
*/
Runner.prototype.hookUp = function(name, fn){
var suites = [this.suite].concat(this.parents()).reverse();
this.hooks(name, suites, fn);
};
/**
* Run hooks from the bottom up.
*
* @param {String} name
* @param {Function} fn
* @api private
*/
Runner.prototype.hookDown = function(name, fn){
var suites = [this.suite].concat(this.parents());
this.hooks(name, suites, fn);
};
/**
* Return an array of parent Suites from
* closest to furthest.
*
* @return {Array}
* @api private
*/
Runner.prototype.parents = function(){
var suite = this.suite
, suites = [];
while (suite = suite.parent) suites.push(suite);
return suites;
};
/**
* Run the current test and callback `fn(err)`.

@@ -170,20 +247,26 @@ *

// run the test
try {
// async
if (test.async) return test.run(function(err){
if (test.finished) {
self.fail(test, new Error('done() called multiple times'));
return;
}
// async
if (test.async) {
try {
return test.run(function(err){
if (test.finished) {
self.fail(test, new Error('done() called multiple times'));
return;
}
fn(err);
});
} catch (err) {
fn(err);
});
// sync
process.nextTick(function(){
}
}
// sync
process.nextTick(function(){
try {
test.run();
fn();
});
} catch (err) {
fn(err);
}
} catch (err) {
fn(err);
}
});
};

@@ -207,3 +290,6 @@

// error handling
if (err) self.fail(test, err);
if (err) {
self.fail(test, err);
self.emit('test end', test);
}

@@ -228,3 +314,3 @@ // next test

self.emit('test', self.test = test);
self.hook('beforeEach', function(err){
self.hookDown('beforeEach', function(err){
if (err) return self.failHook('beforeEach', err);

@@ -237,3 +323,3 @@ self.runTest(function(err){

if (err) return self.failHook('beforeEach', err);
self.hook('afterEach', function(err){
self.hookUp('afterEach', function(err){
if (err) return self.failHook('afterEach', err);

@@ -240,0 +326,0 @@ next();

{
"name": "mocha"
, "version": "0.0.1-alpha5"
, "version": "0.0.1-alpha6"
, "description": "Test framework inspired by JSpec, Expresso, & Qunit"

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

, "engines": { "node": ">= 0.4.x < 0.7.0" }
, "scripts": {
"test": "make test"
}
, "dependencies":{

@@ -16,0 +19,0 @@ "commander": "0.3.2"

# mocha
[![Mocha test framework](http://f.cl.ly/items/3H1W3W3i3W163X0U3127/Screenshot.png)](http://visionmedia.github.com/mocha)
Mocha is a _simple_, _fun_, _extensible_ JavaScript test framework rich with features, running on [node](http://nodejs.org) and the browser. Mocha tests run serially, allowing reporting flexibility, and mapping uncaught exceptions to the correct test cases; This also makes Mocha an ideal choice when mocking and stubbing is involved.
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).
Mocha allows you to use any assertion library you want, if it throws an error, it will work. This means you can utilize libraries such as [should](http://github.com/visionmedia/should.js), node's regular `assert` module, or others.
Testing asynchronous code with Mocha could not be simpler, when your tests are sync simple omit the callback, when they are async add a callback and invoke it when your test is complete.
## Features
- proper exit status for CI support etc
- ideal for asynchronous APIs
- auto-detects and disables coloring for non-ttys
- maps uncaught exceptions to the correct test case
- async test timeout support
- growl notification support
- reports test durations
- highlights slow tests
- global variable leak detection
- configurable test-case timeout
- optionally run tests that match a regexp
- auto-exit to prevent "hanging" due to an active event loop
- easily meta-generate suites & test-cases
- mocha.opts file support
- `mocha-debug(1)` for node debugger support
- detects multiple calls to `done()`
- TextMate bundle
- use any assertion library you want ([should](http://github.com/visionmedia/should.js), `assert`, etc)
- extensible reporting
- `dot`: a dot matrix
- `doc`: a documentation generator (html) based on your tests
- `landing`: a unicode landing strip ✈
- `tap`: test-anything-protocol (TAP) producer
- `progress`: a progress bar
- `spec`: hierarchical specification
- `list`: similar to `spec` as a flat-list
- `json-stream`: streaming JSON delimited by a LF
- `json`: a single JSON chunk on exit
- extensible test DSLs
- BDD
- TDD
- exports
## Usage
```
Usage: mocha [options] [files]
Options:
-h, --help output usage information
-V, --version output the version number
-r, --require <name> require the given module
-R, --reporter <name> specify the reporter to use
-u, --ui <name> specify user-interface (bdd|tdd|exports)
-g, --grep <pattern> only run tests matching <pattern>
-t, --timeout <ms> set test-case timeout in milliseconds [2000]
-s, --slow <ms> "slow" test threshold in milliseconds [75]
-G, --growl enable growl support
Reporters:
dot - dot matrix
doc - html documentation
json - single json object
progress - progress bar
list - spec-style listing
tap - test-anything-protocol
landing - unicode landing strip
json-stream - newline delimited json events
Interfaces:
bdd - describe() / it()
tdd - suite() / test()
exports - module.exports
```
## Interfaces
Mocha "interfaces" providing BDD, TDD, and expresso export-style flavoured APIs on top of the internals.
### BDD
```js
describe('Array', function(){
before(function(){
// ...
});
describe('#indexOf()', function(){
it('should return -1 when not present', function(){
[1,2,3].indexOf(4).should.equal(-1);
});
it('should return the index when present', function(){
[1,2,3].indexOf(3).should.equal(2);
[1,2,3].indexOf(2).should.equal(1);
[1,2,3].indexOf(1).should.equal(0);
});
});
});
```
### TDD
```js
suite('Array', function(){
setup(function(){
// ...
});
suite('#indexOf()', function(){
test('should return -1 when not present', function(){
assert.equal(-1, [1,2,3].indexOf(4));
});
test('should return the index when present', function(){
assert.equal(2, [1,2,3].indexOf(3));
assert.equal(1, [1,2,3].indexOf(2));
assert.equal(0, [1,2,3].indexOf(1));
});
});
});
```
### Exports
```js
module.exports = {
'Array': {
'#indexOf()': {
'should return -1 when not present': function(){
[1,2,3].indexOf(4).should.equal(-1);
},
'should return the index when present': function(){
[1,2,3].indexOf(3).should.equal(2);
[1,2,3].indexOf(2).should.equal(1);
[1,2,3].indexOf(1).should.equal(0);
}
}
}
};
```
## Reporters
Mocha reporters adjust to the terminal window,
and always disable ansi-escape colouring when
the stdio streams are not associated with a tty.
### Dot Matrix
The Dot Matrix reporter is simply a series of dots
that represent test cases, failures highlight in red.
![dot matrix reporter](http://f.cl.ly/items/3b3b471Z1p2U3D1P2Y1n/Screenshot.png)
![dot matrix failure](http://f.cl.ly/items/1P11330L033r423g1y1n/Screenshot.png)
## TAP
The TAP reporter emits lines for a [Test-Anything-Protocol](http://en.wikipedia.org/wiki/Test_Anything_Protocol) consumer.
![test anything protocol](http://f.cl.ly/items/2O0X3h0d1Q430O1t1T3p/Screenshot.png)
## Landing Strip
The Landing Strip reporter is a gimmicky test reporter simulating
a plane landing :) unicode ftw
![landing strip plane reporter](http://f.cl.ly/items/0z1k400K1N1Y2G3u2u0i/Screenshot.png)
## List
The "List" reporter outputs a simple specifications list as
test cases pass or fail, outputting the failure details at
the bottom of the output.
![list reporter](http://f.cl.ly/items/0Y0x1B3l3K0n3t3h3l0p/Screenshot.png)
![failures](http://f.cl.ly/items/2Z0E150v20042G2d1J0i/Screenshot.png)
## JSON
The JSON reporter outputs a single large JSON object when
the tests have completed (failures or not).
## JSON Stream
The JSON Stream reporter outputs newline-delimited JSON "events" as they occur, beginning with a "start" event, followed by test passes or failures, and then the final "end" event.
```json
["start",{"total":12}]
["pass",{"title":"should return -1 when not present","fullTitle":"Array #indexOf() should return -1 when not present","duration":0}]
["pass",{"title":"should return the index when present","fullTitle":"Array #indexOf() should return the index when present","duration":0}]
["fail",{"title":"should return -1 when not present","fullTitle":"Array #indexOf() should return -1 when not present"}]
["end",{"start":"2011-08-29T03:21:02.050Z","suites":13,"passes":11,"tests":12,"failures":1,"end":"2011-08-29T03:21:02.052Z","duration":2}]
````
## Doc
The "doc" reporter outputs a hierarchical HTML body representation
of your tests, wrap it with a header, footer, some styling and you
have some fantastic documentation!
For example suppose you have the following JavaScript:
```js
describe('Array', function(){
describe('#indexOf()', function(){
it('should return -1 when the value is not present', function(){
[1,2,3].indexOf(5).should.equal(-1);
[1,2,3].indexOf(0).should.equal(-1);
})
})
})
```
The command `mocha --reporter doc array` would yield:
```html
<section class="suite">
<h1>Array</h1>
<dl>
<section class="suite">
<h1>#indexOf()</h1>
<dl>
<dt>should return -1 when the value is not present</dt>
<dd><pre><code>[1,2,3].indexOf(5).should.equal(-1);
[1,2,3].indexOf(0).should.equal(-1);</code></pre></dd>
</dl>
</section>
</dl>
</section>
```
## Async tests
Testing async code with mocha is simple, invoke the `done()` callback
when complete, if called multiple times (due to a race-condition etc)
will cause mocha to fail, this is invaluable for testing async code.
```js
describe('something async', function(){
it('should finish after 300ms', function(done){
setTimeout(done, 300);
})
})
```
The `done()` callback also accepts an error, so it's easy to write
tests that adhere to node's callback convention of `(err, result)`:
```js
describe('User.save()', function(){
it('should save without failing', function(done){
var user = new User('tj');
user.save(done);
})
})
```
## Best practices
### test/*
By default `mocha(1)` will use the pattern `./test/*.js`, so
it's usually a good place to put your tests.
### mocha.opts
Mocha will attempt to load `./test/mocha.opts`, these are concatenated with `process.argv`, though command-line args will take precedence. For example suppose you have the following _mocha.opts_ file:
```
--require should
--reporter dot
--ui bdd
```
This will default the reporter to `dot`, require the `should` library,
and use `bdd` as the interface. With this you may then invoke `mocha(1)`
with additional arguments, here enabling growl support and changing
the reporter to `spec`:
```
$ mocha --reporter list --growl
```
### Suite merging
Suites with common names are "merged" in order
to produce unified reporting, especially when
meta-generating tests.
```js
describe('merge', function(){
describe('stuff', function(){
describe('one', function(){
it('should do something', function(){
})
})
})
})
describe('merge', function(){
describe('stuff', function(){
describe('two', function(){
it('should do something', function(){
})
})
})
})
describe('merge stuff', function(){
describe('three', function(){
it('should do something', function(){
})
})
})
```
will produce the following:
![mocha suite merging](http://f.cl.ly/items/380R3S1t1t0b0O2K250V/Screenshot.png)
### Makefiles
Be kind and don't make developers hunt around in your docs to figure
out how to run the tests, add a `make test` target to your _Makefile_:
```
test:
./node_modules/.bin/mocha \
--reporter list
.PHONY: test
```
## TextMate Bundle
The Mocha TextMate bundle includes snippets to
make writing tests quicker and more enjoyable.
To install the bundle run:
$ make tm
## Running tests
Run mocha tests:
$ make test
Run all tests, including interfaces:
$ make test-all
Alter the reporter:
$ make test REPORTER=list
## License

@@ -371,0 +7,0 @@

@@ -14,11 +14,17 @@

, 'before all'
, 'parent before'
, 'before'
, 'one'
, 'after'
, 'parent after'
, 'parent before'
, 'before'
, 'two'
, 'after'
, 'parent after'
, 'parent before'
, 'before'
, 'three'
, 'after'
, 'parent after'
, 'after all'

@@ -29,3 +35,2 @@ , 'root after all']);

beforeEach(function(){
// should not be invoked
calls.push('parent before');

@@ -35,3 +40,2 @@ })

afterEach(function(){
// should not be invoked
calls.push('parent after' );

@@ -60,2 +64,3 @@ })

, 'before all'
, 'parent before'
, 'before']);

@@ -70,5 +75,8 @@ calls.push('one');

, 'before all'
, 'parent before'
, 'before'
, 'one'
, 'after'
, 'parent after'
, 'parent before'
, 'before']);

@@ -82,8 +90,13 @@ calls.push('two');

, 'before all'
, 'parent before'
, 'before'
, 'one'
, 'after'
, 'parent after'
, 'parent before'
, 'before'
, 'two'
, 'after'
, 'parent after'
, 'parent before'
, 'before']);

@@ -90,0 +103,0 @@ calls.push('three');

@@ -6,3 +6,2 @@

beforeEach(function(){
// not hit
calls.push('parent before');

@@ -12,3 +11,2 @@ })

afterEach(function(){
// not hit
calls.push('parent after');

@@ -23,3 +21,3 @@ })

it('one', function(){
calls.should.eql(['before']);
calls.should.eql(['parent before', 'before']);
calls.push('one');

@@ -30,5 +28,8 @@ })

calls.should.eql([
'before'
'parent before'
, 'before'
, 'one'
, 'after'
, 'parent after'
, 'parent before'
, 'before']);

@@ -40,8 +41,13 @@ calls.push('two');

calls.should.eql([
'before'
'parent before'
, 'before'
, 'one'
, 'after'
, 'parent after'
, 'parent before'
, 'before'
, 'two'
, 'after'
, 'parent after'
, 'parent before'
, 'before']);

@@ -57,13 +63,19 @@ calls.push('three');

calls.should.eql([
'before'
'parent before'
, 'before'
, 'one'
, 'after'
, 'parent after'
, 'parent before'
, 'before'
, 'two'
, 'after'
, 'parent after'
, 'parent before'
, 'before'
, 'three'
, 'after']);
, 'after'
, 'parent after']);
})
})
})

@@ -39,2 +39,3 @@

, 'parent after'
, 'parent before'
, 'before']);

@@ -51,5 +52,8 @@ calls.push('one');

, 'parent after'
, 'parent before'
, 'before'
, 'one'
, 'after'
, 'parent after'
, 'parent before'
, 'before']);

@@ -56,0 +60,0 @@ calls.push('two');

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