I have a Lerna monorepo and some of our integration tests in one of the packages is using mongodb-memory-server
.
Issue is that when I run all tests from the root, mongodb-memory-server
starts downloading MongoDB and tests using it of course time out. Then the whole test process terminates by timeout before the MongoDB is fully downloaded, so next time it will start download it again.
A couple of points:
mongodb-memory-server
at dep install time. That is, in my case, when I run lerna bootstrap
. And I am not using mongodb-memory-server-core
, but directly mongodb-memory-server
. Given that I see the mongodb-memory-server
cache in the root node_modules
, that should be working as expected.node_modules
and not in the root. Why so? According to this comment I understand it should be searching it in the root node_modules
instead.Here my global test setup, simplified
const mongod = new MongoMemoryServer()
beforeAll(async done => {
const uri = await mongod.getUri()
await mongoose.connect(uri, {
// ... some opts
})
done()
})
afterAll(async done => {
await mongoose.connection.dropDatabase()
await mongoose.connection.close()
await mongod.stop()
done()
})
Here is my lerna config, simplified
{
"packages": ["packages/*"],
"command": {
"bootstrap": {
"hoist": true,
"strict": true
}
},
"version": "independent"
}
Did I understand things right? Or otherwise, what should be the whole flow here?
Should you need more info, just let me know.
EDIT: better express my doubts and rephrased question.
I confirm the flow is the one described. The missing piece here was the usage of mongodb-memory-server
env variable in jest.config.js
file
process.env = Object.assign(process.env, {
MONGOMS_SYSTEM_BINARY: path.join(
'..',
'..',
'node_modules',
'.cache',
'mongodb-memory-server',
'mongodb-binaries',
'*', // MongoDB version is expanded
'mongod.exe',
),
})