{"id":13465125,"url":"https://github.com/shelfio/jest-mongodb","last_synced_at":"2025-05-14T04:00:33.583Z","repository":{"id":37431968,"uuid":"121576862","full_name":"shelfio/jest-mongodb","owner":"shelfio","description":"Jest preset for MongoDB in-memory server","archived":false,"fork":false,"pushed_at":"2025-04-24T00:43:51.000Z","size":559,"stargazers_count":599,"open_issues_count":48,"forks_count":85,"subscribers_count":25,"default_branch":"master","last_synced_at":"2025-04-25T11:42:20.451Z","etag":null,"topics":["jest","jest-environment","jest-preset","mongodb","nodejs","npm-package","tests"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/shelfio.png","metadata":{"files":{"readme":"readme.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"license","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2018-02-15T00:22:42.000Z","updated_at":"2025-04-08T03:19:31.000Z","dependencies_parsed_at":"2024-01-12T11:51:20.841Z","dependency_job_id":"32f34724-c49d-4196-aa49-4ac0496fec84","html_url":"https://github.com/shelfio/jest-mongodb","commit_stats":{"total_commits":620,"total_committers":31,"mean_commits":20.0,"dds":"0.44516129032258067","last_synced_commit":"382bd0de40a4d3e5cf072d8b2a0b7b68a4b8e5ae"},"previous_names":[],"tags_count":44,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shelfio%2Fjest-mongodb","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shelfio%2Fjest-mongodb/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shelfio%2Fjest-mongodb/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shelfio%2Fjest-mongodb/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/shelfio","download_url":"https://codeload.github.com/shelfio/jest-mongodb/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":251660894,"owners_count":21623334,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["jest","jest-environment","jest-preset","mongodb","nodejs","npm-package","tests"],"created_at":"2024-07-31T14:01:00.304Z","updated_at":"2025-05-14T04:00:33.389Z","avatar_url":"https://github.com/shelfio.png","language":"TypeScript","funding_links":[],"categories":["TypeScript","Packages"],"sub_categories":["Presets"],"readme":"# jest-mongodb [![CircleCI](https://circleci.com/gh/shelfio/jest-mongodb/tree/master.svg?style=svg)](https://circleci.com/gh/shelfio/jest-mongodb/tree/master) ![](https://img.shields.io/badge/code_style-prettier-ff69b4.svg) [![npm (scoped)](https://img.shields.io/npm/v/@shelf/jest-mongodb.svg)](https://www.npmjs.com/package/@shelf/jest-mongodb)\n\n\u003e Jest preset to run MongoDB memory server\n\n## Usage\n\n### 0. Install\n\n```\n$ yarn add @shelf/jest-mongodb --dev\n```\n\nMake sure `mongodb` is installed in the project as well, as it's required as a peer dependency.\n\n### 1. Create `jest.config.js`\n\n```js\nmodule.exports = {\n  preset: '@shelf/jest-mongodb',\n};\n```\n\nIf you have a custom `jest.config.js` make sure you remove `testEnvironment` property, otherwise it will conflict with the preset.\n\n### 2. Create `jest-mongodb-config.js`\n\nSee [mongodb-memory-server](https://github.com/nodkz/mongodb-memory-server#available-options)\n\n```js\nmodule.exports = {\n  mongodbMemoryServerOptions: {\n    binary: {\n      version: '4.0.3',\n      skipMD5: true,\n    },\n    autoStart: false,\n    instance: {},\n  },\n};\n```\n\nTo use the same database for all tests pass the config like this:\n\n```js\nmodule.exports = {\n  mongodbMemoryServerOptions: {\n    binary: {\n      version: '4.0.3',\n      skipMD5: true,\n    },\n    instance: {\n      dbName: 'jest',\n    },\n    autoStart: false,\n  },\n};\n```\n\nTo use separate database for each jest worker pass the `useSharedDBForAllJestWorkers: false` (doesn't create `process.env` variable when using this option):\n\n```js\nmodule.exports = {\n  mongodbMemoryServerOptions: {\n    binary: {\n      skipMD5: true,\n    },\n    autoStart: false,\n    instance: {},\n  },\n\n  useSharedDBForAllJestWorkers: false,\n};\n```\n\nTo use dynamic database name you must pass empty object for instance field:\n\n```js\nmodule.exports = {\n  mongodbMemoryServerOptions: {\n    binary: {\n      version: '4.0.3',\n      skipMD5: true,\n    },\n    instance: {},\n    autoStart: false,\n  },\n};\n```\n\nTo use another uri environment variable name you must set mongoURLEnvName field:\n\n```js\nmodule.exports = {\n  mongodbMemoryServerOptions: {\n    binary: {\n      version: '4.0.3',\n      skipMD5: true,\n    },\n    instance: {},\n    autoStart: false,\n  },\n  mongoURLEnvName: 'MONGODB_URI',\n};\n```\n\nTo use mongo as a replica set you must add the `replSet` config object and set\n`count` and `storageEngine` fields:\n\n```js\nmodule.exports = {\n  mongodbMemoryServerOptions: {\n    binary: {\n      skipMD5: true,\n    },\n    autoStart: false,\n    instance: {},\n    replSet: {\n      count: 3,\n      storageEngine: 'wiredTiger',\n    },\n  },\n};\n```\n\n### 3. Configure MongoDB client\n\nLibrary sets the `process.env.MONGO_URL` for your convenience, but using of `global.__MONGO_URI__` is preferable as it works with ` useSharedDBForAllJestWorkers: false`\n\n```js\nconst {MongoClient} = require('mongodb');\n\ndescribe('insert', () =\u003e {\n  let connection;\n  let db;\n\n  beforeAll(async () =\u003e {\n    connection = await MongoClient.connect(global.__MONGO_URI__, {\n      useNewUrlParser: true,\n      useUnifiedTopology: true,\n    });\n    db = await connection.db();\n  });\n\n  afterAll(async () =\u003e {\n    await connection.close();\n  });\n});\n```\n\n### 4. PROFIT! Write tests\n\n```js\nit('should insert a doc into collection', async () =\u003e {\n  const users = db.collection('users');\n\n  const mockUser = {_id: 'some-user-id', name: 'John'};\n  await users.insertOne(mockUser);\n\n  const insertedUser = await users.findOne({_id: 'some-user-id'});\n  expect(insertedUser).toEqual(mockUser);\n});\n```\n\nCache MongoDB binary in CI by putting this folder to the list of cached paths: `./node_modules/.cache/mongodb-memory-server/mongodb-binaries`\n\nYou can enable debug logs by setting environment variable `DEBUG=jest-mongodb:*`\n\n#### 5. Clean collections before each test (optional)\n\n```js\nbeforeEach(async () =\u003e {\n  await db.collection('COLLECTION_NAME').deleteMany({});\n});\n```\n\n\u003csub\u003eSee [this issue](https://github.com/shelfio/jest-mongodb/issues/173) for discussion\u003c/sub\u003e\n\n#### 6. Jest watch mode gotcha\n\nThis package creates the file `globalConfig.json` in the project root, when using jest `--watch` flag, changes to `globalConfig.json` can cause an infinite loop\n\nIn order to avoid this unwanted behaviour, add `globalConfig` to ignored files in watch mode in the Jest configuation\n\n```js\n// jest.config.js\nmodule.exports = {\n  watchPathIgnorePatterns: ['globalConfig'],\n};\n```\n\n## See Also\n\n- [jest-dynamodb](https://github.com/shelfio/jest-dynamodb)\n\n## Publish\n\n```sh\n$ git checkout master\n$ yarn version\n$ yarn publish\n$ git push origin master --tags\n```\n\n## License\n\nMIT © [Shelf](https://shelf.io)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fshelfio%2Fjest-mongodb","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fshelfio%2Fjest-mongodb","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fshelfio%2Fjest-mongodb/lists"}