Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Setup mocha/chai.js/sinon stack w/ es6 features

Add the following development dependencies to your package.json (from the command line):

Using npm:

$ npm install cross-env mocha chai sinon sinon-chai chai-as-promised chai-datetime --save-dev

Using Yarn:

$ yarn add cross-env mocha chai sinon sinon-chai chai-as-promised chai-datetime --dev

Copy the mocha.conf.js file from this gist to your project's root directory.

Create the test/mocha.opts file specifying the following mocha options:

--require chai/register-expect
--require mocha.conf.js

If you're using babel in your project, you can also add the following option:

--require babel-core/register

Finally, add the test task to your package.json's script section:

{
  ...
  "scripts": {
    ...
    "test": "cross-env NODE_ENV=test mocha test/*.spec.js test/**/*.spec.js",
    ...
  }
  ...
}

Done! You are ready to write your first test.

const chai = require('chai');
// globalize sinon
global.sinon = require('sinon');
// initialize chai plugins
chai.use(require('sinon-chai'));
chai.use(require('chai-as-promised'));
chai.use(require('chai-datetime'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.