Jest preset for MongoDB in-memory server
Installations
npm install @shelf/jest-mongodb
Developer Guide
Typescript
No
Module System
N/A
Min. Node Version
>=16
Score
50.7
Supply Chain
95.1
Quality
84.5
Maintenance
100
Vulnerability
98.9
License
Releases
Contributors
Languages
TypeScript (86.73%)
JavaScript (7.4%)
Shell (5.87%)
Developer
shelfio
Download Statistics
Total Downloads
15,536,145
Last Day
1,470
Last Week
65,444
Last Month
280,712
Last Year
3,998,010
GitHub Statistics
595 Stars
847 Commits
84 Forks
27 Watching
13 Branches
78 Contributors
Package Meta Information
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
Total Downloads
Cumulative downloads
Total Downloads
15,536,145
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
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
jest-mongodb
Jest preset to run MongoDB memory server
Usage
0. Install
$ yarn add @shelf/jest-mongodb --dev
Make sure mongodb
is installed in the project as well, as it's required as a peer dependency.
1. Create 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.
2. Create 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};
3. Configure MongoDB client
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});
4. PROFIT! Write tests
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:*
5. Clean collections before each test (optional)
1beforeEach(async () => { 2 await db.collection('COLLECTION_NAME').deleteMany({}); 3});
See this issue for discussion
6. Jest watch mode gotcha
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};
See Also
Publish
1$ git checkout master 2$ yarn version 3$ yarn publish 4$ git push origin master --tags
License
MIT © Shelf
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
license file detected
Details
- Info: project has a license file: license:0
- Info: FSF or OSI recognized license: MIT License: license:0
Reason
0 existing vulnerabilities detected
Reason
SAST tool is not run on all commits -- score normalized to 5
Details
- Warn: 15 commits out of 28 are checked with a SAST tool
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
- Warn: no security policy file detected
- Warn: no security file to analyze
- Warn: no security file to analyze
- Warn: no security file to analyze
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
branch protection not enabled on development/release branches
Details
- Warn: branch protection not enabled for branch 'master'
Score
3.7
/10
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