From 1a81a291dc085a7b53c27dd121a3dd8df4b63ead Mon Sep 17 00:00:00 2001 From: Connor Imrie Date: Thu, 13 Apr 2017 14:18:39 +0100 Subject: [PATCH 01/11] Add mongodb isolated testing recipe. --- .../isolating-mongodb-integration-tests.md | 86 +++++++++++++++++++ 1 file changed, 86 insertions(+) create mode 100644 docs/recipes/isolating-mongodb-integration-tests.md diff --git a/docs/recipes/isolating-mongodb-integration-tests.md b/docs/recipes/isolating-mongodb-integration-tests.md new file mode 100644 index 000000000..7c0988612 --- /dev/null +++ b/docs/recipes/isolating-mongodb-integration-tests.md @@ -0,0 +1,86 @@ +# Setting up Ava for Isolated MongoDB Integration Tests + +This recipe outlines how to run disposable MongoDB databases in your Ava tests, isolated between tests. + +## Install MongoDB in-memory Server (MongoMem) +In the root directory of your application, run: + +`npm install mongomem --save-dev` + +## 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.* + +```javascript +import { MongoDBServer } from 'mongomem'; +let hasStarted = MongoDBServer.start(); + +// ES6 promise syntax +test('some feature - es6', t => { + // wait for server to be ready + hasStarted.then(() => { + MongoDB.getConnectionString().then(connectionString => { + // run logic with access to connectionString + }); + }); +}); + +// async/await syntax +test('some feature - async/await', async t => { + await hasStarted; + let connectionString = await MongoDBServer.getConnectionString(); +}); +``` + +## 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 output to the console upon start, and you will see any connection errors here. + +## Extra: Setup and Use in Mongoose (MongoDB ODM) +Mongoose is a robust ODM for MongoDB. + +### Install +`npm install mongoose --save` + +Refer to [Mongoose ODM v4.9.4](http://mongoosejs.com/index.html) for full guides and documentation to get started with Mongoose. + +### Import Mongoose + +```javascript +// 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 by importing ‘mongoose’. This is great for your application as it maintains a single access point to your database, but less great for isolated testing. + +You should isolate mongoose between your tests so that the order of test execution is never depended on, and this can be done with a little bit of work. + +### Isolate Mongoose Instance + +You can easily request a new instance of Mongoose. +First, call `new mongoose.Mongoose()` to get the new instance, and then call `connect` with a database connection string provided by the `mongomem` package. + +*You will need to re-attach the old models from the global mongoose to the new one as it does not carry this information over.* + +```javascript +import mongoose from 'mongoose'; +import { MongoDBServer } from 'mongomem' + +let hasStarted = MongoDBServer.start(); + +test('my mongoose model integration test', async t => { + await hasStarted; + const odm = new mongoose.Mongoose(); + odm.connect(await MongoDBServer.getConnectionString()); + + // Re-assign original mongose models to new Mongoose connection + Object.keys(mongoose.models).forEach(name => { + const model = mongoose.models[name]; + odm.model(name, model.schema); + }); + + // Now you can test with Mongoose using the 'odm' variable... +}); +``` From fbb2fef3b01b1a7e124739960695375795715d9a Mon Sep 17 00:00:00 2001 From: Connor Imrie Date: Thu, 13 Apr 2017 14:21:31 +0100 Subject: [PATCH 02/11] Update temporary variable reference in mongodb recipe. --- docs/recipes/isolating-mongodb-integration-tests.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/recipes/isolating-mongodb-integration-tests.md b/docs/recipes/isolating-mongodb-integration-tests.md index 7c0988612..f7b37bf50 100644 --- a/docs/recipes/isolating-mongodb-integration-tests.md +++ b/docs/recipes/isolating-mongodb-integration-tests.md @@ -77,7 +77,7 @@ test('my mongoose model integration test', async t => { // Re-assign original mongose models to new Mongoose connection Object.keys(mongoose.models).forEach(name => { - const model = mongoose.models[name]; + let model = mongoose.models[name]; odm.model(name, model.schema); }); From 134e3b141017722e8fa5d6ccdc9ab5a3c50208fb Mon Sep 17 00:00:00 2001 From: Connor Imrie Date: Thu, 13 Apr 2017 21:14:19 +0100 Subject: [PATCH 03/11] Adjust testing recipe to feedback. Include documentation on server tear-down. --- .../isolating-mongodb-integration-tests.md | 98 +++++++++++-------- 1 file changed, 59 insertions(+), 39 deletions(-) diff --git a/docs/recipes/isolating-mongodb-integration-tests.md b/docs/recipes/isolating-mongodb-integration-tests.md index f7b37bf50..cf637d1f1 100644 --- a/docs/recipes/isolating-mongodb-integration-tests.md +++ b/docs/recipes/isolating-mongodb-integration-tests.md @@ -1,48 +1,63 @@ -# Setting up Ava for Isolated MongoDB Integration Tests +# Setting up AVA for Isolated MongoDB Integration Tests -This recipe outlines how to run disposable MongoDB databases in your Ava tests, isolated between tests. +This recipe outlines how to run disposable MongoDB databases in your AVA tests with per-test isolation. +This uses `mongomem` and is available on [npm](https://www.npmjs.com/package/mongomem). + +`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. ## Install MongoDB in-memory Server (MongoMem) In the root directory of your application, run: -`npm install mongomem --save-dev` +```console +$ 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.* +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.** ```javascript +import test from 'ava'; import { MongoDBServer } from 'mongomem'; -let hasStarted = MongoDBServer.start(); - -// ES6 promise syntax -test('some feature - es6', t => { - // wait for server to be ready - hasStarted.then(() => { - MongoDB.getConnectionString().then(connectionString => { - // run logic with access to connectionString - }); - }); + +test.before('start mongodb server', async t => { + await MongoDBServer.start(); +}) + +test('some feature', async t => { + const connectionString = await MongoDBServer.getConnectionString(); + + // connectionString === 'mongodb://localhost:27017/3411fd12-b5d6-4860-854c-5bbdb011cb93' + // use connectionString to connect to the database with a client of your choice. See below for usage with Mongoose. }); -// async/await syntax -test('some feature - async/await', async t => { - await hasStarted; - let connectionString = await MongoDBServer.getConnectionString(); +``` + +## Cleaning Up + +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. + +```javascript +test.after.always('cleanup', async t => { + MongoDBServer.tearDown(); // This will clean 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 output to the console upon start, and you will see any connection errors here. +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) -Mongoose is a robust ODM for MongoDB. - -### Install -`npm install mongoose --save` +Mongoose is a robust Object-Document-Mapper (ODM) for MongoDB. Refer to [Mongoose ODM v4.9.4](http://mongoosejs.com/index.html) for full guides and documentation to get started with Mongoose. @@ -53,34 +68,39 @@ Refer to [Mongoose ODM v4.9.4](http://mongoosejs.com/index.html) for full guides import mongoose from 'mongoose'; ``` -`mongoose` in this case is a single instance of the Mongoose ODM and is globally available by importing ‘mongoose’. This is great for your application 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 application as it maintains a single access point to your database, but less great for isolated testing. -You should isolate mongoose between your tests so that the order of test execution is never depended on, and this can be done with a little bit of work. +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. ### Isolate Mongoose Instance You can easily request a new instance of Mongoose. First, call `new mongoose.Mongoose()` to get the new instance, and then call `connect` with a database connection string provided by the `mongomem` package. -*You will need to re-attach the old models from the global mongoose to the new one as it does not carry this information over.* +**You will have to manually copy models from the global instance to your new instance.** ```javascript import mongoose from 'mongoose'; import { MongoDBServer } from 'mongomem' -let hasStarted = MongoDBServer.start(); +test.before('start mongodb', async t => { + await MongoDBServer.start(); +}); -test('my mongoose model integration test', async t => { - await hasStarted; - const odm = new mongoose.Mongoose(); - odm.connect(await MongoDBServer.getConnectionString()); - - // Re-assign original mongose models to new Mongoose connection - Object.keys(mongoose.models).forEach(name => { - let model = mongoose.models[name]; - odm.model(name, model.schema); - }); +test.beforeEach(async t => { + const db = new mongoose.Mongoose(); + await db.connect(await MongoDBServer.getConnectionString()); + + for (const name of mongoose.modelNames()) { + db.model(name, mongoose.model(name)); + } + + t.context.db = db; +}); - // Now you can test with Mongoose using the 'odm' variable... +test('my mongoose model integration test', async t => { + const {db} = t.context; + // do something… }); ``` From 2f72002cfc2ad1bb4078f62aa1dc531c5a6beeb9 Mon Sep 17 00:00:00 2001 From: Connor Imrie Date: Fri, 14 Apr 2017 10:02:23 +0100 Subject: [PATCH 04/11] Fix buggy code in copying models to new mongoose instance. Should use schema, not the model. --- docs/recipes/isolating-mongodb-integration-tests.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/recipes/isolating-mongodb-integration-tests.md b/docs/recipes/isolating-mongodb-integration-tests.md index cf637d1f1..96b2838bd 100644 --- a/docs/recipes/isolating-mongodb-integration-tests.md +++ b/docs/recipes/isolating-mongodb-integration-tests.md @@ -93,7 +93,7 @@ test.beforeEach(async t => { await db.connect(await MongoDBServer.getConnectionString()); for (const name of mongoose.modelNames()) { - db.model(name, mongoose.model(name)); + db.model(name, mongoose.model(name).schema); } t.context.db = db; From 3b4423f354077ad5bd67f7c704e40f47ade9e11b Mon Sep 17 00:00:00 2001 From: Connor Imrie Date: Mon, 17 Apr 2017 20:56:58 +0100 Subject: [PATCH 05/11] Indent with tabs --- .../isolating-mongodb-integration-tests.md | 58 ++++++++++++++++++- 1 file changed, 57 insertions(+), 1 deletion(-) diff --git a/docs/recipes/isolating-mongodb-integration-tests.md b/docs/recipes/isolating-mongodb-integration-tests.md index f7b37bf50..f67d0ea33 100644 --- a/docs/recipes/isolating-mongodb-integration-tests.md +++ b/docs/recipes/isolating-mongodb-integration-tests.md @@ -1,6 +1,14 @@ # Setting up Ava for Isolated MongoDB Integration Tests +<<<<<<< Updated upstream This recipe outlines how to run disposable MongoDB databases in your Ava tests, isolated between 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). + +`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. +>>>>>>> Stashed changes ## Install MongoDB in-memory Server (MongoMem) In the root directory of your application, run: @@ -13,6 +21,7 @@ In your test file, import the module and run the server. ```javascript import { MongoDBServer } from 'mongomem'; +<<<<<<< Updated upstream let hasStarted = MongoDBServer.start(); // ES6 promise syntax @@ -29,6 +38,32 @@ test('some feature - es6', t => { test('some feature - async/await', async t => { await hasStarted; let connectionString = await MongoDBServer.getConnectionString(); +======= + +test.before('start mongodb server', async t => { + await MongoDBServer.start(); +}) + +test('some feature', async t => { + const connectionString = await MongoDBServer.getConnectionString(); + + // connectionString === 'mongodb://localhost:27017/3411fd12-b5d6-4860-854c-5bbdb011cb93' + // use connectionString to connect to the database with a client of your choice. See below for usage with Mongoose. +}); + +``` + +## Cleaning Up + +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. + +```javascript +test.after.always('cleanup', t => { + MongoDBServer.tearDown(); // This will clean up temporary file storage. +>>>>>>> Stashed changes }); ``` @@ -66,8 +101,9 @@ First, call `new mongoose.Mongoose()` to get the new instance, and then call `co ```javascript import mongoose from 'mongoose'; -import { MongoDBServer } from 'mongomem' +import { MongoDBServer } from 'mongomem'; +<<<<<<< Updated upstream let hasStarted = MongoDBServer.start(); test('my mongoose model integration test', async t => { @@ -82,5 +118,25 @@ test('my mongoose model integration test', async t => { }); // Now you can test with Mongoose using the 'odm' variable... +======= +test.before('start mongodb', async t => { + await MongoDBServer.start(); +}); + +test.beforeEach(async t => { + const db = new mongoose.Mongoose(); + await db.connect(await MongoDBServer.getConnectionString()); + + for (const name of mongoose.modelNames()) { + db.model(name, mongoose.model(name).schema); + } + + t.context.db = db; +}); + +test('my mongoose model integration test', async t => { + const {db} = t.context; + // Now use the isolated DB instance in your test... +>>>>>>> Stashed changes }); ``` From 6fe2ec13d23a57019d609f67b192505a1c3989f5 Mon Sep 17 00:00:00 2001 From: Connor Imrie Date: Mon, 17 Apr 2017 20:59:44 +0100 Subject: [PATCH 06/11] Retry without phpstorm ruining my life. --- .../isolating-mongodb-integration-tests.md | 72 +++++-------------- 1 file changed, 18 insertions(+), 54 deletions(-) diff --git a/docs/recipes/isolating-mongodb-integration-tests.md b/docs/recipes/isolating-mongodb-integration-tests.md index f67d0ea33..fb37f3ccb 100644 --- a/docs/recipes/isolating-mongodb-integration-tests.md +++ b/docs/recipes/isolating-mongodb-integration-tests.md @@ -1,44 +1,26 @@ -# Setting up Ava for Isolated MongoDB Integration Tests +# Setting up AVA for Isolated MongoDB Integration Tests -<<<<<<< Updated upstream -This recipe outlines how to run disposable MongoDB databases in your Ava tests, isolated between 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). `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. ->>>>>>> Stashed changes ## Install MongoDB in-memory Server (MongoMem) In the root directory of your application, run: -`npm install mongomem --save-dev` +```console +$ 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.* +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.** ```javascript +import test from 'ava'; import { MongoDBServer } from 'mongomem'; -<<<<<<< Updated upstream -let hasStarted = MongoDBServer.start(); - -// ES6 promise syntax -test('some feature - es6', t => { - // wait for server to be ready - hasStarted.then(() => { - MongoDB.getConnectionString().then(connectionString => { - // run logic with access to connectionString - }); - }); -}); - -// async/await syntax -test('some feature - async/await', async t => { - await hasStarted; - let connectionString = await MongoDBServer.getConnectionString(); -======= test.before('start mongodb server', async t => { await MongoDBServer.start(); @@ -62,22 +44,20 @@ This is normally cleaned up by your operating system but it is good practise to ```javascript test.after.always('cleanup', t => { - MongoDBServer.tearDown(); // This will clean up temporary file storage. ->>>>>>> Stashed changes + MongoDBServer.tearDown(); // This will clean 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 output to the console upon start, and you will see any connection errors here. +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) -Mongoose is a robust ODM for MongoDB. - -### Install -`npm install mongoose --save` +Mongoose is a robust Object-Document-Mapper (ODM) for MongoDB. Refer to [Mongoose ODM v4.9.4](http://mongoosejs.com/index.html) for full guides and documentation to get started with Mongoose. @@ -88,39 +68,24 @@ Refer to [Mongoose ODM v4.9.4](http://mongoosejs.com/index.html) for full guides import mongoose from 'mongoose'; ``` -`mongoose` in this case is a single instance of the Mongoose ODM and is globally available by importing ‘mongoose’. This is great for your application 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 application as it maintains a single access point to your database, but less great for isolated testing. -You should isolate mongoose between your tests so that the order of test execution is never depended on, and this can be done with a little bit of work. +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. ### Isolate Mongoose Instance You can easily request a new instance of Mongoose. First, call `new mongoose.Mongoose()` to get the new instance, and then call `connect` with a database connection string provided by the `mongomem` package. -*You will need to re-attach the old models from the global mongoose to the new one as it does not carry this information over.* +**You will have to manually copy models from the global instance to your new instance.** ```javascript import mongoose from 'mongoose'; import { MongoDBServer } from 'mongomem'; -<<<<<<< Updated upstream -let hasStarted = MongoDBServer.start(); - -test('my mongoose model integration test', async t => { - await hasStarted; - const odm = new mongoose.Mongoose(); - odm.connect(await MongoDBServer.getConnectionString()); - - // Re-assign original mongose models to new Mongoose connection - Object.keys(mongoose.models).forEach(name => { - let model = mongoose.models[name]; - odm.model(name, model.schema); - }); - - // Now you can test with Mongoose using the 'odm' variable... -======= test.before('start mongodb', async t => { - await MongoDBServer.start(); +await MongoDBServer.start(); }); test.beforeEach(async t => { @@ -137,6 +102,5 @@ test.beforeEach(async t => { test('my mongoose model integration test', async t => { const {db} = t.context; // Now use the isolated DB instance in your test... ->>>>>>> Stashed changes }); ``` From 8d621d74ef364978d36bad54fd235b53646f8f58 Mon Sep 17 00:00:00 2001 From: Connor Imrie Date: Mon, 17 Apr 2017 21:00:34 +0100 Subject: [PATCH 07/11] Make tab indentation consistent --- docs/recipes/isolating-mongodb-integration-tests.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/recipes/isolating-mongodb-integration-tests.md b/docs/recipes/isolating-mongodb-integration-tests.md index fb37f3ccb..92ce4ce10 100644 --- a/docs/recipes/isolating-mongodb-integration-tests.md +++ b/docs/recipes/isolating-mongodb-integration-tests.md @@ -85,7 +85,7 @@ import mongoose from 'mongoose'; import { MongoDBServer } from 'mongomem'; test.before('start mongodb', async t => { -await MongoDBServer.start(); + await MongoDBServer.start(); }); test.beforeEach(async t => { From 0fd57333767cc5ba57338fe6e0126b2f0835935e Mon Sep 17 00:00:00 2001 From: Sindre Sorhus Date: Tue, 18 Apr 2017 15:59:47 +0700 Subject: [PATCH 08/11] Update isolating-mongodb-integration-tests.md --- .../isolating-mongodb-integration-tests.md | 58 +++++++++---------- 1 file changed, 29 insertions(+), 29 deletions(-) diff --git a/docs/recipes/isolating-mongodb-integration-tests.md b/docs/recipes/isolating-mongodb-integration-tests.md index 92ce4ce10..c099082fe 100644 --- a/docs/recipes/isolating-mongodb-integration-tests.md +++ b/docs/recipes/isolating-mongodb-integration-tests.md @@ -1,26 +1,28 @@ # Setting up AVA for 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). +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). + +`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. -`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. ## Install MongoDB in-memory Server (MongoMem) + In the root directory of your application, run: ```console $ npm install --save-dev mongomem ``` + ## Using MongoMem -In your test file, import the module and run the server. + +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.** -```javascript +```js import test from 'ava'; -import { MongoDBServer } from 'mongomem'; +import {MongoDBServer} from 'mongomem'; test.before('start mongodb server', async t => { await MongoDBServer.start(); @@ -28,61 +30,59 @@ test.before('start mongodb server', async t => { test('some feature', async t => { const connectionString = await MongoDBServer.getConnectionString(); - + // connectionString === 'mongodb://localhost:27017/3411fd12-b5d6-4860-854c-5bbdb011cb93' - // use connectionString to connect to the database with a client of your choice. See below for usage with Mongoose. + // Use `connectionString` to connect to the database with a client of your choice. See below for usage with Mongoose. }); ``` + ## Cleaning Up -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. +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. -```javascript +```js test.after.always('cleanup', t => { - MongoDBServer.tearDown(); // This will clean up temporary file storage. + MongoDBServer.tearDown(); // This will clean 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. +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) -Mongoose is a robust Object-Document-Mapper (ODM) for MongoDB. -Refer to [Mongoose ODM v4.9.4](http://mongoosejs.com/index.html) for full guides and documentation to get started with Mongoose. +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. ### Import Mongoose -```javascript -// myTestCase.test.js - (your test case file!) +```js +// `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 application 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. +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. ### Isolate Mongoose Instance -You can easily request a new instance of Mongoose. -First, call `new mongoose.Mongoose()` to get the new instance, and then call `connect` with a database connection string provided by the `mongomem` package. +You can easily request a new instance of Mongoose. First, call `new mongoose.Mongoose()` to get the new instance, and then call `connect` with a database connection string provided by the `mongomem` package. **You will have to manually copy models from the global instance to your new instance.** -```javascript +```js import mongoose from 'mongoose'; -import { MongoDBServer } from 'mongomem'; +import {MongoDBServer} from 'mongomem'; test.before('start mongodb', async t => { await MongoDBServer.start(); @@ -91,16 +91,16 @@ test.before('start mongodb', async t => { test.beforeEach(async t => { const db = new mongoose.Mongoose(); await db.connect(await MongoDBServer.getConnectionString()); - + for (const name of mongoose.modelNames()) { db.model(name, mongoose.model(name).schema); } - + t.context.db = db; }); test('my mongoose model integration test', async t => { const {db} = t.context; - // Now use the isolated DB instance in your test... + // Now use the isolated DB instance in your test }); ``` From a6b48c4c1cd66df28f5f9e6bf77181a7c09d5e65 Mon Sep 17 00:00:00 2001 From: Connor Imrie Date: Sat, 6 May 2017 14:56:07 +0100 Subject: [PATCH 09/11] Add mongodb integration recipe to recipes list in readme --- readme.md | 1 + 1 file changed, 1 insertion(+) diff --git a/readme.md b/readme.md index 1283a43bb..fcdbcc4b0 100644 --- a/readme.md +++ b/readme.md @@ -1162,6 +1162,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) +- [Isolating MongoDB for integration tests](docs/recipes/isolating-mongodb-integration-tests.md) ## Support From a625c658041164ba6925f12f4eb0eb6525150065 Mon Sep 17 00:00:00 2001 From: Sindre Sorhus Date: Sat, 6 May 2017 22:05:17 +0800 Subject: [PATCH 10/11] Update isolating-mongodb-integration-tests.md --- docs/recipes/isolating-mongodb-integration-tests.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/docs/recipes/isolating-mongodb-integration-tests.md b/docs/recipes/isolating-mongodb-integration-tests.md index c099082fe..a0b9de7e4 100644 --- a/docs/recipes/isolating-mongodb-integration-tests.md +++ b/docs/recipes/isolating-mongodb-integration-tests.md @@ -1,4 +1,4 @@ -# Setting up AVA for Isolated MongoDB Integration Tests +# 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). @@ -34,7 +34,6 @@ test('some feature', async t => { // connectionString === 'mongodb://localhost:27017/3411fd12-b5d6-4860-854c-5bbdb011cb93' // Use `connectionString` to connect to the database with a client of your choice. See below for usage with Mongoose. }); - ``` From 388192661398f02463f47b772d662f25345792b5 Mon Sep 17 00:00:00 2001 From: Sindre Sorhus Date: Sat, 6 May 2017 22:06:03 +0800 Subject: [PATCH 11/11] Update readme.md --- readme.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/readme.md b/readme.md index fcdbcc4b0..945c2e19c 100644 --- a/readme.md +++ b/readme.md @@ -1162,7 +1162,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) -- [Isolating MongoDB for integration tests](docs/recipes/isolating-mongodb-integration-tests.md) +- [Isolated MongoDB integration tests](docs/recipes/isolating-mongodb-integration-tests.md) ## Support