🃏 Painless JavaScript Testing.
JavaScript CSS Other
Permalink
Failed to load latest commit information.
.github Fix issue template. Dec 15, 2016
.vscode Adds babylon support for parsing test files (#2327) Dec 19, 2016
blog Fix broken link to Babel's plugin inline-requests in blog post 'JavaS… Jan 17, 2017
danger [Danger] Ensure node4 builds fine (#2541) Jan 9, 2017
docs add detail regarding manual mocks and node modules (#2651) Jan 20, 2017
examples Update RN example (#2543) Jan 9, 2017
flow-typed mv flowlibs flow-typed (#1283) Jul 13, 2016
integration_tests Filter skipped tests for verbose reporter (#2593) Jan 18, 2017
packages Fix findMatchingTests for directories (#2671) Jan 23, 2017
scripts Use eslint-plugin-jest inside of Jest. (#2509) Jan 5, 2017
types Adding jest-phabricator-coverage (#2316) Jan 19, 2017
website New website colors. (#2623) Jan 18, 2017
.babelrc Adds support for using `async` inside the babel runtime (#2199) Dec 1, 2016
.eslintignore Add experimental react-native support. (#1317) Jul 27, 2016
.eslintrc New eslint config. (#1444) Aug 17, 2016
.flowconfig Add Danger by using a subfolder (#2532) Jan 8, 2017
.gitignore Add Danger by using a subfolder (#2532) Jan 8, 2017
.npmignore Add top-level .npmignore. Jul 12, 2016
.travis.yml Add Danger by using a subfolder (#2532) Jan 8, 2017
CHANGELOG.md Update changelog. Dec 29, 2016
CONTRIBUTING.md Docs: recommend npm version 3 or later in CONTRIBUTING.md (#2612) Jan 16, 2017
LICENSE update license year to range Jan 19, 2016
PATENTS Update Patent Grant Apr 10, 2015
README.md Jest 18 + 2016 in Jest. (#2333) Dec 15, 2016
appveyor.yml Yarn is now preinstalled on AppVeyor (#2041) Nov 2, 2016
circle.yml Deploy Jest website via CircleCI (#2210) Dec 5, 2016
dangerfile.js exclude __tests__ and __mocks__ from flow (#2642) Jan 19, 2017
jest Fix stack trace printing in node 4. Sep 1, 2016
jsconfig.json Add Jest Editor Support (#2192) Dec 7, 2016
lerna.json Update lerna (#2502) Jan 4, 2017
package.json Remove unnecessary test commands from packages. They don’t work anymo… Jan 15, 2017
testSetupFile.js Add AppVeyor Jasmine reporter Sep 3, 2016
yarn.lock Validate config with `jest-validate` package (#2524) Jan 16, 2017

README.md

Jest Build Status Windows Build Status npm version

Painless JavaScript Testing

  • Adaptable: Jest uses Jasmine assertions by default and Jest is modular, extendible and configurable.

  • Sandboxed and Fast: Jest virtualizes JavaScript environments, provides browser mocks and runs tests in parallel across workers.

  • Snapshot Testing: Jest can capture snapshots of React trees or other serializable values to write tests quickly and it provides a seamless update experience.

Getting Started

Before you install Jest, you can try out a real version of Jest through repl.it. Just edit your test and hit the run button!

Install Jest with yarn or npm by running yarn add -D jest or npm install --save-dev jest. Let's get started by writing a test for a hypothetical sum.js file:

module.exports = (a, b) => a + b;

Create a directory __tests__/ with a file sum-test.js or name it sum.test.js or sum.spec.js and put it anywhere in your project:

test('adds 1 + 2 to equal 3', () => {
  const sum = require('../sum');
  expect(sum(1, 2)).toBe(3);
});

Add the following to your package.json:

"scripts": {
  "test": "jest"
}

Run yarn test and Jest will print this message: PASS __tests__/sum-test.js. You just successfully wrote your first test using Jest!

You are ready to use Jest! Here are some more resources to help you get started:

...or watch a video to get started with Jest:

Video by Kent C. Dodds hosted by Egghead.

Babel Integration

If you'd like to use Babel, it can easily be enabled: yarn add -D babel-jest babel-polyfill.

Don't forget to add a .babelrc file in your project's root folder. For example, if you are using ES2015 and React.js with the babel-preset-es2015 and babel-preset-react presets:

{
  "presets": ["es2015", "react"]
}

You are now set up to use all ES2015 features and React specific syntax.

Note: If you are using a more complicated Babel configuration, using Babel's env option, keep in mind that Jest will automatically define NODE_ENV as test. It will not use development section like Babel does by default when no NODE_ENV is set.

React, React Native and Snapshot Testing

Check out the React tutorial and the React Native tutorial to get started with React or React Native codebases. You can use React's test renderer (yarn add -D react-test-renderer) to capture snapshots with Jest's snapshot feature and the toMatchSnapshot matcher:

import renderer from 'react-test-renderer';
test('Link renders correctly', () => {
  const tree = renderer.create(
    <Link page="http://www.facebook.com">Facebook</Link>
  ).toJSON();
  expect(tree).toMatchSnapshot();
});

and it will produce a snapshot like this:

exports[`Link renders correctly 1`] = `
<a
  className="normal"
  href="http://www.facebook.com"
  onMouseEnter={[Function]}
  onMouseLeave={[Function]}>
  Facebook
</a>
`;

On subsequent test runs, Jest will compare the stored snapshot with the rendered output and highlight differences. If there are differences, Jest will ask you to fix your mistake and can be re-run with -u or --updateSnapshot to update an outdated snapshot.

API & Configuration