我正在使用 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 版本 >=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
这样做之后,我所有的测试都毫无怨言地通过了。我希望这个问题会在 future 的 Zombie 修订版中得到修复,但目前上述 hack 使其可行。