Search code examples
node.jsmongodblerna

mongodb-memory-server not downloading MongoDB on lerna bootstrap


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:

  1. I understand the MongoDB is downloaded by 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.
  2. Given point 1, I suspect that when tests get executed, they search for that cache in the package 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.


Solution

  • 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',
        ),
    })