jest-mongodb
jest-mongodb copied to clipboard
[Question] Avoid downloading MongDB for every package
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?
Use mongodb-memory-server-global instead, it should fix for you
How would that work, @sibelius? jest-mongodb directly depends on mongodb-memory-server (non global), how can this be parametrized?
you should open an issue there
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-serverfrom this package as optional orpeerDependency, 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:
-
In my project’s
package.json, add the followingconfigto disable downloading MongoDB binaries:"config": { "mongodbMemoryServer": { "disablePostinstall": "1" } } -
Add
mongodb-memory-server-globalto the project:yarn add mongodb-memory-server-global -D -
Add a
postinstallhook which in turn calls the hook frommongodb-memory-server-global, but before, set environment variableMONGOMS_DISABLE_POSTINSTALL=false(this in turn overrides the setting from thepackage.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-mongodbnow, and having the downloaded binary in thenode_moduleswastes disk space and download time -
Yarn seems to clean the
node_modulesdirectory rather aggressively (see here), thus the.cachegets deleted frequently. By having it in a global location (i.e.~/.cache) instead, this is avoided
I am having issues as well with different versions of the package. Can mongodb-memory-server be set as peerDependency?