Skip to content

Instantly share code, notes, and snippets.

@isaacrankin
Last active August 29, 2015 14:18
Show Gist options
  • Save isaacrankin/64048fb0dbb81b75130a to your computer and use it in GitHub Desktop.
Save isaacrankin/64048fb0dbb81b75130a to your computer and use it in GitHub Desktop.
Remember How Mocha Tests

Command Line

Install Mocha JS globally npm install -g mocha

Then run mocha init test in the root of your project.

"test" is the directory where the testing files will be created, and it's where mocha will look to run tests from.

Then run tests with mocha.

Sorted.

Browser

The previous approach can work in the browser but requires some fixing.

Because test/tests.js loads an assertion library via require (which is only available in Node), you'll get an error. You need to load an assertion library into the page manually.

A better way to setup running tests in the browser is explained in this guide: https://gist.github.com/maicki/7781943

It allows you to run tests in the browser (for real) and inside of a headless browser with Phantom JS, with the help of Grunt JS. Tests are now part of your build process.

If you want to run tests with mocha directly, add the following to the top of test/tests.js (assuming that you're using Chai):

if(typeof require !== 'undefined'){
    var chai = require('chai');
}

var assert = chai.assert;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment