Registry / testing / cypress-mongodb

cypress-mongodb

JSON →
library7.0.0jsnpmunverified

A Cypress plugin that provides custom commands (createCollection, dropCollection, insertOne, insertMany, findOne, findMany, updateOne, updateMany, deleteOne, deleteMany, aggregate) for interacting with a MongoDB server during end-to-end tests. Current stable version is 7.0.0, released on a periodic cadence. It wraps the MongoDB Node.js driver v7 and supports MongoDB 7-8 and Node 20-24. Key differentiators: it works with both local and remote MongoDB instances, allows per-command overrides for database/collection, and offers a failSilently option to avoid failing tests on expected errors. TypeScript types are included. A known issue is that if your project uses the 'mongodb' package, it must be version 7.x to avoid Webpack compilation errors.

npm install cypress-mongodb
INSTALL
IMPORT
SIG · CYPRESS-MONGODB
C
cypress-mongodb
testingjavascriptv7.0.0
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

configurePlugin
import { configurePlugin } from 'cypress-mongodb'
const { configurePlugin } = require('cypress-mongodb')
Use ESM import in cypress.config.js as shown in documentation. CommonJS require may work but official docs use import.
addCommands
import { addCommands } from 'cypress-mongodb/dist/index-browser'
import { addCommands } from 'cypress-mongodb'
Must import from 'cypress-mongodb/dist/index-browser' for browser context (cypress/support/e2e.js). The main entry point is for Node.js (e2e setup).
default (addCommands)
import { addCommands } from 'cypress-mongodb/dist/index-browser'; addCommands();
import { addCommands } from 'cypress-mongodb'
Make sure to call addCommands() after import. No default export; named export only.

Complete setup of cypress-mongodb in Cypress, including configuration and a sample test that inserts and finds a document.

// 1. Install: npm install cypress-mongodb // 2. In cypress.config.js: import { configurePlugin } from 'cypress-mongodb'; module.exports = defineConfig({ env: { mongodb: { uri: process.env.MONGO_URI || 'mongodb://localhost:27017', database: 'testdb' } }, e2e: { setupNodeEvents(on, config) { configurePlugin(on); } } }); // 3. In cypress/support/e2e.js: import { addCommands } from 'cypress-mongodb/dist/index-browser'; addCommands(); // 4. In a test: describe('MongoDB test', () => { it('inserts and finds a document', () => { cy.insertOne({ name: 'Alice' }); cy.findOne({ name: 'Alice' }).then(doc => { expect(doc).to.have.property('name', 'Alice'); }); }); });
Debug
Known issues
breakingVersion 7.x of cypress-mongodb requires mongodb package version 7.x as a peer dependency. If you have mongodb v6, you will get a Webpack compilation error.
fix
Install mongodb@7.0.0 or later: npm install mongodb@7.0.0
affects: >=7.0.0
gotchaaddCommands must be imported from 'cypress-mongodb/dist/index-browser', not from the main entry point. Using the wrong path will result in an error when Cypress tries to load the browser bundle.
fix
Use import { addCommands } from 'cypress-mongodb/dist/index-browser';
affects: >=2.0.0
deprecatedOptions object for commands used to accept failSilently as a string 'true'/'false' (see older examples). Now it should be a boolean.
fix
Pass failSilently: true (boolean) instead of failSilently: 'true' (string).
affects: >=5.0.0
gotchaIf you set mongodb.database in env, but then use an options object with a different database name, the options database takes precedence. This can cause confusion if you expect the env database to always apply.
fix
Be explicit: either always use env database by not passing options, or always pass options with expected database.
affects: >=2.0.0
Errors
Common errors & fixes
Module parse failed: Unexpected token (1:8) - You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file.
Using incompatible version of mongodb (v6) with cypress-mongodb v7.
fix
Upgrade mongodb to >=7.0.0: npm install mongodb@7.0.0
addCommands is not a function
Imported addCommands from the wrong path or using default export incorrectly.
fix
Import { addCommands } from 'cypress-mongodb/dist/index-browser' and call addCommands().
TypeError: Cannot read properties of undefined (reading 'collection')
Missing env.mongodb.uri or database not set and no database option provided to a command.
fix
Set mongodb.uri in cypress.config.js env and provide database either in env or in command options.
Upgrade
Version history
7.0.0latest on npm
Audit
Dependencies
mongodboptionalPeer dependency for connecting to MongoDB. Must be version 7.x.
Agent activity
12 hits · last 30 days
node
10
Resources
cypress-mongodb — npm install cypress-mongodb · libregistry