-
Notifications
You must be signed in to change notification settings - Fork 3.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix xhr.assertSuccess promise #3737
Conversation
LGTM to the PR - this is definitely a must fix thing (may need to interrupt canary to pick it up). Find me to discuss the question offline. |
mockXhr.status = 500; | ||
mockXhr.responseText = '{"a": "hello"}'; | ||
mockXhr.headers['Content-Type'] = 'application/json'; | ||
mockXhr.getResponseHeader = () => 'application/json'; | ||
const promise = assertSuccess( | ||
createResponseInstance('{"a": 2}', mockXhr)); | ||
promise.should.be.rejected; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Isn't the issue that you're not using .should.eventually.be.rejected
? Or, you could return the promise with a flip-flop:
return assertSuccess(createResponseInstance('{"a": 2}', mockXhr)).then(() => {
throw new Error('should never reach this'); // This makes a resolved promise blow up the tests
}, (err) => {
return; // this makes the rejected promise pass the tests
});
Importantly, this won't hide a never resolving promise.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've tested with eventually.be.rejected
that wasn't the issue. The initial issue we thought might happen (on a previous PR #3669) is that returning the already resolved promise returned from response.json()
inside the new Promise
constructor would actually resolve the promise instead of actually the intended behavior of rejecting it. This doesn't see true that's why this test no longer needed/valid.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not seeing a return? We did immediately resolve
when there was a JSON error response, which we definitely should have caught.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah that was previously, we added that test to catch that case and then removed the return statement.
We did immediately resolve when there was a JSON error response, which we definitely should have caught.
Yes the edit to this test makes sure we catch that.
PR won't let me comment on the correct line. On L361, there's no need to create another promise instance (chaining a response.json().then(json => {
err.responseJson = json;
reject(err);
}, () => {
reject(err);
}); |
Didn't realize chaining |
It avoids another Promise instance and runs in the same tick. |
Done. |
LGTM. |
The else part is obvious. What might not be obvious is the test change. @dvoytenko I dropped (changed actually) the test of the previous return a resolved promise issue inside the constructor of the promise, this doesn't actually to have the effect that we thought it would. Tried it with a snippet like the following:
I am not sure if I am still missing the issue with the previous return statement - if I am can you write a small snippet (like the above) that could show the problem? Probably will help me understand it better.