DEV Community

Cover image for How to verify an object is empty object with Jest (extended)
Adrian Matei for Codever

Posted on • Originally published at codever.dev

How to verify an object is empty object with Jest (extended)

First install jest-extended with the npm install --save-dev jest-extended command and use the toBeEmpty assertion as in the following example:

const { toBeEmpty } = require('jest-extended');
expect.extend({ toBeEmpty });

describe('setFulltextSearchTermsFilter', () => {
  test('returns filter without $text when fulltextSearchTerms is empty', () => {
    const fulltextSearchTerms = [];
    const filter = {};
    const searchInclude = 'any';
    expect(searchUtils.setFulltextSearchTermsFilter(fulltextSearchTerms, filter, searchInclude)).toBeEmpty();
  });
});
Enter fullscreen mode Exit fullscreen mode

Use .toBeEmpty when checking if a String '', Array [], Object {}, or Iterable is empty. Because toBeEmpty supports checking for emptiness of Iterables, you can use it to check whether a Map, or Set is empty, as well as checking that a generator yields no values.


Project: codever - File: search.utils.spec.js



Reference -

https://jest-extended.jestcommunity.dev/docs/matchers/tobeempty


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

Codever is open source on Github⭐🙏

Top comments (0)