GithubHelp home page GithubHelp logo

sebmarkbage / jest Goto Github PK

View Code? Open in Web Editor NEW

This project forked from jestjs/jest

1.0 3.0 0.0 10.79 MB

๐Ÿƒ Painless JavaScript Testing.

Home Page: http://facebook.github.io/jest/

License: Other

JavaScript 96.11% CSS 3.81% TypeScript 0.03% Shell 0.04%

jest's Introduction

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

<generated_getting_started_start /> Install Jest with npm by running 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 npm 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:

Babel Integration

If you'd like to use Babel, it can easily be enabled: npm install --save-dev 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 (npm install --save-dev 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. <generated_getting_started_end />

API & Configuration

jest's People

Contributors

aaronabramov avatar amasad avatar blainekasten avatar clentfort avatar cpojer avatar daniel15 avatar dmitrysoshnikov avatar edvinerikson avatar emmenko avatar forbeslindesay avatar gaearon avatar hankhsiao avatar harshadsabne avatar ide avatar jbonta avatar jeffmo avatar jquense avatar kentaromiura avatar keyz avatar kl3ryk avatar leebyron avatar maximderbin avatar philvargas avatar sampepose avatar sophiebits avatar stephnr avatar vjeux avatar wincent avatar yungsters avatar zpao avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.