-
Notifications
You must be signed in to change notification settings - Fork 8
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
Cannot programmatically abort an xhr #42
Comments
Hi, the You read more about the rationale for this change here: #30 I can see that your usage pattern doesn't fit directly with this change. Could you explain some more what you're trying to achieve by calling |
In my app the user is uploading some content using an xhr. Whenever this xhr gets cancelled by the user (by clicking on the I want to check that the correct information is displayed to the when the xhr is aborted this way. xhr.addEventListener('abort', () => {
// display some info to the user
}) That's the reason why I need to call |
Ok I understand the use-case. Any chance you can show more of the test case? There's one workaround which is to do something like this:
If that doesn't work for you I can see other options, including making the xhr available in |
I am testing a service, I don't have access to the underlying xhr. Service code: const service = () => new Promise((resolve, reject) => {
const xhr = new XMLHTTPRequest();
xhr.addEventListener('abort', () => reject('user cancelled the upload'))
// ...
}) Test: // This used to work in 7.0.4
;(global.XMLHttpRequest as any).onSend = (xhr: XMLHttpRequest) => {
xhr.abort()
}
await expect(service()).rejects.toEqual('user cancelled the upload') I guess adding a new Another way would be to expose the xhr on the onSend method as you suggested. |
Can you try with ced7ace? I added the |
I retrieved the branch and built it but I don't have access to the second parameter. This is how I am using it: import * as MockXMLHttpRequest from 'mock-xmlhttprequest'
// ...
const MockXhr = MockXMLHttpRequest.newMockXhr()
global.XMLHttpRequest = MockXhr
// ...
it('test', () => {
// onSend appears to be defined like this (see types/MockXhr.d.ts) and only receives one parameter:
// onSend?: (xhr: MockXhr) => void;
;(global.XMLHttpRequest as any).onSend = (xhr: XMLHttpRequest) => {
xhr.abort()
}
} |
That's unexpected. Here's what my
I even included a unit test to test that the xhr gets passed to the callback: ced7ace#diff-c98920d8a38c44bd1bcddb36d810958f375925f7c60957d780816f09c4fd2ded |
Ah, my bad! |
* Remove useless Promise resolution value * Add MockXhr argument to the onSend hook. Fixes #42
Describe the bug
In version
7.0.4
I was able to programmatically abort a request.In version
8.1.0
this doesn't seem to work anymore:abort
isn't defined anymore inMockXhrRequest
.Is there a way to programmatically abort the request in the new version?
Steps to reproduce the behavior:
Environment in which you encounter the issue:
The text was updated successfully, but these errors were encountered: