npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

jest-contain-object

v1.0.0

Published

A jest extension to make object comparisons easier

Downloads

3

Readme

jest-contain-object

A jest extension to make object comparisons easier by checking to see if an object is a subset of another object.

Getting started

Add the following to your src/setupTests.js or equivalent:

import toContainObject from "jest-contain-object";

expect.extend({
  toContainObject
});

To use, simply use the following syntax in your tests:

expect(something).toContainObject(somethingElse);

Why jest-contain-object?

In testing React components, I frequently ran into issues when looking to ensure that props would contain desired elements. For instance, let's say I have the following component:

const MyComponent = ({ onClick, children }) => (
  <div className="my-component" style={{ margin: "auto" }} onClick={onClick}>
    {children}
  </div>
);

When I want to test the component above, I would typically do two things:

  1. Check that the div is always being rendered
  2. Ensure that the props are passed properly

I don't really care about testing the className or style props since these are hardcoded/not a part of the component's external API. The issue however, is that the following test (written using Enzyme and Jest) will fail:

const props = {
  onClick: jest.fn(),
  children: "some text"
};

expect(shallow(<MyComponent {...props} />).props()).toEqual(props);

with the following error:

...

expect(received).toEqual(expected)

Expected value to equal:
  {"children": "some text", "className": "my-component", "onClick": [Function mockConstructor], "style": [Object object]}
Received:
  {"children": "some text", "onClick": [Function mockConstructor]}

...

Now, one way to fix this is to change the props object into this:

const props = {
  onClick: jest.fn(),
  className: "my-component",
  children: "some text",
  style: { margin: "auto" }
};

But this can quickly get cumbersome if you have a lot of props that are not variable and/or exposed to the component's external API. It also makes my test unnecessarily brittle and not resistant to simple and relatively insignificant changes like if I changed classNam="my-component" to className="some-component", for instance.

Using jest-contain-object, this test can instead be transformed into:

const props = {
  onClick: jest.fn(),
  children: "some text"
};

it(`should pass all props to 'MyComponent'`, () => {
  expect(shallow(<MyComponent {...props} />).props()).toContainObject(props);
});

Now, all we're checking is to make sure that the props that are exposed to the outside are properly passed to the component without having tests that are overly brittle.

Another use case for this is situations where you're spreading props across rendered elements. Let's take the example:

const MyOtherComponent = ({
  onClick,
  className,
  buttonClassName,
  children
}) => (
  <div className={className}>
    <button className={buttonClassName} onClick={onClick}>
      {children}
    </button>
  </div>
);

In this scenario, we might want to test the following:

  1. A div is always rendered
  2. A button is always rendered
  3. The div always and only gets className
  4. The button always and only gets buttonClassName as its className and onClick

Now, we might write some complicated or brittle tests to test all this out. But, using jest-contain-object, we can simply write the following tests to achieve 3 and 4:

const props = {
  className: "hello",
  buttonClassName: "world",
  children: "what's up?",
  onClick: jest.fn()
};

it(`should only give the 'className' prop to the div`, () => {
  const { className, ...otherProps } = props;

  expect(
    shallow(<MyOtherComponent {...props} />)
      .find("div")
      .props()
  ).toContainObject({
    className
  });

  expect(
    shallow(<MyOtherComponent {...props} />)
      .find("div")
      .props()
  ).not.toContainObject(otherProps);
});

it(`should only give the 'buttonClassName', 'onClick', and 'children' props to the button`, () => {
  const { buttonClassName, onClick, children, ...otherProps } = props;

  expect(
    shallow(<MyOtherComponent {...props} />)
      .find("button")
      .props()
  ).toContainObject({
    className: buttonClassName,
    onClick,
    children
  });

  expect(
    shallow(<MyOtherComponent {...props} />)
      .find("button")
      .props()
  ).not.toContainObject(otherProps);
});

Neat, right?

License

jest-contain-object is MIT licensed.