Handler inside createMockFunction never gets triggered.
franzcatch opened this issue · 1 comments
Sample code:
const mock = require("xhr-mock").default
describe('MockingHttp', function() {
// replace the real XHR object with the mock XHR object before each test
beforeEach(() => mock.setup());
// put the real XHR object back and clear the mocks after each test
afterEach(() => mock.teardown());
it("test", async () => {
mock.get(/(.*?)/, (req, res) => {
console.log("code never gets here")
return res.status(201).body('{"data":{"id":"abc-123"}}');
});
await browser.get("http://localhost:59812/stores/11/inventory")
expect(true).toEqual(true)
});
});
Note: I am using this with Protractor on a web app built on Angular 1.5.11.
Note the console.log("code never gets here")
never gets executed. I also put console.log
inside createMockFunction
and it never gets called. I did put one into MockXMLHttpRequest.addHandler
and it does get called. So for some reason, the handler is not triggering the mock function.
Am I doing something wrong here? Have tried several regex to no avail.
I was thinking this would match ALL traffic coming from the browser would get matched up with the mock created. However, I could be mistaken, but looking at the underlying code closer - it seems that xhr-mock is only meant to intercept calls made from a new XMLHttpRequest();
.
It seems that all of the needed pieces to facilitate intercepting all browser traffic with a mock are here in this logic. Perhaps consider tweaking the code a bit to intercept all network traffic and match it up with mocks that are in place so you don't have to deliberately trigger a send
from a new XMLHttpRequest();
?