Reputation: 3181
We're running some functional unit tests using theintern.io. Unfortunately, third party network calls randomly cause the page to time out, thus causing all of our unit tests to fail.
At a certain point, I'd like to cut the cord on all network calls to prevent the browser from hanging / tests from failing. I've tried window.stop()
, but this causes the test to hang.
How can I stop all network calls without also stopping javascript execution of my functional tests?
Upvotes: 0
Views: 605
Reputation: 337
Firstly, window.stop
won't stop javascript execution, in your case it might look like its doing so, if the tests are getting loaded dynamically.
window.stop
is same as click stop button on your browser's address bar (Source: https://developer.mozilla.org/en-US/docs/Web/API/Window/stop).
Now coming to your questions, there are multiple ways to handle this, my suggestion would be to mock all the network calls. (Assuming the third party calls that you mentioned are all AJAX). This is also a good practice while writing unit tests. You can use sinon
for this. (Source: http://sinonjs.org/)
You can give an expected response, or just give a 404
or anything else, and respond immediately to the requests. That way, your tests won't timeout. Hope it helps
{
beforeEach() {
server = sinon.fakeServer.create();
},
afterEach() {
server.restore()
},
tests: {
testA() {
server.respondWith("GET", /<regexPattern>/,[200, {"Content-Type": "text/plain"},""]);
server.respondImmediately = true;
}
}
}
Upvotes: 1