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.
Finally, in your cypress/support/e2e.js add the following:
cy.createCollection('someCollection');// collection with name `someCollection` will be createdcy.createCollection('someOtherCollection',{database: 'someDatabase',failSilently: 'true'}).then(result=>{cy.log(result);// Will return 'Collection created' or the error object if collection already exists. Will not fail the test});cy.dropCollection('someCollection');// collection will be dropedcy.dropCollection('nonexistentCollection',{database: 'someDatabase',failSilently: 'true'}).then(result=>{cy.log(result);// Will return 'Collection dropped' or the error object if collection doesn’t exist. Will not fail the test});
An array of Document objects that will be inserted
options
Object (optional)
Provide additional options (see below)
> examples
cy.insertOne({document: 1});// will insert the provided document in mongodbcy.insertOne({document: 1},{collection: 'someCollection',database: 'someDatabase'}).then(result=>{cy.log(result);// prints the _id of inserted document});cy.insertMany([{document: 1},{document: 2}]);// will insert provided documents in mongodbcy.insertMany([{document: 1},{document: 2}],{collection: 'some_other_collection'}).then(result=>{console.log(result);// prints the key-value pairs of the inserted ids});
Specifies query selection criteria using query operators
filter
Object (required)
The selection criteria for the deletion
options
Object (optional)
Provide additional options (see below)
> examples
import{ObjectId}from'mongodb';cy.findOne({_id: newObjectId()}).then(result=>{cy.log(result);// prints the document with the _id if found, otherwise null});cy.findMany({document: 1}).then(result=>{cy.log(result);// prints the array of documents if any matched, or empty array});cy.findOneAndUpdate({document: 2},{$set: {document: 3}}).then(result=>{cy.log(result);// prints the original document with value 2});cy.findOneAndUpdate({document: 3},{$set: {document: 4}},{upsert: true,returnDocument: 'after'}).then((result: any)=>{cy.log(result);// prints the updated document with the value 4, will create (upsert) a new document if none are found});
cy.deleteOne({document: 1});// will delete a first matched documentcy.deleteOne({document: 1},{collection: 'new_collection',database: 'some_database'}).then(result=>{cy.log(result);// prints 1 (or 0) document deleted});cy.deleteMany(deleteClause).then(res=>{cy.log(result);// prints '# documents deleted'});
A document representing the mongodb command to run
options
Object (optional)
Provide additional options (see below)
> examples
constcommand={listCollections: 1,nameOnly: true};// any kind of commandcy.runCommand(command).then(result=>{cy.log(result);// prints the result of the command});
> available options
Options
Default
Description
database
Value specified in the mongodb environment variable
Database on top of which the command will be executed
collection
Value specified in the mongodb environment variable
Collection on top of which the command will be executed
failSilently
false
Control if the command will fail or if the collection is not found
As our application touches 2-3 databases while writing e2e tests, currently the feasibility does not exists for this plugin. Please enable a connect/disconnect method which will be useful in connecting and disconnecting the databases whenever required.
When attempting to use deleteOne with _id it does not work for me but when I use other key/values I can get it to delete. I dont know what I am doing wrong. I can use another mongo package and delete by _id and new ObjectId('634d732c5bcba068900dfc94'). I am using import { ObjectId } from 'mongodb';
I'm trying to use that with cucumber preprocessor but it doesn't recognize the commands
and in terminal
i have this in my e2e config file
e2e: {
reporter: require.resolve("@badeball/cypress-cucumber-preprocessor/pretty-reporter"),
// We've imported your old cypress plugins here.
// You may want to clean this up later by importing these.
setupNodeEvents( cypressOn, config ) {
const on = require('cypress-on-fix')(cypressOn);
// This is required for the preprocessor to be able to generate JSON reports after each run, and more,
addCucumberPreprocessorPlugin(on, config);
on(
"file:preprocessor",
createBundler({
plugins: [createEsbuildPlugin(config)],
})
);
configurePlugin(on)
return config;
}
It would be great if there was an ability to add { "upsert": true } to the options for updateOne so that I could insert a new record when one does not exist.
Currently does not return changed or deleted data from a collection in the result
In version 5.2 of mongo, it is possible to use the function findOneAndUpdate() or findOneAndDelete passing as option returnDocument 'after' or 'before' to return the changed or deleted result.
It's cool any mongoDB command has the option to return the result in the console.
Note: I noticed that apparently there is a conflict when updating to a new version of mongoDB
It doesn't seem possible to use a filter in either the findOne or updateOne methods. The only method I can get to work is insertOne with all the same values above only without the filter.
Hi! I'm working on a cypress & typescript project. I have installed cypress-mongodb and it's great.
However, I struggled to find references on how to do things properly (examples of good practices).
For instance, I ended up doing something like this:
Do you think this approach is correct?
Is there any better option than typing result as any?
Do you know if I am supposed to use then/should and that list of expects inside?
I got that working after hours of searching, not founding documentation, and trial and error.
P.S.: I'd love to start writing some docs once I have a clear idea of what I'm doing.
First up, thanks for the super useful package, it has helped a lot!
Kind of a silly question (please let me know if this is not the right place to ask).
For a document to be added:
const document = {
text: 'whatever'
createdAt: new Date()
}
From my normal application (a Meteor application), inserting this results in a createdAt field that is of a Date type. However, doing the same within cypress:
cy.insertOne({
text: 'whatever'
createdAt: new Date()
});
... leads to a document where the createdAt field is of type string.
Is there something straightforward that I am missing? I have tried looking around but all documentation and queries regarding this seem to indicate that mongo should be automatically defaulting to an object type.
I'm using the Insert One method to insert data into my db, however it always returns my data as an object plus with an default set like the following:
-object()
--data
-default
---object
----data
I am unclear as to why its seeding my data in this form but is there anyway to just store it without either objects or defaults and just the data itself??