4

我正在使用 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
  • 摩卡 v1.8.1
  • 柴 v1.5.0
  • 僵尸.js v1.4.1
4

1 回答 1

3

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

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

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 使其可行。

于 2013-03-29T12:18:18.173 回答