jest-mongodb icon indicating copy to clipboard operation
jest-mongodb copied to clipboard

[Question] Avoid downloading MongDB for every package

Open vinyoliver opened this issue 5 years ago • 5 comments

Hi,

I'm facing the following problem, I've got several microservices structured in a way that every microservice is a project. Every microservice inherits from the base package which holds the commons dependencies used for every project. One of these dependencies is jest-mongodb and mongodb for unit testing;

The problem I am facing is that every microservice is downloading a mongoDB instead of downloading just once for the base package.json where it's defined. Any suggestion on how to solve this?

vinyoliver avatar Dec 14 '19 21:12 vinyoliver

Use mongodb-memory-server-global instead, it should fix for you

sibelius avatar Jun 08 '20 13:06 sibelius

How would that work, @sibelius? jest-mongodb directly depends on mongodb-memory-server (non global), how can this be parametrized?

qqilihq avatar Apr 12 '21 08:04 qqilihq

you should open an issue there

sibelius avatar Apr 12 '21 11:04 sibelius

Thanks, @sibelius. But to clarify: The suggestion above (i.e. simply adding mongodb-memory-server-global to my project which uses jest-mongodb) will not fix this.

mongodb-memory-server already provides three separate packages:

  • mongodb-memory-server -- “Auto-downloads version […] mongod binary on npm install to: node_modules/.cache/mongodb-binaries.”
  • mongodb-memory-server-global -- “Auto-downloads […] mongod binary on npm install to: %HOME%/.cache/mongodb-binaries / ~/.cache/mongodb-binaries.”
  • mongodb-memory-server-core -- “Does NOT auto-download mongod on npm install.”

Suggestion

jest-mongodb should ideally have a mechanism to define on which of these three to depend (instead of forcing mongodb-memory-server).

  • A quick idea would be to declare the dependency to mongodb-memory-server from this package as optional or peerDependency, and let the user decide on which three options to depend (by explicitly pulling in the desired one). Would you be open to a MR in that direction, @vladgolubev ?

  • Another idea would be to have also three variants of this package (same as the mongodb-memory-server), but this would probably lead to too much maintenance overhead?

  • Any further ideas? 🙂

Workaround

In fact, @sibelius suggestion was a nudge into the right direction, and I was at least able to come up with a rather crude hack:

  1. In my project’s package.json, add the following config to disable downloading MongoDB binaries:

    "config": {
      "mongodbMemoryServer": {
        "disablePostinstall": "1"
      }
    }
    
  2. Add mongodb-memory-server-global to the project: yarn add mongodb-memory-server-global -D

  3. Add a postinstall hook which in turn calls the hook from mongodb-memory-server-global, but before, set environment variable MONGOMS_DISABLE_POSTINSTALL=false (this in turn overrides the setting from the package.json)

    "scripts": {
      "postinstall": "MONGOMS_DISABLE_POSTINSTALL=false node ./node_modules/mongodb-memory-server-global/postinstall.js"
    }
    

Why?

So, again, why this hassle?

  • We have many projects which use jest-mongodb now, and having the downloaded binary in the node_modules wastes disk space and download time

  • Yarn seems to clean the node_modules directory rather aggressively (see here), thus the .cache gets deleted frequently. By having it in a global location (i.e. ~/.cache) instead, this is avoided

qqilihq avatar Apr 12 '21 12:04 qqilihq

I am having issues as well with different versions of the package. Can mongodb-memory-server be set as peerDependency?

SalvatorePreviti avatar Feb 10 '23 00:02 SalvatorePreviti