Karma: Testing window.onbeforeunload

Created on 7 May 2014  路  8Comments  路  Source: karma-runner/karma

Hi guys,

I'm trying to test an event handler for window.onbeforeunload and getting

some of your tests did a full page reload!

Even though I'm running my tests with

angular.element(window).triggerHandler('beforeunload')

I see the code and it's actually listening for the same event. Is there any way to temporary disable that error or handle it somehow else?

backlog

Most helpful comment

Setting onbeforeunload to null helps here:

window.onbeforeunload = null;

Still curious if you have some other suggestions,

Thanks

All 8 comments

Setting onbeforeunload to null helps here:

window.onbeforeunload = null;

Still curious if you have some other suggestions,

Thanks

+1

This is caused by a bugfix https://github.com/karma-runner/karma/commit/15d80f47a227839e9b0d54aeddf49b9aa9afe8aa

In my opinion, this isn't a bugfix, just annoying. I can't event call window.onbeforeunload() to test my code.

The commit is a sadly very necessary bugfix as all major test frameworks (mocha, jasmine, qunit) immediately break if a test does a page reload. So we are protecting against it, as they can't handle this behaviour. If there were a test framework that persisted it test state over a page reload using something like a server, local storage or sth. else we could think of adding an option to disable it, but at the moment I do not know of a framework that allows this.

Closing due to inactivity

For reference, this worked for me.
Angular service:

    setUpBeforeunload(): void {
      window.addEventListener('beforeunload', this.closeConnection);
    }

And onto unit testing with Jasmine:

    describe('setUpBeforeunload', () => {
      it('should set up window eventListener', () => {
        spyOn(window, 'addEventListener');
        service.setUpBeforeunload();
        expect(window.addEventListener).toHaveBeenCalledWith('beforeunload', service.closeConnection);
      });

      it('should call closeConnection()', () => {
        spyOn(service, 'closeConnection');
        service.setUpBeforeunload();
        window.dispatchEvent(new Event('beforeunload'));
        expect(service.closeConnection).toHaveBeenCalled();
      });
    });

test coverage is not happening
any solution what wrong i did?

// function
@HostListener('window:onbeforeunload', ['$event'])
clearLocalStorage(event) {
window.localStorage.removeItem('currentUser');
}
// test case
it('sholud check clearLocalStorage() for clear local storage', () => {
spyOn(localStorage, 'getItem').and.callFake(mockLocalStorage.removeItem);
let event = new Event('onbeforeunload');
Object.defineProperty(event, 'keyCode', { 'value': localStorage.getItem('currentUser') });
document.dispatchEvent(event);
expect(true).toEqual(true);
})

Setting onbeforeunload to null helps here:

window.onbeforeunload = null;

Still curious if you have some other suggestions,

Thanks

Setting this before:
window.dispatchEvent(new Event('beforeunload')) worked for me

Was this page helpful?
0 / 5 - 0 ratings