When testing stylus and the assertion throws, it calls the callback a second time with the Assertion error:

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

I'm using this to work-around:

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

I'm thinking it's a stylus bug to re-call the callback. Or Am I missing something here?

有帮助吗?

解决方案

Your hypothesis is correct. Here is the code in stylus:

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

fn is the callback. It is added to listeners and will be called once as part of the loop that calls all listeners. If the callback raises an exception there, then it is called again as part of the exception handling.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top