Skip to content

Commit

Permalink
Clean up #1357
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed May 6, 2017
1 parent 970872c commit cf5c459
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 18 deletions.
@@ -1,13 +1,13 @@
# Isolated MongoDB integration tests

This recipe outlines how to run disposable MongoDB databases in your AVA tests with per-test isolation. This uses `mongomem` which is available on [npm](https://www.npmjs.com/package/mongomem).
> How to run disposable MongoDB databases in your AVA tests with per-test isolation.
`mongomem` is a package that allows you to quickly run a temporary MongoDB server locally. It uses temporary file storage which is destroyed when the server stops.
This uses [`MongoMem`](https://github.com/CImrie/mongomem), which allows you to quickly run a temporary MongoDB server locally. It uses temporary file storage which is destroyed when the server stops.


## Install MongoDB in-memory Server (MongoMem)

In the root directory of your application, run:
In the root directory of your app, run:

```console
$ npm install --save-dev mongomem
Expand All @@ -17,14 +17,14 @@ $ npm install --save-dev mongomem
## Using MongoMem

In your test file, import the module, and run the server.

**Make sure to run the server at the start of your file, outside of any test cases.**

```js
import test from 'ava';
import {MongoDBServer} from 'mongomem';

test.before('start mongodb server', async t => {
test.before('start server', async t => {
await MongoDBServer.start();
})

Expand All @@ -41,35 +41,32 @@ test('some feature', async t => {

After you have run your tests, you should include a `test.after.always()` method to clean up the MongoDB server. This will remove any temporary files the server used while running.

This is normally cleaned up by your operating system but it is good practise to do it manually to avoid OS-specific issues.
This is normally cleaned up by your operating system, but it is good practise to do it manually.

```js
test.after.always('cleanup', t => {
MongoDBServer.tearDown(); // This will clean up temporary file storage
MongoDBServer.tearDown(); // Cleans up temporary file storage
});
```


## Debugging

If the server does not seem to start, you can set the following option before you call `MongoDBServer.start()`:
`MongoDBServer.debug = true;`

This will allow the MongoDB server to print connection or file permission errors when it's starting. It checks and picks an available port to run the server on, so errors are likely to be related to file permissions.
If the server does not seem to start, you can set the `MongoDBServer.debug = true;` option before you call `MongoDBServer.start()`. This will allow the MongoDB server to print connection or file permission errors when it's starting. It checks and picks an available port to run the server on, so errors are likely to be related to file permissions.


## Extra: Setup and Use in Mongoose (MongoDB ODM)
## Extra: Setup and use in Mongoose (MongoDB ODM)

Mongoose is a robust Object-Document-Mapper (ODM) for MongoDB. Refer to [Mongoose ODM v4.9.4](http://mongoosejs.com) for full guides and documentation to get started with Mongoose.
[Mongoose](http://mongoosejs.com) is a robust Object-Document-Mapper (ODM) for MongoDB. Refer to its documentation to get started with Mongoose.

### Import Mongoose

```js
// `myTestCase.test.js` - (your test case file!)
// `myTestCase.test.js` - (Your test case file)
import mongoose from 'mongoose';
```

`mongoose` in this case is a single instance of the Mongoose ODM and is globally available. This is great for your app as it maintains a single access point to your database, but less great for isolated testing.
`mongoose` in this case is a single instance of the Mongoose ODM and is globally available. This is great for your app as it maintains a single access point to your database, but less great for isolated testing.

You should isolate Mongoose instances between your tests, so that the order of test execution is never depended on. This can be done with a little bit of work.

Expand All @@ -83,7 +80,7 @@ You can easily request a new instance of Mongoose. First, call `new mongoose.Mon
import mongoose from 'mongoose';
import {MongoDBServer} from 'mongomem';

test.before('start mongodb', async t => {
test.before('start server', async t => {
await MongoDBServer.start();
});

Expand All @@ -98,7 +95,7 @@ test.beforeEach(async t => {
t.context.db = db;
});

test('my mongoose model integration test', async t => {
test('my Mongoose model integration test', async t => {
const {db} = t.context;
// Now use the isolated DB instance in your test
});
Expand Down
2 changes: 1 addition & 1 deletion readme.md
Expand Up @@ -1163,7 +1163,7 @@ It's the [Andromeda galaxy](https://simple.wikipedia.org/wiki/Andromeda_galaxy).
- [Debugging tests with Chrome DevTools](docs/recipes/debugging-with-chrome-devtools.md)
- [Debugging tests with WebStorm](docs/recipes/debugging-with-webstorm.md)
- [Precompiling source files with webpack](docs/recipes/precompiling-with-webpack.md)
- [Isolated MongoDB integration tests](docs/recipes/isolating-mongodb-integration-tests.md)
- [Isolated MongoDB integration tests](docs/recipes/isolated-mongodb-integration-tests.md)

## Support

Expand Down

0 comments on commit cf5c459

Please sign in to comment.