jasmine-jquery
jasmine-jquery copied to clipboard
EventSpy handler is not removed after each test
Every time an event spy is created (e.g. in beforeEach block), a new event handler is added to the selected element. If that element persists between tests, multiple spy handlers will be attached, causing the call count to increase arithmetically. For example, the second test below fails even though it is identical to the first one:
var eventSpy;
beforeEach(function() {
eventSpy = spyOnEvent(document, 'myEvent');
});
it('test 1', function() {
expect(eventSpy.calls.count()).toBe(0);
$(document).trigger('myEvent');
expect(eventSpy.calls.count()).toBe(1);
});
it('test 2', function() {
expect(eventSpy.calls.count()).toBe(0);
$(document).trigger('myEvent');
expect(eventSpy.calls.count()).toBe(1);
});
I think the addition of this will help:
afterEach(function() {
$(document).off();
});
jasmine-jquery
does not, from what I can see, unregister listeners. A possible assumption here is the element one attaches an event spy too, should be destroyed between tests.
The reason why the call count is magically incrementing is because the document
element is not replaced between tests.
@renegare Yes that is correct and that's exactly what I consider a bug. Spies should be removed between tests.
I understand where you are coming from. Had me stomped. The reason I choose not to refer to this as bug because I looked at the code behind this, its quite intentional and IMO makes sense within the context of testing ...
Any who, saying this, I use FlightJS (jasmine-flight) so elements are destroyed automatically, so would be nice and would save time "bug hunting" if all spies are automatically cleaned up.
I think its best someone from the core dev team give some insight on this.
Just ran into this issue today, annoying.