运行单元测试时,PhantomJS在语法错误后退出

时间:2014-09-16 08:12:16

标签: javascript unit-testing jasmine phantomjs karma-runner

我们在构建服务器上使用Karma和PhantomJS的组合运行我们的Jasmine单元测试。在Chrome上本地运行测试工作正常,错误不会在那里发生(这是一个不同的问题)。以下是发生的事情:

在执行的某一点上,测试会进入" undefined"错误并停止:

PhantomJS 1.9.7 (Mac OS X) ERROR
  TypeError: 'undefined' is not an object (evaluating 'dropScope.resize')
  at /Users/nwinkler/workspaces/.../foo.js:250
PhantomJS 1.9.7 (Mac OS X): Executed 654 of 1221 ERROR (14.512 secs / 14.386 secs)
DEBUG [karma]: Run complete, exitting.
DEBUG [launcher]: Disconnecting all browsers

即使使用--force标志运行业力,它仍会在此时退出。

有没有办法让Karma / PhantomJS继续运行测试而不停止此时?为什么PhantomJS无法从此错误中恢复?

我没有找到undefined错误的解决方案,这是一个不同的主题 - 我想了解为什么PhantomJS和Karma在此时退出并且不再继续剩余的单元测试。

1 个答案:

答案 0 :(得分:0)

我通常使用此代码来警告错误并恢复运行(phantomjs)

phantom.onError = function(msg, trace) {
  var msgStack = ['PHANTOM ERROR: ' + msg];
  if (trace && trace.length) {
    msgStack.push('TRACE:');
    trace.forEach(function(t) {
      msgStack.push(' -> ' + (t.file || t.sourceURL) + ': ' + t.line + (t.function ? ' (in function ' + t.function +')' : ''));
    });
  }
  console.error(msgStack.join('\n'));
  //phantom.exit(1); // do not stop
};

如果您使用的是页面对象:

page.onError = function(msg, trace) {
  // never display errors
   var msgStack = ['SITE ERROR: ' + msg];
  if (trace && trace.length) {
    msgStack.push('TRACE:');
    trace.forEach(function(t) {
      msgStack.push(' -> ' + (t.file || t.sourceURL) + ': ' + t.line + (t.function ? ' (in function ' + t.function +')' : ''));
    });
  }
  console.error(msgStack.join('\n'));
}
相关问题