dimanche 8 mars 2020

Jest toMatchObject returns expected = 0 but test fails

I'm new with using Jest and I'm trying to do a simple test.

I basically just want to see if my object has some specific subfields so I'm using the toMatchObject method, but my test doesn't want to pass even if my object are similar (you can see my console.log at the end of the console's display.

And I really don't get it. Like, Jest is telling me that it's expecting (expected - 0) and that it received more than expected too. But is it not the goal of the toMatchObject function ?

I think I miss something :c...

Well, thank you in advance for your help

This is my code :

import { UserExport } from "../src/model/user";
import { DatabaseManager } from "../src/db/databaseManager";


const dbm: Promise<DatabaseManager> = DatabaseManager.getDb()

const userTemplate: UserExport = ({
    _id: new Types.ObjectId("507f191e810c19729de860ea"),
    pseudo: "toto",
    email: "toto@etu.uca.fr",
    posts: []
} as unknown) as UserExport;

describe("Test User DB", () => {
    test("should return the doc with findById", async () => {
        let userModel: Model<UserExport> = (await dbm).getModel<UserExport>("users");
        mockingoose(userModel).toReturn(userTemplate, "findOne");
        debugger;
        const doc = await userModel.findById(new Types.ObjectId("507f191e810c19729de860ea"));
        console.log(doc, userTemplate);
        expect({...doc}).toMatchObject(userTemplate);
    });
});

And this is the output :

tests/user.test.ts (10.94s)
  Test User DB
    ✕ should return the doc with findById (107ms)

  ● Test User DB › should return the doc with findById

    expect(received).toMatchObject(expected)

    - Expected  -  0
    + Received  + 75

      Object {
    +   "$__": InternalCache {
    +     "$options": Object {},
    +     "$setCalled": Set {
    +       "_id",
    +       "pseudo",
    +       "email",
    +       "posts",
    +     },
          "_id": "507f191e810c19729de860ea",
    +     "activePaths": StateMachine {
    +       "forEach": [Function anonymous],
    +       "paths": Object {
    +         "_id": "modify",
    +         "email": "modify",
    +         "posts": "modify",
    +         "pseudo": "modify",
    +       },
    +       "stateNames": Array [
    +         "require",
    +         "modify",
    +         "init",
    +         "default",
    +         "ignore",
    +       ],
    +       "states": Object {
    +         "default": Object {},
    +         "ignore": Object {},
    +         "init": Object {},
    +         "modify": Object {
    +           "_id": true,
    +           "email": true,
    +           "posts": true,
    +           "pseudo": true,
    +         },
    +         "require": Object {},
    +       },
    +     },
    +     "adhocPaths": undefined,
    +     "cachedRequired": Object {},
    +     "emitter": EventEmitter {
    +       "_events": Object {},
    +       "_eventsCount": 0,
    +       "_maxListeners": 0,
    +     },
    +     "fullPath": undefined,
    +     "getters": Object {},
    +     "inserting": undefined,
    +     "ownerDocument": undefined,
    +     "pathsToScopes": Object {
    +       "posts": Object {
    +         "_id": "507f191e810c19729de860ea",
              "email": "toto@etu.uca.fr",
              "posts": Array [],
              "pseudo": "toto",
    +       },
    +     },
    +     "populate": undefined,
    +     "populated": undefined,
    +     "removing": undefined,
    +     "saveError": undefined,
    +     "saving": undefined,
    +     "scope": undefined,
    +     "selected": undefined,
    +     "session": null,
    +     "shardval": undefined,
    +     "strictMode": true,
    +     "validationError": undefined,
    +     "version": undefined,
    +     "wasPopulated": false,
    +   },
    +   "$locals": Object {},
    +   "_doc": Object {
    +     "_id": "507f191e810c19729de860ea",
    +     "email": "toto@etu.uca.fr",
    +     "posts": CoreMongooseArray [],
    +     "pseudo": "toto",
    +   },
    +   "errors": undefined,
    +   "isNew": true,
      }

      23 |         const doc = await userModel.findById(new Types.ObjectId("507f191e810c19729de860ea"));
      24 |         console.log(doc, userTemplate);
    > 25 |         expect({...doc}).toMatchObject(userTemplate);
         |                          ^
      26 |     });
      27 | });
      28 |

      at tests/user.test.ts:25:26
      at step (tests/user.test.ts:44:23)
      at Object.next (tests/user.test.ts:25:53)
      at fulfilled (tests/user.test.ts:16:58)

  console.log tests/user.test.ts:24
    {
      posts: [],
      _id: 507f191e810c19729de860ea,
      pseudo: 'toto',
      email: 'toto@etu.uca.fr'
    } {
      _id: 507f191e810c19729de860ea,
      pseudo: 'toto',
      email: 'toto@etu.uca.fr',
      posts: []
    }

Test Suites: 1 failed, 1 total
Tests:       1 failed, 1 total
Snapshots:   0 total
Time:        11.605s
Ran all test suites.
npm ERR! Test failed.  See above for more details.

Aucun commentaire:

Enregistrer un commentaire