Skip to content

Instantly share code, notes, and snippets.

@dianedouglas
Last active October 31, 2021 21:20
Show Gist options
  • Save dianedouglas/f45a540e0d1c223ced95 to your computer and use it in GitHub Desktop.
Save dianedouglas/f45a540e0d1c223ced95 to your computer and use it in GitHub Desktop.
Testing with Mocha and Chai from the command line

Testing from the commandline

First install the testing framework:

npm install mocha --save-dev
npm install chai --save-dev
npm install mocha -g

Then at the top of the spec file:

var expect = require('chai').expect;

//pull in whatever module/functions we are testing.
var pingpong = require('./../js/pingpong.js').pingpong;

Now we can run tests with:

mocha [name of spec folder]

For example, let's assume our test files are in a folder called spec. Then the command would be:

mocha spec

And we should throw the command into our package.json file. Find the line that starts with "test" :

  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },

And replace it with the testing command:

  "scripts": {
    "test": "mocha [name of spec folder]"
  },

So with a test folder called spec this would be:

  "scripts": {
    "test": "mocha spec"
  },

Then from the command line we can now run:

npm test
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment