Unit testing window.onerror with jasmine

I am new to javascript and trying to use jasmine to unit test some error handling code.

In particular, I am trying to write several tests that confirm that our custom code (called windowHandleError), which replaces window.onerror (), is called and does what we want.

I tried something like:

       it("testing window.onerror", function() {
        spyOn(globalerror, 'windowHandleError');
        globalerror.install();

        var someFunction = function() {
            undefinedFunction();
        };
        expect(function() {someFunction();}).toThrow();
        expect(globalerror.windowHandleError).toHaveBeenCalled();
    });

But this does not cause attacks. There are some related questions that I have been looking at, but they seem to be asking about specific browsers or how / where to use onerror instead of testing it.
window.onerror does not start in Firefox
Capturing JavaScript error in Selenium
window.onerror does not work
How to run script.onerror in Internet Explorer?

, , , onerror, . - ?

+3
2

.

try/catch, , , (True QUnit ). window.onerror , try/catch, unittest.

onerror .

try {
    //Code that should fail here.
    someUndefinedFunction();
} catch (e) {
    window.onerror.call(window, e.toString(), document.location.toString(), 2);
}

expect(globalerror.windowHandleError).toHaveBeenCalled();

, document.location url, . e.stack .

, unit test, , ​​ , .

0

JavaScript Buster.JS, .

, window.onerror, :

  "error within the system": function (done) {

    setTimeout(function() {
      // throw some real-life exception
      not_defined.not_defined();
    }, 10);

    setTimeout(function() {
      assert.isTrue($.post.called);
      done();
    }, 100);
  }

setTimeout, , , 100 setTimeout, done(), , Buster. JS. Jasmine, done() async.

+3

All Articles