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 then
s 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 then
s 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 badges1 Answer
Reset to default 5Best thing I found instead of multiple await
s 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 then
s 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 then
s 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 badges1 Answer
Reset to default 5Best thing I found instead of multiple await
s 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
版权声明:本文标题:javascript - Jest test: Awating multiple promises in connected component - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745505027a2153564.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论