Skip to content

Instantly share code, notes, and snippets.

@sgnl
Last active December 28, 2015 21:46
Show Gist options
  • Save sgnl/9592b11629ae5ab9862c to your computer and use it in GitHub Desktop.
Save sgnl/9592b11629ae5ab9862c to your computer and use it in GitHub Desktop.
Adding Mocha/Chai to your project (morning exercise edition)

Read the docs?

Mocha Chai

In your project directory

  • Initialized git?
  • Initialized npm?

Install Packages

  • Install your packages with the command npm install -D mocha chai
    • Flags:
      • use -D to save the package(s) in the package.json file under "devDependencies"
      • use -S to save the package(s) in the package.json file under "dependencies"

Packages for running, creating, and managing your tests are generally considered development dependencies. Use the -D flag.

Create Structure

  • Mocha, by default, looks for test files in a directory called test.
    • create your test files in this directory. here is a suggestion for file name:
      • *.spec.js * refers to the filename you're testing. If you're testing a module saved as calculator.js your test file should be named calculator.spec.js

Inside your test file

  • Did you require mocha and/or chai ?
  • Initialize chai's expect or should ?
  • Are you requiring the file you're testing? ex. calculator.spec.js tests the calculator.js file. calculator.js needs to be imported into the calculator.spec.js file using node's require function.
  • desc block? reference at mochajs.org
  • it block? refernece at mochajs.org
  • assertions? reference at chaijs.com

Run your tests

  • command: mocha
  • keep watching: mocha --watch
  • bail on first failure: mocha --bail
  • all-in-one: mocha --watch --bail

aw yiss.

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