How to run 1000 React tests per second

Published: 2023-06-08 by Lars  tooltestcode

Presentation by Lars Thorup at Copenhagen React meetup June 8th, 2023.

Video recording on Youtube

snail after running cheetah

How it started

(1553 tests in 4 seconds - with Mocha and Enzyme)

How it's going

(89 tests in 80 seconds - with Vitest, React Testing Library and jsdom)

So: what's the problem?

DOM testing profile

Prefer unit tests over integration tests

Testing Pyramid

Introducing "react-test-renderer"

react-test-renderer-sandbox sub millisecond test runs

Simple example test

function Count() {
  const [count, setCount] = useState(0);
  const onClick = () => {
    setCount((count) => count + 1);
  };
  return (
    <button onClick={onClick}>
      <span>{`count is ${count}`}</span>
    </button>
  );
}

it("should let user click to increment count", () => {
  const { root } = TestRenderer.create(<Count />);
  const button = root.findByType("button");

  button.props.onClick();

  expect(button.props.children.props.children).toEqual("count is 1");
});

Comparison

150 times faster on Windows 50 times faster on Ubuntu

Trade-offs

Pursue the speed!

1500 dots in 4 seconds

(1500 tests in 4 seconds - with Vitest and React Test Renderer)

Discuss on Twitter