简体   繁体   English

禁用mocha.js的错误处理

[英]Disable error handling for mocha.js

Does anyone know if there a way to disable error handling by Mocha? 有没有人知道是否有办法禁用Mocha的错误处理?

I'd really like to let the exception bubble up and be handled by the browser during debugging. 我真的想让异常冒出来并在调试期间由浏览器处理。

Update: The context is this. 更新:上下文是这样的。 - Some code throw's an unexpected error. - 某些代码抛出了意外错误。 - I would like to disable "try/catch" (something like jasmine's tests do) and be thrown into the debugger in chrome. - 我想禁用“try / catch”(类似于jasmine的测试)并将其抛入chrome中的调试器中。

This way I can inspect the current state of the application at that moment. 这样我就可以检查当时应用程序的当前状态。 It sounds like there isn't something built in, and I will need to manually do this each time I get some odd error in a test. 听起来好像没有内置的东西,每次我在测试中出现一些奇怪的错误时我都需要手动执行此操作。 Maybe by wrapping my code with 也许通过包装我的代码

try {
  somethingThatBreaks();
} catch(e) {
  debugger;
}

Mocha installs its own onerror handler to trap exceptions that may occur in asynchronous code. Mocha安装自己的onerror处理程序来捕获异步代码中可能发生的异常。 You can disable it with: 你可以用以下方法禁用它:

window.onerror = undefined;

This is dangerous . 这很危险 You should do this only when you must absolutely do so. 只有在绝对必须这样做时才应该这样做。 If you make this a routine thing, Mocha will completely miss errors that happen in asynchronous code. 如果你把它作为例程,Mocha将完全错过异步代码中发生的错误。

If what you want to do is let exceptions in synchronous code bubble up, I'm not seeing what this would accomplish. 如果您想要做的是让同步代码中的异常冒出来,我就不会看到它会实现什么。 If you want to inspect the exception before Mocha touches it, a try...catch block wrapping your test will do this. 如果你想在Mocha触摸它之前检查异常,那么包装你的测试的try...catch块将会这样做。

Mocha's error handling is contained in the following code : Mocha的错误处理包含在以下代码中

if (this.async) {
  try {
    this.fn.call(ctx, function(err){
      if (err instanceof Error || toString.call(err) === "[object Error]") return done(err);
      if (null != err) return done(new Error('done() invoked with non-Error: ' + err));
      done();
    });
  } catch (err) {
    done(err);
  }
  return;
}

// sync
try {
  if (!this.pending) this.fn.call(ctx);
  this.duration = new Date - start;
  fn();
} catch (err) {
  fn(err);
}

You could possibly throw the error with the catch (or remove the try..catch entirely) however it would most likely break the test suite if a test fails. 您可能会使用catch抛出错误(或完全删除try..catch )但是如果测试失败,它很可能会破坏测试套件。

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

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