How to compare mongoose model objects in jest while excluding _id

The schema model object in Mongoose provides an _id that is of type ObjectId. If you are not interested in comparing value of this attribute when you compare it compare objects in jest, you can exclude it by calling the toObject method of the mongoose …


This content originally appeared on DEV Community and was authored by Adrian Matei

The schema model object in Mongoose provides an _id that is of type ObjectId. If you are not interested in comparing value of this attribute when you compare it compare objects in jest, you can exclude it by calling the toObject method of the mongoose model and set the _id object to nothing via the spread operator, something similar to the following:

expect({...resultBookmark.toObject(), _id: {}}).toEqual({...expectedBookmark.toObject(), _id: {}})

The complete testing method with the setup is shown in the snippet bellow,
where expected bookmark model should match the mapped bookmark from the given request:

const showdown = require('showdown');
const Bookmark = require('../../model/bookmark');
const bookmarkRequestMapper = require('./bookmark-request.mapper');

jest.mock('showdown', () => {
  const makeHtml = jest.fn(() => '<p>This is a test bookmark</p>');
  return {
    Converter: jest.fn().mockImplementation(() => ({makeHtml})),
  };
});

describe('toBookmark', () => {
  const req = {
    body: {
      _id: '123',
      name: 'Test Bookmark',
      location: 'https://example.com',
      language: 'en',
      description: 'This is a test bookmark',
      tags: ['test'],
      public: true,
    },
    params: {
      userId: '456',
    },
  };

  beforeEach(() => {
    showdown.Converter.mockClear();
    showdown.Converter().makeHtml.mockClear();
    req.body.descriptionHtml = undefined;
    req.body.youtubeVideoId = undefined;
    req.body.stackoverflowQuestionId = undefined;
  });

  it('should use descriptionHtml if it is provided', () => {
    req.body.descriptionHtml = '<p>This is a test bookmark</p>';

    const expectedBookmark = new Bookmark({
      _id: '123',
      name: 'Test Bookmark',
      location: 'https://example.com',
      language: 'en',
      description: 'This is a test bookmark',
      descriptionHtml: '<p>This is a test bookmark</p>',
      tags: ['test'],
      public: true,
      userId: '456',
      likeCount: 0,
      youtubeVideoId: null,
      stackoverflowQuestionId: null,
    });

    const resultBookmark = bookmarkRequestMapper.toBookmark(req);

    expect({...resultBookmark.toObject(), _id: {}}).toEqual({...expectedBookmark.toObject(), _id: {}})
    expect(showdown.Converter().makeHtml).not.toHaveBeenCalled();
  });
});

Project: codever - File: bookmark-request.mapper.test.js

Shared with ❤️ from Codever. Use 👉 copy to mine functionality to add it to your personal snippets collection.

Codever is open source on Github⭐🙏


This content originally appeared on DEV Community and was authored by Adrian Matei


Print Share Comment Cite Upload Translate Updates
APA

Adrian Matei | Sciencx (2023-03-02T06:50:20+00:00) How to compare mongoose model objects in jest while excluding _id. Retrieved from https://www.scien.cx/2023/03/02/how-to-compare-mongoose-model-objects-in-jest-while-excluding-_id/

MLA
" » How to compare mongoose model objects in jest while excluding _id." Adrian Matei | Sciencx - Thursday March 2, 2023, https://www.scien.cx/2023/03/02/how-to-compare-mongoose-model-objects-in-jest-while-excluding-_id/
HARVARD
Adrian Matei | Sciencx Thursday March 2, 2023 » How to compare mongoose model objects in jest while excluding _id., viewed ,<https://www.scien.cx/2023/03/02/how-to-compare-mongoose-model-objects-in-jest-while-excluding-_id/>
VANCOUVER
Adrian Matei | Sciencx - » How to compare mongoose model objects in jest while excluding _id. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2023/03/02/how-to-compare-mongoose-model-objects-in-jest-while-excluding-_id/
CHICAGO
" » How to compare mongoose model objects in jest while excluding _id." Adrian Matei | Sciencx - Accessed . https://www.scien.cx/2023/03/02/how-to-compare-mongoose-model-objects-in-jest-while-excluding-_id/
IEEE
" » How to compare mongoose model objects in jest while excluding _id." Adrian Matei | Sciencx [Online]. Available: https://www.scien.cx/2023/03/02/how-to-compare-mongoose-model-objects-in-jest-while-excluding-_id/. [Accessed: ]
rf:citation
» How to compare mongoose model objects in jest while excluding _id | Adrian Matei | Sciencx | https://www.scien.cx/2023/03/02/how-to-compare-mongoose-model-objects-in-jest-while-excluding-_id/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.