|
| 1 | +# Endpoint testing with Mongoose |
| 2 | + |
| 3 | +This recipe shows you how to test your endpoints with AVA and Mongoose, assuming you use Express as your framework. |
| 4 | + |
| 5 | +## Setup |
| 6 | + |
| 7 | +This recipe uses the following libraries: |
| 8 | + |
| 9 | +1. [`mongod-memory-server`](https://github.com/nodkz/mongodb-memory-server) (A MongoDB in-memory Server) |
| 10 | +2. [SuperTest](https://github.com/visionmedia/supertest) (An endpoint testing library) |
| 11 | +3. [Mongoose](http://mongoosejs.com) |
| 12 | + |
| 13 | +Install the first two libraries by running the following code: |
| 14 | + |
| 15 | +```console |
| 16 | +$ npm install --save-dev mongodb-memory-server supertest |
| 17 | +``` |
| 18 | + |
| 19 | +You should have Mongoose installed already. If not, run the following code to install it: |
| 20 | + |
| 21 | +(Note: You need at least Mongoose v4.11.3) |
| 22 | + |
| 23 | +```console |
| 24 | +$ npm install mongoose |
| 25 | +``` |
| 26 | + |
| 27 | +## Prerequisites |
| 28 | + |
| 29 | +You'll need a server file and a Mongoose model. See the [`server.js`](https://github.com/zellwk/ava-mdb-test/blob/master/server.js) and [`models/User.js`](https://github.com/zellwk/ava-mdb-test/blob/master/models/User.js) examples. |
| 30 | + |
| 31 | +Note that `server.js` does not start the app. Instead this must be done by SuperTest, so that the app endpoints can be tested. If you're using Express for your application, make sure you have a startup file that imports `app` and calls `app.listen()`. |
| 32 | + |
| 33 | +## Your test file |
| 34 | + |
| 35 | +First, include the libraries you need: |
| 36 | + |
| 37 | +```js |
| 38 | +// Libraries required for testing |
| 39 | +import test from 'ava' |
| 40 | +import request from 'supertest' |
| 41 | +import MongodbMemoryServer from 'mongodb-memory-server' |
| 42 | +import mongoose from 'mongoose' |
| 43 | + |
| 44 | +// Your server and models |
| 45 | +import app from '../server' |
| 46 | +import User from '../models/User' |
| 47 | +``` |
| 48 | + |
| 49 | +Next start the in-memory MongoDB instance and connect to Mongoose: |
| 50 | + |
| 51 | +```js |
| 52 | +// Start MongoDB instance |
| 53 | +const mongod = new MongodbMemoryServer() |
| 54 | + |
| 55 | +// Create connection to Mongoose before tests are run |
| 56 | +test.before(async () => { |
| 57 | + const uri = await mongod.getConnectionString(); |
| 58 | + await mongoose.connect(uri, {useMongoClient: true}); |
| 59 | +}); |
| 60 | +``` |
| 61 | + |
| 62 | +When you run your first test, MongoDB downloads the latest MongoDB binaries. The download is ~70MB so this may take a minute. |
| 63 | + |
| 64 | +You'll want to populate your database with dummy data. Here's an example: |
| 65 | + |
| 66 | +```js |
| 67 | +test.beforeEach(async () => { |
| 68 | + const user = new User({ |
| 69 | + |
| 70 | + name: 'One' |
| 71 | + }); |
| 72 | + await user.save(); |
| 73 | +}); |
| 74 | +``` |
| 75 | + |
| 76 | +Dummy data should be cleared after each test: |
| 77 | + |
| 78 | +```js |
| 79 | +test.afterEach.always(() => User.remove()); |
| 80 | +``` |
| 81 | + |
| 82 | +Now you can use SuperTest to send off a request for your app endpoint. Use AVA for your assertions: |
| 83 | + |
| 84 | +```js |
| 85 | +// Note that the tests are run serially. See below as to why. |
| 86 | + |
| 87 | +test.serial('litmus get user', async t => { |
| 88 | + const {app} = t.context; |
| 89 | + const res = await request(app) |
| 90 | + .get('/litmus') |
| 91 | + . send({email : '[email protected]'}); |
| 92 | + t.is(res.status, 200); |
| 93 | + t.is(res.body.name, 'One'); |
| 94 | +}); |
| 95 | + |
| 96 | +test.serial('litmus create user', async t => { |
| 97 | + const {app} = t.context; |
| 98 | + const res = await request(app) |
| 99 | + .post('/litmus') |
| 100 | + .send({ |
| 101 | + |
| 102 | + name: 'New name' |
| 103 | + }); |
| 104 | + |
| 105 | + t.is(res.status, 200); |
| 106 | + t.is(res.body.name, 'New name'); |
| 107 | + |
| 108 | + // Verify that user is created in DB |
| 109 | + const newUser = await User. findOne({email : '[email protected]'}); |
| 110 | + t.is(newUser.name, 'New name'); |
| 111 | +}); |
| 112 | +``` |
| 113 | + |
| 114 | +Finally disconnect from and stop MongoDB when all tests are done: |
| 115 | + |
| 116 | +```js |
| 117 | +test.after.always(async () => { |
| 118 | + mongoose.disconnect() |
| 119 | + mongod.stop() |
| 120 | +}) |
| 121 | + |
| 122 | +``` |
| 123 | + |
| 124 | +And you're done! |
| 125 | + |
| 126 | +## Reusing the configuration across files |
| 127 | + |
| 128 | +You may choose to extract the code for the `test.before`, `test.beforeEach`, `test.afterEach.always` and `test.after.always` hooks into a separate file. Have a look at https://github.com/zellwk/ava-mdb-test for an example. |
| 129 | + |
| 130 | +## Using `test.serial` instead of `test` |
| 131 | + |
| 132 | +Your tests likely change the database. Using `test()` means they run concurrently, which may cause one test to affect another. Instead if you use `test.serial()` then the tests will run one at a time. You can then clean up your database between test runs, making the tests more predictable. |
| 133 | + |
| 134 | +You could run tests concurrently if you create separate Mongoose connections for each test. This is harder to set up, though. More information can be found [here](https://github.com/nodkz/mongodb-memory-server#several-mongoose-connections-simultaneously). |
0 commit comments