mongo-unit
Advanced tools
Comparing version 1.4.4 to 1.4.5
{ | ||
"name": "mongo-unit", | ||
"version": "1.4.4", | ||
"version": "1.4.5", | ||
"description": "mongo db for unit tests", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
207
README.md
# mongo-unit | ||
[![CircleCI](https://circleci.com/gh/mikhail-angelov/mongo-unit.svg?style=svg)](https://circleci.com/gh/mikhail-angelov/mongo-unit) | ||
This library is done to simplify creation of integration tests for node.js application with Mongo DB. | ||
I starts local mongodb process using [mongodb-prebuilt](https://github.com/winfinit/mongodb-prebuilt) library, | ||
and it work in "InMemory" mode, which improve performance of your tests. | ||
[![NPM](https://nodei.co/npm/mongo-unit.png)](https://nodei.co/npm/mongo-unit) | ||
### How to use it | ||
Since running embedded mongo takes some time, we need to config `mocha` to wait until mongo is up and running, the easiest way to do it add `--delay` flag for `mocha` and handle async flow, like this | ||
``` | ||
//package.json | ||
... | ||
"scripts": { | ||
... | ||
"test": "mocha ./**/*.spec.js --delay", | ||
... | ||
}, | ||
... | ||
``` | ||
add init.spec.js | ||
```javascript | ||
//init.spec.js | ||
const mongoUnit = require('mongo-unit') | ||
mongoUnit.start().then(url => { | ||
console.log('fake mongo is started: ', url) | ||
process.env.DATABASE_URL = url // this var process.env.DATABASE_URL = will keep link to fake mongo | ||
run() // this line start mocha tests | ||
}) | ||
after(() => { | ||
const dao = require('./dao') | ||
console.log('stop') | ||
dao.close() | ||
return mongoUnit.stop() | ||
}) | ||
``` | ||
Code under test | ||
```javascript | ||
//dao.js | ||
const mongoose = require('mongoose') | ||
const userSchema = new mongoose.Schema({ | ||
name: String, | ||
}) | ||
const taskSchema = new mongoose.Schema({ | ||
userId: String, | ||
task: String, | ||
}) | ||
module.exports = { | ||
init: () => mongoose.connect(process.env.DATABASE_URL), | ||
close: () => mongoose.disconnect(), | ||
User: mongoose.model('user', userSchema), | ||
Task: mongoose.model('task', taskSchema), | ||
} | ||
``` | ||
Test data | ||
```json | ||
{ | ||
"users": [ | ||
{ | ||
"_id": "56d9bf92f9be48771d6fe5b1", | ||
"name": "test" | ||
}, | ||
{ | ||
"_id": "56d9bf92f9be48771d6fe5b2", | ||
"name": "John" | ||
} | ||
], | ||
"tasks": [ | ||
{ | ||
"userId": "56d9bf92f9be48771d6fe5b1", | ||
"task": "do stuff" | ||
}, | ||
{ | ||
"userId": "56d9bf92f9be48771d6fe5b1", | ||
"task": "fix stuff" | ||
} | ||
] | ||
} | ||
``` | ||
Mocha test | ||
```javascript | ||
//dao.spec.js | ||
const {expect} = require('chai') | ||
const mongoUnit = require('mongo-unit') | ||
const testData = require('./data.json') | ||
const daoUT = require('./dao') | ||
describe('dao', ()=>{ | ||
before(() => daoUT.init()) | ||
beforeEach(() => mongoUnit.load(testData)) | ||
afterEach(() => mongoUnit.drop()) | ||
it('should find all users', () => { | ||
return daoUT.User.find() | ||
.then(users => { | ||
expect(users.length).to.equal(2) | ||
expect(users[0].name).to.equal('test') | ||
}) | ||
}) | ||
it('should find all tasks for user 1', () => { | ||
return daoUT.User.find() | ||
.then(users => users[0]) | ||
.then(user=>daoUT.Task.find({userId: user._id})) | ||
.then(tasks => { | ||
expect(tasks.length).to.equal(2) | ||
expect(tasks[0].task).to.equal('do stuff') | ||
}) | ||
}) | ||
}) | ||
``` | ||
## General info | ||
[![CircleCI](https://circleci.com/gh/mikhail-angelov/mongo-unit.svg?style=svg)](https://circleci.com/gh/mikhail-angelov/mongo-unit) | ||
I was inspired by [dbUnit](http://dbunit.sourceforge.net) library, which is very popular in java world. | ||
This library is done to simplify creation of integration tests for node.js application with Mongo DB. | ||
I starts local mongodb process using [mongodb-prebuilt](https://github.com/winfinit/mongodb-prebuilt) library, | ||
and it work in "InMemory" mode, which improve performance of your tests. | ||
> There is alternative library for mocking Mongo: [mockgoose](https://github.com/mockgoose/mockgoose) | ||
## Requirements | ||
It works on Node.js 4+ (ES2015) | ||
It works on Node.js 8+ | ||
@@ -66,83 +178,2 @@ ## Installation | ||
### Basic Usage | ||
Code under test | ||
```javascript | ||
const mongoose = require('mongoose') | ||
const Schema = mongoose.Schema | ||
function dao(url) { | ||
mongoose.connect(url) | ||
const userSchema = new Schema({ | ||
name: String | ||
}) | ||
const taskSchema = new Schema({ | ||
userId: String, | ||
task: String | ||
}) | ||
return { | ||
User: mongoose.model('user', userSchema), | ||
Task: mongoose.model('task', taskSchema) | ||
} | ||
} | ||
``` | ||
Test data | ||
```json | ||
{ | ||
"users": [ | ||
{ | ||
"_id": "56d9bf92f9be48771d6fe5b1", | ||
"name": "test" | ||
}, | ||
{ | ||
"_id": "56d9bf92f9be48771d6fe5b2", | ||
"name": "John" | ||
} | ||
], | ||
"tasks": [ | ||
{ | ||
"userId": "56d9bf92f9be48771d6fe5b1", | ||
"task": "do stuff" | ||
}, | ||
{ | ||
"userId": "56d9bf92f9be48771d6fe5b1", | ||
"task": "fix stuff" | ||
} | ||
] | ||
} | ||
``` | ||
Mocha test | ||
```javascript | ||
describe('dao', ()=>{ | ||
const mongoUnit = require('../index') | ||
const testData = require('./fixtures/basic.json') | ||
var daoUT | ||
before(() => mongoUnit.start() | ||
.then(url=>daoUT=dao(url)) | ||
.then(()=>mongoUnit.load(testData))) | ||
after(() => mongoUnit.drop()) | ||
it('should find all users', () => { | ||
return daoUT.User.find() | ||
.then(users => { | ||
expect(users.length).to.equal(2) | ||
expect(users[0].name).to.equal('test') | ||
}) | ||
}) | ||
it('should find all tasks for user 1', () => { | ||
return daoUT.User.find() | ||
.then(users => users[0]) | ||
.then(user=>daoUT.Task.find({userId: user._id})) | ||
.then(tasks => { | ||
expect(tasks.length).to.equal(2) | ||
expect(tasks[0].task).to.equal('do stuff') | ||
}) | ||
}) | ||
}) | ||
``` |
12935
179