admin管理员组

文章数量:1022712

I have a connected ponent (HOC) that fetches some data in ponentDidMount, like:

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

function fetchData() {
  return axios.get("");
}

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

class Wele extends Component {
  ponentDidMount() {
    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: .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 ponent = await ReactDOM.render(<App />, div);

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

Full test code here: .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("")
    .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 ponent = await await ReactDOM.render(<App />, div);

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

Here's a PR showing the above:

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?

I have a connected ponent (HOC) that fetches some data in ponentDidMount, like:

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

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

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

class Wele extends Component {
  ponentDidMount() {
    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./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 ponent = await ReactDOM.render(<App />, div);

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

Full test code here: https://github./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./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 ponent = await await ReactDOM.render(<App />, div);

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

Here's a PR showing the above: https://github./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?

Share Improve this question asked Jun 29, 2017 at 14:07 gylazgylaz 13.6k8 gold badges53 silver badges58 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 5

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 ponent = 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.

I have a connected ponent (HOC) that fetches some data in ponentDidMount, like:

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

function fetchData() {
  return axios.get("");
}

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

class Wele extends Component {
  ponentDidMount() {
    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: .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 ponent = await ReactDOM.render(<App />, div);

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

Full test code here: .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("")
    .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 ponent = await await ReactDOM.render(<App />, div);

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

Here's a PR showing the above:

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?

I have a connected ponent (HOC) that fetches some data in ponentDidMount, like:

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

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

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

class Wele extends Component {
  ponentDidMount() {
    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./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 ponent = await ReactDOM.render(<App />, div);

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

Full test code here: https://github./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./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 ponent = await await ReactDOM.render(<App />, div);

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

Here's a PR showing the above: https://github./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?

Share Improve this question asked Jun 29, 2017 at 14:07 gylazgylaz 13.6k8 gold badges53 silver badges58 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 5

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 ponent = 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.

本文标签: javascriptJest test Awating multiple promises in connected componentStack Overflow