-
-
Notifications
You must be signed in to change notification settings - Fork 535
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
Response not returned if using jest.useFakeTimers()
#448
Comments
Hey, @bud-mo. Thanks for reporting this, and especially for the reproduction repository. I'm debugging the issue and I can see that the response Promise resolves correctly, as we replace native I'm researching this at the moment, but I don't have much experience with fake timers, so it's going to take some time. If you are familiar with them, could you double check how you've used them on other projects, or on the web? |
However, the xhr.onload = function() {
var options = {
status: xhr.status,
statusText: xhr.statusText,
headers: parseHeaders(xhr.getAllResponseHeaders() || '')
};
options.url = 'responseURL' in xhr ? xhr.responseURL : options.headers.get('X-Request-URL');
var body = 'response' in xhr ? xhr.response : xhr.responseText;
setTimeout(function() {
resolve(new Response(body, options));
}, 0);
};
That may be the reason your test timeouts. That may also be the reason why if you |
I've also found a few threads that mention that you cannot use
You can find more info of why that's so in the first referenced link. |
If you swap import http from 'http'
test(`Test`, (done) => {
let resBody = ''
// Resolving against the current location, because relative URLs don't exist in NodeJS.
const req = http.request(new URL('/api/test', location.href), {
method: 'POST',
})
req.on('response', (res) => {
res.on('data', (chunk) => (resBody += chunk))
res.on('end', () => {
const resBodyObject = JSON.parse(resBody)
expect(resBodyObject).toMatchInlineSnapshot(`
Object {
"test": "hello",
}
`)
done()
})
})
req.write(JSON.stringify({}))
req.end()
}) SuggestionsI don't think this is the issue with MSW. I suggest you to:
I'm always willing to reopen the issue if we find sufficient proof it's MSW problem. |
@kettanaito Thank you for the support! |
Please let us know what would be the solution to this. We could include fake timers in some usage examples for others to follow. Thanks! |
@kettanaito I ended up using node-fetch. In my case is easy to use because I am wrapping the fetch to inject some auth headers to each request, so I have mocked that function to use the For documentation purpose I think we can provide an example with a simple fetch wrapper around I created a branch with an updated working example with a possible solution, this patch can be a starting point: |
Environment
Request handlers
Actual request
Current behavior
Using
jest.useFakeTimers()
the response is not returned by the server provided bymsw
.The handler is called correctly, but the response hangs.
Expected behavior
Use
jest.useFakeTimers()
into some tests without affectingmsw
.Screenshots
Here You can find a repository to reproduce the issue: https://github.com/bud-mo/msw-example
The text was updated successfully, but these errors were encountered: