How to spy on a custom event in jasmine?
jasmine, javascript, testing
Solution
Try something like this. Worked for me
describe("Test:", function(){
it("Expects event will be spied: ", function() {
var eventSpy = jasmine.createSpy();
sampleElement.addEventListener('sample event', eventSpy);
expect(eventSpy).toHaveBeenCalled();
});
Problem
I have a custom event defined. I want to spy on it with jasmine. But the problem I have is that it is failing when I am using `spyOn` to spy on that event. When I spy on some function it is working fine. Heres what I tried: ``` describe("Test:", function(){ it("Expects event will be spied: ", function() { var eventSpy = spyOn(window, 'myEvent').andCallThrough(); expect(eventSpy).toHaveBeenCalled(); //Also tried this: //expect(eventSpy).not.toHaveBeenCalled(); }); }); ``` So I tried both `not.toHaveBeenCalled()` and `toHaveBeenCalled()` but it fails in both the cases. So I guess `spyOn` is unable to spy on the custom event. *Note: * I looked at other SO answers with a similar question, but it was something to do with a click event. But in my case it is a custom event that will get fired based on some conditions automatically.