Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 27 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,29 +1,35 @@
# Jest

[![CircleCI Build Status](https://circleci.com/gh/facebook/jest.svg?style=shield)](https://circleci.com/gh/facebook/jest)
[![Travis Build Status](https://travis-ci.org/facebook/jest.svg?branch=master)](https://travis-ci.org/facebook/jest) [![Windows Build Status](https://ci.appveyor.com/api/projects/status/8n38o44k585hhvhd/branch/master?svg=true)](https://ci.appveyor.com/project/Daniel15/jest/branch/master) [![npm version](https://badge.fury.io/js/jest.svg)](http://badge.fury.io/js/jest)
[![Travis Build Status](https://travis-ci.org/facebook/jest.svg?branch=master)](https://travis-ci.org/facebook/jest)
[![Windows Build Status](https://ci.appveyor.com/api/projects/status/8n38o44k585hhvhd/branch/master?svg=true)](https://ci.appveyor.com/project/Daniel15/jest/branch/master)
[![npm version](https://badge.fury.io/js/jest.svg)](http://badge.fury.io/js/jest)

🃏 Delightful JavaScript Testing

- **👩🏻‍💻 Easy Setup**: Complete and easy to set-up JavaScript testing solution. Works out of the box for any React project.
* **👩🏻‍💻 Easy Setup**: Complete and easy to set-up JavaScript testing solution.
Works out of the box for any React project.

- **🏃🏽 Instant Feedback**: Fast interactive watch mode runs only test files related to changed files and is optimized to give signal quickly.
* **🏃🏽 Instant Feedback**: Fast interactive watch mode runs only test files
related to changed files and is optimized to give signal quickly.

- **📸 Snapshot Testing**: Capture snapshots of React trees or other serializable values to simplify testing and to analyze how state changes over time.
* **📸 Snapshot Testing**: Capture snapshots of React trees or other
serializable values to simplify testing and to analyze how state changes over
time.

## Getting Started

<!-- generated_getting_started_start -->

Install Jest using [`npm`](https://www.npmjs.com/):

```
```bash
npm install --save-dev jest
```

Or via [`yarn`](https://yarnpkg.com/en/package/jest):

```
```bash
yarn add --dev jest
```

Expand Down Expand Up @@ -59,7 +65,7 @@ Add the following section to your `package.json`:

Finally, run `npm test` and Jest will print this message:

```
```bash
PASS ./sum.test.js
✓ adds 1 + 2 to equal 3 (5ms)
```
Expand All @@ -83,7 +89,8 @@ jest my-test --notify --config=config.json
```

If you'd like to learn more about running `jest` through the command line, take
a look at the [Jest CLI Options](https://facebook.github.io/jest/docs/cli.html) page.
a look at the [Jest CLI Options](https://facebook.github.io/jest/docs/cli.html)
page.

## Additional Configuration

Expand All @@ -92,14 +99,14 @@ a look at the [Jest CLI Options](https://facebook.github.io/jest/docs/cli.html)
To use [Babel](http://babeljs.io/), install the `babel-jest` and
`regenerator-runtime` packages:

```
```bash
npm install --save-dev babel-jest babel-core regenerator-runtime
```

> Note: If you are using a babel version 7 then you need to install `babel-jest`
> with the following command:
>
> ```
> ```bash
> npm install --save-dev babel-jest 'babel-core@^7.0.0-0' @babel/core regenerator-runtime
> ```

Expand Down Expand Up @@ -165,11 +172,13 @@ started.

To use TypeScript in your tests you can use
[ts-jest](https://github.com/kulshekhar/ts-jest).

<!-- generated_getting_started_end -->

## Documentation

Learn more about using Jest at http://facebook.github.io/jest
Learn more about using
[Jest on the official site!](http://facebook.github.io/jest)

* [Getting Started](http://facebook.github.io/jest/docs/en/getting-started.html)
* [Guides](http://facebook.github.io/jest/docs/en/snapshot-testing.html)
Expand All @@ -178,14 +187,18 @@ Learn more about using Jest at http://facebook.github.io/jest

## Badge

Show the world you're using *Jest* → [![tested with jest](https://img.shields.io/badge/tested_with-jest-99424f.svg)](https://github.com/facebook/jest)
Show the world you're using _Jest_ →
[![tested with jest](https://img.shields.io/badge/tested_with-jest-99424f.svg)](https://github.com/facebook/jest)

```md
[![tested with jest](https://img.shields.io/badge/tested_with-jest-99424f.svg)](https://github.com/facebook/jest)
```

## Contributing

Send issues and pull requests with your ideas. For more information about contributing PRs and issues, see our [Contribution Guidelines](https://github.com/facebook/jest/blob/master/CONTRIBUTING.md).
Send issues and pull requests with your ideas. For more information about
contributing PRs and issues, see our
[Contribution Guidelines](https://github.com/facebook/jest/blob/master/CONTRIBUTING.md).

[Good First Issue](https://github.com/facebook/jest/labels/Good%20First%20Issue) is a great starting point for PRs.
[Good First Issue](https://github.com/facebook/jest/labels/Good%20First%20Issue)
is a great starting point for PRs.
12 changes: 6 additions & 6 deletions docs/Configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -591,7 +591,7 @@ module that exports a function expecting a string as the first argument for the
path to resolve and an object with the following structure as the second
argument:

```
```json
{
"basedir": string,
"browser": bool,
Expand Down Expand Up @@ -766,7 +766,7 @@ test(() => {

Rendered snapshot:

```
```json
Pretty foo: Object {
"x": 1,
"y": 2,
Expand Down Expand Up @@ -907,7 +907,7 @@ specify both options.

The following is a visualization of the default regex:

```
```bash
├── __tests__
│ └── component.spec.js # test
│ └── anything # test
Expand All @@ -925,7 +925,7 @@ This option allows the use of a custom results processor. This processor must be
a node module that exports a function expecting an object with the following
structure as the first argument and return it:

```
```json
{
"success": bool,
"startTime": epoch,
Expand Down Expand Up @@ -976,13 +976,13 @@ implementation.

The test runner module must export a function with the following signature:

```
```ts
function testRunner(
config: Config,
environment: Environment,
runtime: Runtime,
testPath: string,
): Promise<TestResult>
): Promise<TestResult>;
```

An example of such function can be found in our default
Expand Down
4 changes: 2 additions & 2 deletions docs/ExpectAPI.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ expect.extend({

This will print something like this:

```
```bash
expect(received).toBe(expected)

Expected value to be (using Object.is):
Expand Down Expand Up @@ -1050,7 +1050,7 @@ test('throws on octopus', () => {

And it will generate the following snapshot:

```
```js
exports[`drinking flavors throws on octopus 1`] = `"yuck, octopus flavor"`;
```

Expand Down
10 changes: 5 additions & 5 deletions docs/GettingStarted.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ title: Getting Started

Install Jest using [`npm`](https://www.npmjs.com/):

```
```bash
npm install --save-dev jest
```

Or via [`yarn`](https://yarnpkg.com/en/package/jest):

```
```bash
yarn add --dev jest
```

Expand Down Expand Up @@ -47,7 +47,7 @@ Add the following section to your `package.json`:

Finally, run `npm test` and Jest will print this message:

```
```bash
PASS ./sum.test.js
✓ adds 1 + 2 to equal 3 (5ms)
```
Expand Down Expand Up @@ -80,14 +80,14 @@ a look at the [Jest CLI Options](CLI.md) page.
To use [Babel](http://babeljs.io/), install the `babel-jest` and
`regenerator-runtime` packages:

```
```bash
npm install --save-dev babel-jest babel-core regenerator-runtime
```

> Note: If you are using a babel version 7 then you need to install `babel-jest`
> with the following command:
>
> ```
> ```bash
> npm install --save-dev babel-jest 'babel-core@^7.0.0-0' @babel/core regenerator-runtime
> ```

Expand Down
4 changes: 2 additions & 2 deletions docs/MigrationGuide.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,14 @@ dirty migration work. It runs a code transformation on your codebase using

Install Jest Codemods with `npm` by running:

```
```bash
npm install -g jest-codemods
```

To transform your existing tests, navigate to the project containing the tests
and run:

```
```bash
jest-codemods
```

Expand Down
63 changes: 33 additions & 30 deletions docs/MockFunctionAPI.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,25 +127,25 @@ mockFn.mock.calls[1][0] === 1; // true

`mockImplementation` can also be used to mock class constructors:

```
```js
// SomeClass.js
module.exports = class SomeClass {
m(a, b) {}
}
};

// OtherModule.test.js
jest.mock('./SomeClass'); // this happens automatically with automocking
const SomeClass = require('./SomeClass')
const mMock = jest.fn()
jest.mock('./SomeClass'); // this happens automatically with automocking
const SomeClass = require('./SomeClass');
const mMock = jest.fn();
SomeClass.mockImplementation(() => {
return {
m: mMock
}
})
m: mMock,
};
});

const some = new SomeClass()
some.m('a', 'b')
console.log('Calls to m: ', mMock.mock.calls)
const some = new SomeClass();
some.m('a', 'b');
console.log('Calls to m: ', mMock.mock.calls);
```

### `mockFn.mockImplementationOnce(fn)`
Expand All @@ -154,30 +154,30 @@ Accepts a function that will be used as an implementation of the mock for one
call to the mocked function. Can be chained so that multiple function calls
produce different results.

```
var myMockFn = jest.fn()
```js
const myMockFn = jest
.fn()
.mockImplementationOnce(cb => cb(null, true))
.mockImplementationOnce(cb => cb(null, false));

myMockFn((err, val) => console.log(val));
> true
myMockFn((err, val) => console.log(val)); // true

myMockFn((err, val) => console.log(val));
> false
myMockFn((err, val) => console.log(val)); // false
```

When the mocked function runs out of implementations defined with
mockImplementationOnce, it will execute the default implementation set with
`jest.fn(() => defaultValue)` or `.mockImplementation(() => defaultValue)` if
they were called:

```
var myMockFn = jest.fn(() => 'default')
```js
const myMockFn = jest
.fn(() => 'default')
.mockImplementationOnce(() => 'first call')
.mockImplementationOnce(() => 'second call');

// 'first call', 'second call', 'default', 'default'
console.log(myMockFn(), myMockFn(), myMockFn(), myMockFn());
> 'first call', 'second call', 'default', 'default'
```

### `mockFn.mockName(value)`
Expand All @@ -197,7 +197,7 @@ expect(mockFn).toHaveBeenCalled();

Will result in this error:

```
```bash
expect(mockedFunction).toHaveBeenCalled()

Expected mock function to have been called.
Expand Down Expand Up @@ -232,14 +232,15 @@ chained so that successive calls to the mock function return different values.
When there are no more `mockReturnValueOnce` values to use, calls will return a
value specified by `mockReturnValue`.

```
const myMockFn = jest.fn()
```js
const myMockFn = jest
.fn()
.mockReturnValue('default')
.mockReturnValueOnce('first call')
.mockReturnValueOnce('second call');

// 'first call', 'second call', 'default', 'default'
console.log(myMockFn(), myMockFn(), myMockFn(), myMockFn());
> 'first call', 'second call', 'default', 'default'
```

### `mockFn.mockResolvedValue(value)`
Expand All @@ -255,7 +256,7 @@ Useful to mock async functions in async tests:
```js
test('async test', async () => {
const asyncMock = jest.fn().mockResolvedValue(43);

await asyncMock(); // 43
});
```
Expand All @@ -272,11 +273,12 @@ Useful to resolve different values over multiple async calls:

```js
test('async test', async () => {
const asyncMock = jest.fn()
const asyncMock = jest
.fn()
.mockResolvedValue('default')
.mockResolvedValueOnce('first call')
.mockResolvedValueOnce('second call');

await asyncMock(); // first call
await asyncMock(); // second call
await asyncMock(); // default
Expand All @@ -297,7 +299,7 @@ Useful to create async mock functions that will always reject:
```js
test('async test', async () => {
const asyncMock = jest.fn().mockRejectedValue(new Error('Async error'));

await asyncMock(); // throws "Async error"
});
```
Expand All @@ -314,10 +316,11 @@ Example usage:

```js
test('async test', async () => {
const asyncMock = jest.fn()
const asyncMock = jest
.fn()
.mockResolvedValueOnce('first call')
.mockRejectedValueOnce(new Error('Async error'));

await asyncMock(); // first call
await asyncMock(); // throws "Async error"
});
Expand Down
Loading