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
Jest preset for MongoDB in-memory server
npm install @shelf/jest-mongodb
Typescript
Module System
Min. Node Version
50.7
Supply Chain
95.1
Quality
84.5
Maintenance
100
Vulnerability
98.9
License
TypeScript (86.73%)
JavaScript (7.4%)
Shell (5.87%)
Total Downloads
15,536,145
Last Day
1,470
Last Week
65,444
Last Month
280,712
Last Year
3,998,010
595 Stars
847 Commits
84 Forks
27 Watching
13 Branches
78 Contributors
Latest Version
4.3.2
Package Id
@shelf/jest-mongodb@4.3.2
Unpacked Size
14.65 kB
Size
5.61 kB
File Count
21
Publised On
09 May 2024
Cumulative downloads
Total Downloads
Last day
-44.9%
1,470
Compared to previous day
Last week
-12.2%
65,444
Compared to previous week
Last month
-13.4%
280,712
Compared to previous month
Last year
1.8%
3,998,010
Compared to previous year
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$ yarn version 3$ yarn publish 4$ git push origin master --tags
MIT © Shelf
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
0 existing vulnerabilities detected
Reason
SAST tool is not run on all commits -- score normalized to 5
Details
Reason
Found 2/18 approved changesets -- score normalized to 1
Reason
2 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 1
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 2024-12-16
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