-
Notifications
You must be signed in to change notification settings - Fork 42
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Example of using Mockgoose with AVA #3
Comments
I did try to achieve that and didn't manage to. in the end it comes down to the fact that mongoose itself can only be connected to one db at a time. so by reconnecting on beforeEach you end up removing the connection to the db while another test is maybe writing to it, which makes for some pretty confusing bugs with stack traces deep inside mongoose itself. the way to achieve this would be to have different mongoose instances for each test, which seeing as all tests in the same file run in the same process is not straightforward at all. it might be possible but I wouldn't bet on it. |
on further investigation, it should actually possible to do something like this, the awkward part will be getting a reference to the models inside each test import test from 'ava'
import mongoose from 'mongoose'
import mockgoose from 'mockgoose'
// i'm assuming this models file is a file that creates or imports all the mdoels you want to use in your test here
import models from '../../models/'
test.beforeEach(async t => {
// mongoose exposes the constructor for the mongoose object
const mongooseInstance = new mongoose.Mongoose
// the default export is simply an instance of that constructor
// mock this freshly created instance
await mockgoose(mongooseInstance)
// from my tests it works even if all instances "connect" to the same url
const url = `mongodb://2`
// connect this fresh instance
await mongooseInstance.connect(url)
// recreate the models on this new mongoose instance
Object.keys(mongoose.models).forEach(name => {
const model = mongoose.models[name]
mongooseInstance.model(name, model.schema)
})
// expose the mongoose instance to the test
t.context.mongoose = mongooseInstance
})
test('insert', async t => {
// each test must use its own mongoose instance and retrieve the models from there
const Task = t.context.mongoose.model('Task')
await new Task({
session_id: 'abc',
name: 'abc'
}).save()
const tasks = await Task.find({})
t.is(tasks.length, 1)
})
test('list', async t => {
// each test must use its own mongoose instance and retrieve the models from there
const Task = t.context.mongoose.model('Task')
// wait 2s to make sure the 'insert' test has inserted the model
await wait(2000)
const tasks = await Task.find({})
// the model inserted in the other test is not present in this one :)
t.is(tasks.length, 0)
})
function wait(timeout) {
return new Promise((resolve) => setTimeout(resolve, timeout))
} the gotcha here is that inside each test you really have to use that test's mongoose instance, so if you're trying to test a server that uses the models things might get trickier, but for directly testing the models it should work. |
This is pretty awesome but very hack really thanks I need to test a koa server (endpoint test) that will use these models I'll let u know if I figure it out how to handle this case |
I've decided to move to jest, and use snapshot test on my mongoose model tests it just works thanks |
I want to run tests in parallel that needs a mongo database using ava
My current problem is that I need a different database for each test as I don't want one test to influence into another
this is my SO question - http://stackoverflow.com/questions/39100051/how-to-run-parallel-tests-with-ava-and-mongoose
this is a good discussion as well - avajs/ava#472
Can I connect into different databases using mockgoose?
this is a sample code that I'm trying to use it
this is the error that I've got running the master of this repo:
The text was updated successfully, but these errors were encountered: