Categories
React Answers

How to fix tests can’t fail within setImmediate or process.nextTick callback with React and Jest?

Spread the love

To fix tests can’t fail within setImmediate or process.nextTick callback with React and Jest, we can use await when we mount our component for testing.

For instance, we write

it("should render proper number of messages based on itemsPerPortion", async () => {
  const component = await shallow(
    <PublishedMessages
      itemsPerPortion={2}
      messagesStore={mockMessagesStore()}
    />
  );

  component.update(); // still needed
  expect(component.find(".item").length).toBe(2);
});

to mount the component PublishedMessages component with shallow.

We use await to wait for the next tick.

And then we call component.update to update the component and call expect to check our result.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *