繁体   English   中英

test适用于jasmine-node,但不适用于jasmine

[英]test works with jasmine-node, but not and with jasmine

我有一个订阅了未被捕获的错误事件的对象,我正在尝试测试它的行为。 首先我尝试使用jasmine-node,但现在当我尝试使用茉莉花时,我发现了麻烦。 任何人都可以帮助我。

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)
});

提前致谢。

当运行jasmine ./tests/alLErrorException.spec.js命令时,我通过jasmine直接执行此工作。 需要进行以下更改:

始终设置侦听器,即使不应执行_callback也是_callback

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

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

添加一个函数来拦截uncaughtException事件,如果我们_callback则调用_listening

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

删除_setupEvents所有其他uncaughtException事件处理程序:

_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);
        }
    }
}

这是必需的,因为jasmine设置了自己的uncaughtException处理程序并报告错误,即使错误是由AllErrorHandler类捕获的。

下面是AllErrorHandler类的完整源代码的粘贴,其中包含所需的更改。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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