Skip to content

Instantly share code, notes, and snippets.

@kerimdzhanov
Last active August 3, 2018 08:21
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kerimdzhanov/2b9f5ef8a6eff7149b4a1652790042e7 to your computer and use it in GitHub Desktop.
Save kerimdzhanov/2b9f5ef8a6eff7149b4a1652790042e7 to your computer and use it in GitHub Desktop.
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