Gathering detailed insights and metrics for @shelf/jest-mongodb
Gathering detailed insights and metrics for @shelf/jest-mongodb
Gathering detailed insights and metrics for @shelf/jest-mongodb
Gathering detailed insights and metrics for @shelf/jest-mongodb
npm install @shelf/jest-mongodb
Typescript
Module System
Min. Node Version
Node Version
NPM Version
TypeScript (86.73%)
JavaScript (7.92%)
Shell (5.35%)
Total Downloads
0
Last Day
0
Last Week
0
Last Month
0
Last Year
0
MIT License
601 Stars
927 Commits
86 Forks
25 Watchers
8 Branches
78 Contributors
Updated on Jul 04, 2025
Latest Version
5.2.2
Package Id
@shelf/jest-mongodb@5.2.2
Unpacked Size
15.18 kB
Size
5.41 kB
File Count
16
NPM Version
10.9.0
Node Version
22.12.0
Published on
May 17, 2025
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
2
2
Jest preset to run MongoDB memory server
$ yarn add @shelf/jest-mongodb --dev
Make sure mongodb
is installed in the project as well, as it's required as a peer dependency.
jest.config.js
1module.exports = { 2 preset: '@shelf/jest-mongodb', 3};
If you have a custom jest.config.js
make sure you remove testEnvironment
property, otherwise it will conflict with the preset.
jest-mongodb-config.js
1module.exports = { 2 mongodbMemoryServerOptions: { 3 binary: { 4 version: '4.0.3', 5 skipMD5: true, 6 }, 7 autoStart: false, 8 instance: {}, 9 }, 10};
To use the same database for all tests pass the config like this:
1module.exports = { 2 mongodbMemoryServerOptions: { 3 binary: { 4 version: '4.0.3', 5 skipMD5: true, 6 }, 7 instance: { 8 dbName: 'jest', 9 }, 10 autoStart: false, 11 }, 12};
To use separate database for each jest worker pass the useSharedDBForAllJestWorkers: false
(doesn't create process.env
variable when using this option):
1module.exports = { 2 mongodbMemoryServerOptions: { 3 binary: { 4 skipMD5: true, 5 }, 6 autoStart: false, 7 instance: {}, 8 }, 9 10 useSharedDBForAllJestWorkers: false, 11};
To use dynamic database name you must pass empty object for instance field:
1module.exports = { 2 mongodbMemoryServerOptions: { 3 binary: { 4 version: '4.0.3', 5 skipMD5: true, 6 }, 7 instance: {}, 8 autoStart: false, 9 }, 10};
To use another uri environment variable name you must set mongoURLEnvName field:
1module.exports = { 2 mongodbMemoryServerOptions: { 3 binary: { 4 version: '4.0.3', 5 skipMD5: true, 6 }, 7 instance: {}, 8 autoStart: false, 9 }, 10 mongoURLEnvName: 'MONGODB_URI', 11};
To use mongo as a replica set you must add the replSet
config object and set
count
and storageEngine
fields:
1module.exports = { 2 mongodbMemoryServerOptions: { 3 binary: { 4 skipMD5: true, 5 }, 6 autoStart: false, 7 instance: {}, 8 replSet: { 9 count: 3, 10 storageEngine: 'wiredTiger', 11 }, 12 }, 13};
Library sets the process.env.MONGO_URL
for your convenience, but using of global.__MONGO_URI__
is preferable as it works with useSharedDBForAllJestWorkers: false
1const {MongoClient} = require('mongodb'); 2 3describe('insert', () => { 4 let connection; 5 let db; 6 7 beforeAll(async () => { 8 connection = await MongoClient.connect(global.__MONGO_URI__, { 9 useNewUrlParser: true, 10 useUnifiedTopology: true, 11 }); 12 db = await connection.db(); 13 }); 14 15 afterAll(async () => { 16 await connection.close(); 17 }); 18});
1it('should insert a doc into collection', async () => { 2 const users = db.collection('users'); 3 4 const mockUser = {_id: 'some-user-id', name: 'John'}; 5 await users.insertOne(mockUser); 6 7 const insertedUser = await users.findOne({_id: 'some-user-id'}); 8 expect(insertedUser).toEqual(mockUser); 9});
Cache MongoDB binary in CI by putting this folder to the list of cached paths: ./node_modules/.cache/mongodb-memory-server/mongodb-binaries
You can enable debug logs by setting environment variable DEBUG=jest-mongodb:*
1beforeEach(async () => { 2 await db.collection('COLLECTION_NAME').deleteMany({}); 3});
See this issue for discussion
This package creates the file globalConfig.json
in the project root, when using jest --watch
flag, changes to globalConfig.json
can cause an infinite loop
In order to avoid this unwanted behaviour, add globalConfig
to ignored files in watch mode in the Jest configuation
1// jest.config.js 2module.exports = { 3 watchPathIgnorePatterns: ['globalConfig'], 4};
1$ git checkout master 2$ pnpm version 3$ pnpm publish 4$ git push origin master --tags
MIT © Shelf
No vulnerabilities found.
Reason
30 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 10
Reason
no binaries found in the repo
Reason
0 existing vulnerabilities detected
Reason
license file detected
Details
Reason
SAST tool is run on all commits
Details
Reason
Found 0/15 approved changesets -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Score
Last Scanned on 2025-06-30
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More