gylaz
gylaz

Reputation: 13581

Jest test: Awating multiple promises in connected component

I have a connected component (HOC) that fetches some data in componentDidMount, like:

function setData(data) {
  return { type: "SET_DATA", data: data };
}

function fetchData() {
  return axios.get("http://echo.jsontest.com/key/value/one/two");
}

function getData(dispatch) {
  return () => fetchData().then(response => dispatch(setData(response.data)));
}

class Welcome extends Component {
  componentDidMount() {
    this.props.getData();
  }

  render() {
    if (this.props.data) {
      return <div>We have data!</div>;
    } else {
      return <div>Waiting for data...</div>;
    }
  }
}

Full code can be seen here: https://github.com/gylaz/react-integration-test-example/blob/eb3238c0a8aa4b15331a031d7d2d3a0aa97ef9c7/src/App.js

And my test looks like:

it("renders without crashing", async () => {
  axios.get = jest.fn(() => {
    return Promise.resolve({ data: { one: 1, two: 2 } });
  });
  const div = document.createElement("div");
  const component = await ReactDOM.render(<App />, div);

  expect(div.textContent).toEqual("We have data!");
});

Full test code here: https://github.com/gylaz/react-integration-test-example/blob/eb3238c0a8aa4b15331a031d7d2d3a0aa97ef9c7/src/App.test.js

The test passes fine!

However, when I make a modification to the fetchData method to extract out the actual data from the response (via Promise), like:

function fetchData() {
  return axios
    .get("http://echo.jsontest.com/key/value/one/two")
    .then(response => response.data);
}

function getData(dispatch) {
  return () => fetchData().then(data => dispatch(setData(data)));
}

The test will fail, until I add another await right before the first await:

it("renders without crashing", async () => {
  axios.get = jest.fn(() => {
    return Promise.resolve({ data: { one: 1, two: 2 } });
  });
  const div = document.createElement("div");
  const component = await await ReactDOM.render(<App />, div);

  expect(div.textContent).toEqual("We have data!");
});

Here's a PR showing the above: https://github.com/gylaz/react-integration-test-example/pull/1

The more thens in my call chain, the more awaits I need to add, and that seems cumbersome.

Is there a better way to await everything at once in the test, or another solution?

Upvotes: 2

Views: 5018

Answers (1)

gylaz
gylaz

Reputation: 13581

Best thing I found instead of multiple awaits is to wrap the expectation in setImmediate or process.nextTick. And you don't need to use async/await.

For example:

it("renders without crashing", () => {
  axios.get = jest.fn(() => {
    return Promise.resolve({ data: { one: 1, two: 2 } });
  });
  const div = document.createElement("div");
  const component = ReactDOM.render(<App />, div);

  setImmediate(() => {
    expect(div.textContent).toEqual("We have data!");
  });
});

An explanation about promises and the event look can be found in this article.

One downside of this approach is that Jest will crash the test runner if an expectation fails (rather than showing the failure but not crashing). There is currently a bug which can be followed in this issue on GitHub.

Upvotes: 5

Related Questions