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-react-matchers

v0.3.0

Published

A small set of react matchers to make testing ReactJS components with Jest more pleasant.

Downloads

75

Readme

Jest React Matchers

React element matching the way it should be. Make your tests clean, simple, and easy to read. This is a non-intrusive addition to your existing testing frameworks.

These matchers are totally pre-beta. Feedback is absolutely welcome. As are contributions. I'm releasing a 0.1.0 with the element matcher and the children matcher. I'm also going to write a recursive search matcher too in the next few weeks, but I think this is a decent start.

Background

Learning to test ReactJS components was a harrowing experience. It's easy to find yourself with a test suite full of things like this:

const component = renderedElement.props.children.props.children[0];
expect(component.type).toEqual(MyComponent);
// ...

Then, you update your structure and now suddenly rendered.props.children is an array instead! All your tests are broken and you have to go back and update them:

const component = renderedElement.props.children[0].props.children[0];
expect(component.type).toEqual(MyComponent);
// ...

And you probably at some point end up with dozens of lines like this:

const component = renderedElement.props.children;
expect(component.props.firstName).toEqual('pippy');
expect(component.props.lastName).toEqual('dippy');
expect(component.props.age).toEqual(5);
expect(component.props.className).toEqual('kiddo');
// ...

So, you look at Enzyme, which is totally awesome! So now you can write things like:

const wrapper = mount((
  <ul>
    <li>Pippy</li>
    <li>Hatch</li>
    <li>Lu</li>
  </div>
));

expect(wrapper.containsAllMatchingElements([
  <li>Pippy</li>,
  <li>Hatch</li>
])).to.equal(true);

This is super great! But, when it fails unexpectedly, it doesn't tell you anything beyond, "expected true but got false." Bah!

Enter jest react matchers. Now, you can write:

const actual = (
  <ul>
    Why is this here!?
    <li>Pippy</li>
    <li>Hatch</li>
    <li wife={true}>Lu</li>
  </ul>
);

it('should match all of the expected children', () => {
  const expected = (
    <ul>
      Why is this here!?
      <li>Pippy</li>
      <li wife={true} />
    </ul>
  );

  expect(actual).toHaveMatchingChildren(<li />, <li>Pippy</li>, <li>Hatch</li>);
  expect(actual).toHaveMatchingChildren('Why is this here!?');
  expect(actual).toHaveMatchingChildren(expected.props.children);

  expect(actual).not.toHaveMatchingChildren(<div />, 'nor this');
});

Pass a varargs, an array, or a single instance and get what you expect. Match on children and other properties. It's an asymetric matcher. Be as generic or specific as you want. When something doesn't match, get meaningful output.

Expected to find children:
  <div data-awesome='true'>
    An awesome div!!
  </div>
  <span>
    Lame Span
  </span>

in:
  <div>
    <li>
      Pippy
    </li>
    <li>
      Hatch
    </li>
    <li data-tis-wife='true'>
      Lu
    </li>
  </div>

Expected not to find children:
  <li data-tis-wife='true' />
  <li>
    Lu
  </li>

in:
  <div>
    <li>
      Pippy
    </li>
    <li>
      Hatch
    </li>
    <li data-tis-wife='true'>
      Lu
    </li>
  </div>

Find Element in Hierarchy

Finding elements in a hierarchy is also easy. That is to say, if you're looking for the presence (or absence) of elements anywhere in the object graph, you can do that too!

const actual = (
  <div>
    <h1>Why is this here!?</h1>
    <div>
      <span>Pippy</span>
    </div>
    <li>Hatch</li>
    <div>
      <p>
        <li wife={true}>Lu</li>
      </p>  
    </div>
  </div>
);

it('should match all of the expected children', () => {
  expect(actual).toFindMatchingElements(<li />, <span>Pippy</span>, <li>Hatch</li>, 'Pippy');
  expect(actual).toFindMatchingElements('Why is this here!?');

  expect(actual).not.toFindMatchingElements(<marquee />, 'nor this');
});

Installation

npm install jest-react-matchers --save

Usage

import jestReactMatchers from 'jest-react-matchers';
expect.extend(jestReactMatchers);

const element = (
  <div name='pip' adorbs={true}>
    <h1>Permanent Excitement!!</h1>
    <h2 />
  </div>
);

expect(element).matchElement(<div name='pip' />);
expect(element).matchElement(<div name='pip'><h1 /></div>);

expect(element).toHaveMatchingChildren(<h1 />, <h2 />);
expect(element).toHaveMatchingChildren(element.props.children);

License

MIT