Giter VIP home page Giter VIP logo

cypress-mongodb's Introduction

Introduction

Plugin that allows interaction with MongoDB server using Cypress commands.

Installation

run npm install cypress-mongodb
configure (see below)
profit

Supported and tested MongoDB versions

4.4, 5.0, 6.0

Usage

cy.createCollection('new_collection', {database: 'new_database'}); // creates both collection and database

const oneDocument = {document: 1};
cy.insertOne(oneDocument, {collection: 'some_collection', database: 'some_database'}).then(res => {
    cy.log(res); // prints the id of inserted document
});

const manyDocuments = [{document: 1}, {document: 2}];
cy.insertMany(manyDocuments, {collection: 'some_other_collection'}).then(res => { // defaults to database from env variable
    console.log(res); // prints the key-value pairs with inserted ids
});

const deleteClause = {document: 1};
cy.deleteOne(oneDocument, {collection: 'new_collection', database: 'some_database'}).then(res => {
    cy.log(res); // prints 1 (or 0) document deleted
});

cy.deleteMany(deleteClause).then(res => { // defaults to collection and database from env variables
    cy.log(res); // prints '# documents deleted'
});

const pipeline = []; // any kind of aggregation
cy.aggregate(pipeline).then(res => {
    cy.log(res); // prints the result of the aggregation
});

cy.dropCollection('start_new').then(res => {
    cy.log(res); // prints 'Collection dropped'
});

createCollection and dropCollection have the option to failSilently.

cy.createCollection('existing_collection', {failSilently: true}).then(res => {
    cy.log(res); // Error object if collection already exists
});

cy.dropCollection('nonexistent_collection', {failSilently: true}).then(res => {
    cy.log(res); // Error object if collection doesn’t exist
});

Environment setup

Add the following env properties in your cypress.config.js file:

{
    module.exports = defineConfig({
        "env": {
            "mongodb": {
                "uri": "mongodb://localhost:27017",
                "database": "database_name",
                "collection": "collection_name"
            }
        }
    });
}

Note: only mongodb.uri is mandatory, you can always override/set database and collection names in each cypress mongodb command using options. You can set both local and remote urls.

Plugin configuration - JavaScript

In your cypress.config.js add the following:

const mongo = require('cypress-mongodb');

module.exports = defineConfig({
    e2e: {
        setupNodeEvents(on, config) {
            mongo.configurePlugin(on);
        }
    }
});

In your cypress/support/e2e.js add the following:

const mongo = require('cypress-mongodb');
mongo.addCommands();

Plugin configuration - TypeScript

In your cypress.config.ts add the following:

import {defineConfig} from 'cypress'
import {configurePlugin} from 'cypress-mongodb';

/**
 * @type {Cypress.PluginConfig}
 */
export default defineConfig({
    e2e: {
        setupNodeEvents(on) {
            configurePlugin(on);
        }
    }
});

In your cypress/support/e2e.ts add the following:

import {addCommands} from "cypress-mongodb";

addCommands();

Future development & support

Please create feature requests for things you'd like to see.
Please raise issues for any problems you encounter.

cypress-mongodb's People

Contributors

zaista avatar ajhoddinott avatar edgerunner avatar pabloramirezdev avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.