简体   繁体   中英

jasmine testing uncaught error event handler

I am having difficulties testing a new package that I am writing with jasmine. The package idea is to create a listener for error or "uncaughtException" (in node) and give it a callback to work if there is such event.

describe('AllErrorHandler', function () {
    it('error is passed to the callback', function () {
        const error = new Error("testError");
        const callbackError;

        let errorHandler = new AllErrorHandler((error) => {
            callbackError = error;
        })

        throw error;

        expect(callbackError).toBe(error);
    })
})

How can I make this right?

First of all, error is not a function. You need throw error not throw error() .

Then the line after you throw is not reachable (unless you wrap the whole thing with try-catch :) which kinda beats the whole point of the test).

You could try something like this. But I'm not 100% this would work.

describe('AllErrorHandler', function () {
    it('error is passed to the callback', function (done) {
        const error = new Error("testError");

        let errorHandler = new AllErrorHandler((arg1) => {
           expect(arg1).toBe(error);
           done();
        });

        setTimeout(() => {
          throw error;
        }, 0)
    })
})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM