Mocha.js 错误时双重回调

Mocha.js 错误时双重回调,mocha.js,stylus,Mocha.js,Stylus,测试手写笔和断言抛出时,它会再次调用回调,并出现断言错误: var expect = require('chai').expect, stylus = require('stylus'), i = 0 describe('test stylus', function(){ it('calls back', function(done){ stylus('p\n\tcolor white').render(function(err,css){

测试手写笔和断言抛出时,它会再次调用回调,并出现断言错误:

var expect = require('chai').expect,
    stylus = require('stylus'),
    i = 0

describe('test stylus', function(){
    it('calls back', function(done){
        stylus('p\n\tcolor white').render(function(err,css){
            i++;
            console.log('callback', i) //logs twice
            expect(css).equal('p\n\t{ color: bad;\n}')
            done()
        })
    })
}) 
我用这个来解决:

describe('test stylus', function(){
    it('calls back', function(done){
        stylus('p\n\tcolor white').render(function(err,css){
            try {
                expect(css).equal('p\n\t{ color: bad;\n}')
            } catch(e) {
                done(e)
            }   
        })
    })
})

我认为重新调用回调是笔式错误。还是我遗漏了什么?

你的假设是正确的。以下是手写笔的输入:

Renderer.prototype.render = function(fn){

  // ...

  try {

    // ...

    var listeners = this.listeners('end');
    if (fn) listeners.push(fn);
    for (var i = 0, len = listeners.length; i < len; i++) {
      var ret = listeners[i](null, css); // Called here once.
      if (ret) css = ret;
    }
    if (!fn) return css;
  } catch (err) {
    var options = {};
    options.input = err.input || this.str;
    options.filename = err.filename || this.options.filename;
    options.lineno = err.lineno || parser.lexer.lineno;
    if (!fn) throw utils.formatException(err, options);
    // Called here a second time if there is an exception.
    fn(utils.formatException(err, options));
  }
};
Renderer.prototype.render=函数(fn){
// ...
试一试{
// ...
var listeners=this.listeners('end');
if(fn)侦听器。push(fn);
for(var i=0,len=listeners.length;i

fn
是回调。它被添加到
侦听器中
,并将作为调用所有侦听器的循环的一部分被调用一次。如果回调在那里引发异常,那么它将作为异常处理的一部分再次调用。

鉴于stylus render通常是一个构建时函数,我认为这可能是可以接受的行为。