I am currently conducting a test on an AJAX request using the XMLHttpRequest
method:
export default function performTestRequest() {
const xhr = new XMLHttpRequest();
xhr.open('GET', 'https://example.com/service');
xhr.onload = () => {
console.log('onload');
// etc.
};
xhr.onerror = () => {
console.log('onerror');
// etc.
};
xhr.send();
}
To carry out the test, I have utilized Nock in combination with Mocha:
describe('performTestRequest()', () => {
it('should handle a successful request', () => {
nock('https://example.com')
.log(console.log)
.get('/service')
.reply(200, { some: 'json' });
performTestRequest();
expect( /* things I want to check */ ).to.be.true;
});
});
Upon running this test, I noticed that instead of triggering xhr.onload()
, xhr.onerror()
is being fired. Despite confirming through Nock's log output that my AJAX request is indeed intercepted and matches the expected URL by Nock.
What could be the reason for xhr.onerror
getting called over xhr.onload
in my testing scenario?