The test works with jasmine - node, but not with jasmine

I have an object subscribed to an uncaught error event, and I'm trying to check its behavior. At first I tried with jasmine - node, but now when I try to go with jasmine, I found trouble. Can anyone help me.

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

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

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

Thanks in advance.

+4
source share
1 answer

I got this work when executed directly through jasmine when the command is run jasmine ./tests/alLErrorException.spec.js. The following changes are required:

Always tune your listeners, even if _callbackyou shouldn't.

constructor(callback, startListening = true) {
  if (!callback) {
    throw new Error("Missing callback function");
  }

  this._callback = callback;
  this._listening = startListening;
  this._setupEvents();
}

Add a function to intercept events uncaughtExceptionand call _callbackif we _listening:

_handler() {
  if(this._listening) {
    this._callback.apply(null, arguments);
  }
}

uncaughtException _setupEvents:

_setupEvents(attatch = true) {
    this._listening = attatch ? true : false;

    if (attatch) {
        if (typeof window !== "undefined") {
            window.addEventListener("error", this._callback);
        } else {
            // Added this line
            process.removeAllListeners('uncaughtException');
            process.addListener('uncaughtException', this._handler.bind(this));
        }
    } else {
        if (typeof window !== "undefined") {
            window.removeEventListener("error", this._callback);
        } else {
            process.removeListener('uncaughtException', this._callback);
        }
    }
}

, uncaughtException , AllErrorHandler.

- AllErrorHandler .

+3

Source: https://habr.com/ru/post/1690036/


All Articles