Skip to content

Front End Testing With Jest

dlangston edited this page Dec 17, 2014 · 5 revisions

Jest

Painless JavaScript Unit Testing built on top of the Jasmine test framework.

  • Familiar Approach: Built on top of the Jasmine test framework, using familiar expect(value).toBe(other) assertions

  • Mock by Default: Automatically mocks CommonJS modules returned by require(), making most existing code testable

  • Short Feedback Loop: DOM APIs are mocked and tests run in parallel via a small node.js command line utility

Instulation

Note: You will use npm to install Jest. If you don't have npm you can get it by installing node.js which includes npm.

To install Jest:

  1. Run $ npm install jest-cli --save-dev
  2. Add the following to the package.json file.
{
 ...
 "scripts": {
   "test": "jest"
 }
 ...
}

Setup

  1. Create a directory __tests__/.
  • This is where you will keep your individual tests.

Execution

  1. Run npm test
  • You should see a result similar to this:
    [PASS] __tests__/name-of-your-test.js (0.015s)

Resources