jwerle/chirp — pull request #14 by traviskaufman (2013-09-23 00:54:18+00:00)
Title: [#7] Setup test environment for front-end / back-end code

## Overview
Tests use mocha (with BDD setup), chai (specifically `chai.assert`), and sinon.

For back-end tests, it's just a matter of writing the test and putting it in the right place. For front-end tests, require the file via the component's path. Also rather than introducing yet another package manager like bower I just downloaded sinon and chai as pre-built js files and dropped them in `test/support/front-end` shown below.

## How to run tests
```sh
$ make backendtest # run back-end tests in node using mocha
$ make frontendtest # run front-end tests in Chrome, FF, Safari, and PhantomJS using Karma. Opera wasn't working that well.
$ make test # run all tests. Can also use npm test to do this.
```

## Test Structure
```
karma.front-end-tests.conf.js # karma config for front-end tests
test/
-- front-end # front-end *.spec.js files go here
-- back-end # back-end *.spec.js files go here
-- support # test helpers, test scripts, etc.
-- front-end # helpers/scripts for front-end tests
-- test-bootstrap.js # exposes chai.assert globally, adds bind shim for PhantomJS other runtimes missing it
-- chai.js # Vendor-built chai.js
-- sinon-1.7.3.js # Vendor-built sinon.js
-- back-end # helpers/scripts for back-end tests
-- test-bootstrap.js # exposes sinon and chai globally so we don't have to require them in our tests
```

## Front-end Test
test/front-end/example.spec.js
```javascript
var example = require('chirp/public/js/chirp/example');

describe('example', function() {
describe('#log', function() {
var msg;
var log_stub;

beforeEach(function() {
msg = 'yo';
log_stub = sinon.stub(console, 'log');
});

afterEach(function() {
console.log.restore();
});

it('logs a message to the console', function() {
example.log(msg);
assert.isTrue(log_stub.calledWithExactly(msg));
});

it('logs "hello werld!" if no message is given', function() {
example.log();
assert.isTrue(log_stub.calledWithExactly('hello werld!'));
});

});
});
```

Back-end tests are pretty straightforward so I left an example out.

public/js/chirp/example.js
```javascript
exports.log = function(msg) {
console.log(msg || 'hello werld!');
};
```