Skip to content

Commit

Permalink
re-create docs for v22.3 with latest changes from master
Browse files Browse the repository at this point in the history
  • Loading branch information
ronami committed Feb 17, 2018
1 parent 8aaa211 commit 6bd9618
Show file tree
Hide file tree
Showing 30 changed files with 323 additions and 160 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
id: version-22.2.2-cli
id: version-22.3-cli
title: Jest CLI Options
original_id: cli
---
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
id: version-22.2.2-configuration
id: version-22.3-configuration
title: Configuring Jest
original_id: configuration
---
Expand Down Expand Up @@ -314,34 +314,6 @@ Default: `undefined`
This option allows the use of a custom global teardown module which exports an
async function that is triggered once after all test suites.

### `mapCoverage` [boolean]

##### available in Jest **20.0.0+**

Default: `false`

If you have [transformers](#transform-object-string-string) configured that emit
source maps, Jest will use them to try and map code coverage against the
original source code when writing [reports](#coveragereporters-array-string) and
checking [thresholds](#coveragethreshold-object). This is done on a best-effort
basis as some compile-to-JavaScript languages may provide more accurate source
maps than others. This can also be resource-intensive. If Jest is taking a long
time to calculate coverage at the end of a test run, try setting this option to
`false`.

Both inline source maps and source maps returned directly from a transformer are
supported. Source map URLs are not supported because Jest may not be able to
locate them. To return source maps from a transformer, the `process` function
can return an object like the following. The `map` property may either be the
source map object, or the source map object as a string.

```js
return {
code: 'the code',
map: 'the source map',
};
```

### `moduleFileExtensions` [array<string>]

Default: `["js", "json", "jsx", "node"]`
Expand Down Expand Up @@ -439,7 +411,8 @@ Specifies notification mode. Requires `notify: true`.
* `success`: send a notification when tests pass.
* `change`: send a notification when the status changed.
* `success-change`: send a notification when tests pass or once when it fails.
* `failure-success`: send a notification when tests fails or once when it passes.
* `failure-success`: send a notification when tests fails or once when it
passes.

### `preset` [string]

Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
id: version-22.2.2-expect
id: version-22.3-expect
title: Expect
original_id: expect
---
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
id: version-22.2.2-getting-started
id: version-22.3-getting-started
title: Getting Started
original_id: getting-started
---
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
id: version-22.2.2-api
id: version-22.3-api
title: Globals
original_id: api
---
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
id: version-22.2.2-jest-object
id: version-22.3-jest-object
title: The Jest Object
original_id: jest-object
---
Expand Down Expand Up @@ -159,7 +159,7 @@ as well as all the modules that it imports._

Modules that are mocked with `jest.mock` are mocked only for the file that calls
`jest.mock`. Another file that imports the module will get the original
implementation even if run after the test file that mocks the module.
implementation even if it runs after the test file that mocks the module.

Returns the `jest` object for chaining.

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
id: version-22.2.2-jest-platform
id: version-22.3-jest-platform
title: Jest Platform
original_id: jest-platform
---
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
id: version-22.2.2-manual-mocks
id: version-22.3-manual-mocks
title: Manual Mocks
original_id: manual-mocks
---
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
id: version-22.2.2-migration-guide
id: version-22.3-migration-guide
title: Migrating to Jest
original_id: migration-guide
---
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
id: version-22.2.2-mock-function-api
id: version-22.3-mock-function-api
title: Mock Functions
original_id: mock-function-api
---
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
id: version-22.2.2-mock-functions
id: version-22.3-mock-functions
title: Mock Functions
original_id: mock-functions
---
Expand Down
128 changes: 128 additions & 0 deletions website/versioned_docs/version-22.3/MongoDB.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
---
id: version-22.3-mongodb
title: Using with MongoDB
original_id: mongodb
---

With the [Global Setup/Teardown](Configuration.md#globalsetup-string) and
[Async Test Environment](Configuration.md#testenvironment-string) APIs, Jest can
work smoothly with [MongoDB](https://www.mongodb.com/).

## A jest-mongodb example

The basic idea is to:

1. Spin up in-memory mongodb server
2. Export a global variable with mongo URI
3. Write tests for queries / aggregations using a real database ✨
4. Shut down mongodb server using Global Teardown

Here's an example of the GlobalSetup script

```js
// setup.js
const MongodbMemoryServer = require('mongodb-memory-server');

const MONGO_DB_NAME = 'jest';
const mongod = new MongodbMemoryServer.default({
instance: {
dbName: MONGO_DB_NAME,
},
binary: {
version: '3.2.19',
},
});

module.exports = function() {
global.__MONGOD__ = mongod;
global.__MONGO_DB_NAME__ = MONGO_DB_NAME;
};
```

Then we need a custom Test Environment for Mongo

```js
// mongo-environment.js
class MongoEnvironment extends NodeEnvironment {
constructor(config) {
super(config);
}

async setup() {
console.log('Setup MongoDB Test Environment');

this.global.__MONGO_URI__ = await global.__MONGOD__.getConnectionString();
this.global.__MONGO_DB_NAME__ = global.__MONGO_DB_NAME__;

await super.setup();
}

async teardown() {
console.log('Teardown MongoDB Test Environment');

await super.teardown();
}

runScript(script) {
return super.runScript(script);
}
}
```

Finally we can shut down mongodb server

```js
// teardown.js
module.exports = async function() {
await global.__MONGOD__.stop();
};
```

With all the things set up, we can now write our tests like this:

```js
// test.js
const {MongoClient} = require('mongodb');

let connection;
let db;

beforeAll(async () => {
connection = await MongoClient.connect(global.__MONGO_URI__);
db = await connection.db(global.__MONGO_DB_NAME__);
});

afterAll(async () => {
await connection.close();
await db.close();
});

it('should aggregate docs from collection', async () => {
const files = db.collection('files');

await files.insertMany([
{type: 'Document'},
{type: 'Video'},
{type: 'Image'},
{type: 'Document'},
{type: 'Image'},
{type: 'Document'},
]);

const topFiles = await files
.aggregate([
{$group: {_id: '$type', count: {$sum: 1}}},
{$sort: {count: -1}},
])
.toArray();

expect(topFiles).toEqual([
{_id: 'Document', count: 3},
{_id: 'Image', count: 2},
{_id: 'Video', count: 1},
]);
});
```

Here's the code of
[full working example](https://github.com/vladgolubev/jest-mongodb).
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
id: version-22.2.2-more-resources
id: version-22.3-more-resources
title: More Resources
original_id: more-resources
---
Expand Down Expand Up @@ -33,9 +33,7 @@ projects.

Ask questions and find answers from other Jest users like you.
[Reactiflux](http://www.reactiflux.com/) is a Discord chat where a lot of Jest
discussion happens. Check out the
[#jest](https://discordapp.com/channels/102860784329052160/103622435865104384)
channel.
discussion happens. Check out the [#jest](https://discord.gg/MWRhKCj) channel.

Follow the [Jest Twitter account](https://twitter.com/fbjest) and
[blog](/jest/blog/) to find out what's happening in the world of Jest.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
id: version-22.2.2-puppeteer
id: version-22.3-puppeteer
title: Using with puppeteer
original_id: puppeteer
---
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
id: version-22.2.2-setup-teardown
id: version-22.3-setup-teardown
title: Setup and Teardown
original_id: setup-teardown
---
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
id: version-22.2.2-snapshot-testing
id: version-22.3-snapshot-testing
title: Snapshot Testing
original_id: snapshot-testing
---
Expand Down Expand Up @@ -128,9 +128,9 @@ Failed snapshots can also be updated interactively in watch mode:

![](/jest/img/content/interactiveSnapshot.png)

Once you enter Interactive Snapshot Mode, Jest will step you through
the failed snapshots one test suite at a time and give you the opportunity to
review the failed output.
Once you enter Interactive Snapshot Mode, Jest will step you through the failed
snapshots one test suite at a time and give you the opportunity to review the
failed output.

From here you can choose to update that snapshot or skip to the next:

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
id: version-22.2.2-asynchronous
id: version-22.3-asynchronous
title: Testing Asynchronous Code
original_id: asynchronous
---
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
id: version-22.2.2-testing-frameworks
id: version-22.3-testing-frameworks
title: Testing Web Frameworks
original_id: testing-frameworks
---
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
id: version-22.2.2-timer-mocks
id: version-22.3-timer-mocks
title: Timer Mocks
original_id: timer-mocks
---
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
id: version-22.2.2-troubleshooting
id: version-22.3-troubleshooting
title: Troubleshooting
original_id: troubleshooting
---
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
id: version-22.2.2-tutorial-async
id: version-22.3-tutorial-async
title: An Async Example
original_id: tutorial-async
---
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
id: version-22.2.2-tutorial-react
id: version-22.3-tutorial-react
title: Testing React Apps
original_id: tutorial-react
---
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
id: version-22.2.2-tutorial-react-native
id: version-22.3-tutorial-react-native
title: Testing React Native Apps
original_id: tutorial-react-native
---
Expand Down Expand Up @@ -83,11 +83,9 @@ the component and capture the rendered output and create a snapshot file:

```javascript
// __tests__/Intro-test.js
import 'react-native';
import React from 'react';
import Intro from '../Intro';

// Note: test renderer must be required after react-native.
import renderer from 'react-test-renderer';

test('renders correctly', () => {
Expand Down Expand Up @@ -251,17 +249,6 @@ real device and then modeling a manual mock after the real module.
If you end up mocking the same modules over and over it is recommended to define
these mocks in a separate file and add it to the list of `setupFiles`.

### require react-native before the test renderer

Currently it is required to require react-native before loading the test
renderer:

```js
import 'react-native';
// Require after react-native
import renderer from 'react-test-renderer';
```

### `@providesModule`

If you'd like to use Facebook's `@providesModule` module system through an npm
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
id: version-22.2.2-tutorial-jquery
id: version-22.3-tutorial-jquery
title: DOM Manipulation
original_id: tutorial-jquery
---
Expand Down
Loading

0 comments on commit 6bd9618

Please sign in to comment.