samedi 8 août 2020

Mongoose Schema object equality test failing in Jest

I created 2 Mongoose schema like so

const schemaA = new mongoose.Schema({
  type: {
    type: mongoose.Schema.Types.String,
    enum: ["string", "number"],
  },
 });

const schemaB = new mongoose.Schema({
  type: {
    type: mongoose.Schema.Types.String,
    enum: ["string", "number"],
  },
});

then in Jest test I compared their paths

expect(schemaA.paths).toMatchObject(schemaB.paths);

For some odd reason I am getting this error

expect(received).toMatchObject(expected)

- Expected  - 2
+ Received  + 1

  Object {
    "_id": f {
      "_index": null,
      "defaultValue": [Function h],
      "getters": Array [],
      "instance": "ObjectID",
      "options": r {
        "auto": true,
        "type": "ObjectId",
      },
      "path": "_id",
      "setters": Array [
        [Function y],
      ],
      "validators": Array [],
      Symbol(mongoose#schemaType): true,
    },
-   "type": h {
+   "type": Object {
      "_index": null,
      "enumValidator": [Function anonymous],
      "enumValues": Array [
        "string",
        "number",
      ],
      "getters": Array [],
      "instance": "String",
      "options": r {
        "enum": Array [
          "string",
          "number",
        ],
        "type": [Function h],
      },
      "path": "type",
      "regExp": null,
      "setters": Array [],
      "validators": Array [
        Object {
          "enumValues": Array [
            "string",
            "number",
          ],
          "message": "`{VALUE}` is not a valid enum value for path `{PATH}`.",
          "type": "enum",
          "validator": [Function anonymous],
        },
      ],
-     Symbol(mongoose#schemaType): true,
    },
  }

Oddly enough, the JSON.stringify() value passes the test.

// Passes!
expect(JSON.stringify(schemaA.paths)).toEqual(JSON.stringify(schemaB.paths)

Please check the reproduction here:

https://codesandbox.io/s/musing-mendel-w7mw4?file=/src/schema.spec.js

https://github.com/webberwang/mongoose-schema-jest/blob/master/src/schema.spec.js

Aucun commentaire:

Enregistrer un commentaire