Zaista / cypress-mongodb

Cypress MongoDB plugin
MIT License
17 stars 8 forks source link

ObjectId type is not working #5

Closed joshmedeski closed 2 years ago

joshmedeski commented 2 years ago

Great library, the API is simple and I'm thankful for TypeScript support.

The Problem

I want to hard code the _id on an inserted user. When I pass the

import { ObjectId } from "MongoDB";

describe("login", () => {
  beforeEach(() => {
    cy.deleteMany({}, "users");
    const objectId = new ObjectId();

    const user = {
      _id: objectId,
      ...otherFields
    };

    cy.insertOne(user, "users").then((res) => {
      cy.log(JSON.stringify(res));
    });

When I run this, the objectId is inserted as a string on the mongoDb model (see Compass screenshot below).

Screen Shot 2022-03-10 at 1 07 23 PM

If I remove the _id field in the payload, the system is able to autogenerate a valid ObjectId on the mongo entry.

Screen Shot 2022-03-10 at 1 06 36 PM

What's going on? Why is the ObjectId object being converted to a string on the insert?

I looked into the forceServerObjectId on the insert options but haven't had any success figuring it out.

Zaista commented 2 years ago

Hey, thanks for posting the issue. I haven't had the time to fix it during the weekend, but I will as soon as I get a chance for it

joshmedeski commented 2 years ago

Thanks for the update, I'm not using this package anymore so this is not a high priority for me.