skip to Main Content

I’ve setup the plugin cypress-mongodb for Cypress according to the documentation, and I think everything should work, because I am able to successfully use

cy.deleteMany({},{collection: 'colname', database: 'dbname'}) 

to clear the collection, but I have problem with adding documents back.

Here is a sample code I try to insert:

cy.insertOne({
                _id: "3c329243-3ff2-4c11-af09-a51ba33f9a18",
                application: {
                  lt: "A",
                  someDate: {
                    $date: "2023-09-19T00:00:00.000Z"
                  },
                  otherDate: {
                    $date: "2023-09-13T00:00:00.000Z"
                  }
                },
                ids: [],
                createdAt: {
                  $date: "2023-09-13T10:56:16.000Z"
                }
              }, {collection: 'colname', database: 'dbname'})

but fail with:

> Document failed validation Learn more
node_modules/cypress-mongodb/dist/commands/insert.js:22:1
  20 |     }
  21 |     args.document = (0, bson_1.serialize)(args.document);
> 22 |     return cy.task('insertOne', args).then(function (result) {
     | ^
  23 |         return result;
  24 |     });
  25 | } 

This document above is just an example, but when I try to add the same real document through MongoDB Compass it validates, and gets added without problems.

We use MongoDB 5.0.13 Community

Does anyone know what could be wrong?

2

Answers


  1. Chosen as BEST ANSWER

    Okay, I have found the problem and the solution.

    Since we use MongoDB the documents are saved as Extended JSON (EJSON) with specified field data types that are somewhat incompatible with JS format.

    The reason I was getting the error was DB side validation error due to sending JS object with EJSON structure:

    someDate : {$date: "2023-09-19T00:00:00.000Z"}
    

    Now, to correctly change the EJSON document (or array of docs) into JS object I had to use bson npm package and it's EJSON.deserialize() method like this:

    cy.fixture(jsonPath).then((json) => {
        cy.insertMany(EJSON.deserialize(json), {collection: 'col', database: 'db'})
    })
    

  2. There seems to be an update to the library in the code mentioned in the error message.

    Compare your error message to the latest source

    export function insertOne(document: Document, options?: any): Chainable {
      const args = {
        uri: Cypress.env('mongodb').uri,
        database: options?.database || Cypress.env('mongodb').database,
        collection: options?.collection || Cypress.env('mongodb').collection,
        options: options,
        document: document,
      };
    
      validate(args);
    
      if (!document) {
        throw new Error('Document must be specified');
      } else if (typeof document !== 'object' || Array.isArray(document)) {
        throw new Error('Document must be an object');
      }
    
      args.document = serialize(args.document);              // Updated
    
      return cy.task('insertOne', args).then((result: any) => {
        return result;
      });
    }
    

    You may find it goes away if you bring your install up to the latest version.

    Or at least get some more helpful error diagnostics.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search