多次访问Zombie.js“断言未定义”?

时间:2013-03-29 04:28:17

标签: node.js coffeescript mocha zombie.js chai

我正在使用Zombie.js和Mocha设置集成测试,并遇到令人费解的问题,即只有第一个browser.visit()调用似乎成功。我的规格看起来像这样:

browser = new Browser site: "http://localhost:101010"

describe '/docs', ->
  ['app', 'server', 'timetable', 'util'].forEach (file) ->
    describe "/#{file}.html", -> it "documents #{file}.coffee", (done) ->
      browser.visit "/docs/#{file}.html", ->
        browser.text('title').should.equal "#{file}.coffee"
        do done

加载/docs/app.html的那些测试的第一个无故障通过。但是,所有后续测试都会失败,产生如下所示的堆栈跟踪:

 ReferenceError: assert is not defined
  at Object.HTML5Parser.phases.inBody.startTagBody (/home/$USER/projects/timetable/node_modules/zombie/node_modules/html5/lib/html5/parser.js:1828:4)
  at Object.HTML5Parser.phases.base.processStartTag (/home/$USER/projects/timetable/node_modules/zombie/node_modules/html5/lib/html5/parser.js:65:40)
  at EventEmitter.Parser.do_token (/home/$USER/projects/timetable/node_modules/zombie/node_modules/html5/lib/html5/parser.js:2436:21)
  at EventEmitter.<anonymous> (/home/$USER/projects/timetable/node_modules/zombie/node_modules/html5/lib/html5/parser.js:2457:30)
  at EventEmitter.emit (events.js:117:20)
  at EventEmitter.emitToken (/home/$USER/projects/timetable/node_modules/zombie/node_modules/html5/lib/html5/tokenizer.js:99:9)
  at emit_current_token (/home/$USER/projects/timetable/node_modules/zombie/node_modules/html5/lib/html5/tokenizer.js:873:3)
  at tag_name_state (/home/$USER/projects/timetable/node_modules/zombie/node_modules/html5/lib/html5/tokenizer.js:400:4)
  at EventEmitter.<anonymous> (/home/$USER/projects/timetable/node_modules/zombie/node_modules/html5/lib/html5/tokenizer.js:941:10)
  at EventEmitter.emit (events.js:95:17)
  at EventEmitter.HTML5Tokenizer.pump (/home/$USER/projects/timetable/node_modules/zombie/node_modules/html5/lib/html5/tokenizer.js:932:11)
  at EventEmitter.HTML5Tokenizer.tokenize (/home/$USER/projects/timetable/node_modules/zombie/node_modules/html5/lib/html5/tokenizer.js:89:23)
  at EventEmitter.Parser.parse (/home/$USER/projects/timetable/node_modules/zombie/node_modules/html5/lib/html5/parser.js:2391:17)
  at HtmlToDom.appendHtmlToElement (/home/$USER/projects/timetable/node_modules/zombie/node_modules/jsdom/lib/jsdom/browser/htmltodom.js:91:50)
  at Object.innerHTML (/home/$USER/projects/timetable/node_modules/zombie/node_modules/jsdom/lib/jsdom/browser/index.js:481:17)
  at Object.core.HTMLDocument.write (/home/$USER/projects/timetable/node_modules/zombie/node_modules/jsdom/lib/jsdom/level2/html.js:406:22)
  at Object.exports.jsdom (/home/$USER/projects/timetable/node_modules/zombie/node_modules/jsdom/lib/jsdom.js:70:9)
  at History._createDocument (/home/$USER/projects/timetable/node_modules/zombie/lib/zombie/history.js:174:22)
  at /home/$USER/projects/timetable/node_modules/zombie/lib/zombie/history.js:126:30
  at /home/$USER/projects/timetable/node_modules/zombie/lib/zombie/resources.js:147:16
  at Request._callback (/home/$USER/projects/timetable/node_modules/zombie/lib/zombie/resources.js:335:16)
  at Request.self.callback (/home/$USER/projects/timetable/node_modules/zombie/node_modules/request/main.js:120:22)
  at Request.EventEmitter.emit (events.js:98:17)
  at Request.<anonymous> (/home/$USER/projects/timetable/node_modules/zombie/node_modules/request/main.js:633:16)
  at Request.EventEmitter.emit (events.js:95:17)
  at IncomingMessage.<anonymous> (/home/$USER/projects/timetable/node_modules/zombie/node_modules/request/main.js:595:14)
  at IncomingMessage.EventEmitter.emit (events.js:117:20)
  at _stream_readable.js:872:14
  at process._tickCallback (node.js:415:13)

Zombie的browser.visit()不打算不止一次被调用,或者是否还有其他问题?

编辑注意版本:

  • Node.js v0.10.1
  • Mocha v1.8.1
  • Chai v1.5.0
  • Zombie.js v1.4.1

1 个答案:

答案 0 :(得分:3)

这似乎是Node版本&gt; = 0.10中出现的兼容性问题,如下所述:https://github.com/assaf/zombie/issues/487

https://github.com/assaf/zombie/issues/487#issuecomment-15548684中提到的解决方法解决了我的特殊问题。我在导入Zombie之前立即将以下代码添加到testi/common.coffee文件中:

do patchEventEmitterToHideMaxListenerWarning = ->
  return if global.eventEmitterPatched
  global.eventEmitterPatched = true
  events = require 'events'
  Old = events.EventEmitter
  events.EventEmitter = ->
    this.setMaxListeners(0)
  events.EventEmitter.prototype = Old.prototype

这样做之后,我的所有测试都没有投诉。我希望这个问题能够在未来的Zombie修订版中修补,但是现在上面的hack使它变得可行。