question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Testing window.onbeforeunload

See original GitHub issue

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?

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Comments:9 (2 by maintainers)

github_iconTop GitHub Comments

7reactions
rinatiocommented, May 7, 2014

Setting onbeforeunload to null helps here:

window.onbeforeunload = null;

Still curious if you have some other suggestions,

Thanks

3reactions
whyboriscommented, Jul 13, 2018

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();
      });
    });
Read more comments on GitHub >

github_iconTop Results From Across the Web

Test onbeforeunload in Jest - Stack Overflow
Show activity on this post. You best bet should be dispatching the event yourself. window.dispatchEvent(new Event("beforeunload"));.
Read more >
Window: beforeunload event - Web APIs | MDN
The beforeunload event is fired when the window, the document and its resources are about to be unloaded. The document is still visible...
Read more >
window.onbeforeunload and Cypress - Gleb Bahmutov
The application code uses window.onbeforeunload callback to ask the user to confirm before navigating away from the page. public/app.js ...
Read more >
onBeforeUnload test - JSFiddle - Code Playground
Test your JavaScript, CSS, HTML or CoffeeScript online with JSFiddle code editor. ... window.onbeforeunload = function(e) {. 2. e = e || window.event;....
Read more >
unload And beforeunload Events And How To Debug Them ...
log("second event to fire (2)"); }; // top window's unload event would fire after all beforeunload events have been fired window.onunload = ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found