是否可以使用 jsdom 的 jQueryify 功能来使用 jasmine-node?我想做的是使用 NodeJS 来测试一些依赖于 DOM 是否存在的 JavaScript。
这是我尝试过的简化案例。当我运行脚本时,茉莉花节点识别规范,但不运行expect()
:
var fs = require('fs'),
jsdom = require('jsdom'),
window = jsdom.createWindow(),
jasmine = require('jasmine-node')
describe("jQueryify", function() {
it('should run the test!', function () {
jsdom.jQueryify(window, function (window, jquery) {
expect(1).toEqual(1)
})
})
})
或者,是否有一种不同/更好的方法来测试 NodeJS 中的内容(假设有类似浏览器的环境)?
好的,基于一个这是对半相关问题的回答 https://stackoverflow.com/questions/6101673/what-are-the-use-cases-of-jsdom/6130263#6130263,我能够得到expect()
执行。我想我的问题的答案不是使用内置的 jQueryify 函数,而是“手动”引入 jQuery。
var fs = require('fs'),
window = require('jsdom').jsdom('<html><head></head><body></body></html>').createWindow(),
script = window.document.createElement('script'),
jasmine = require('jasmine-node')
describe("jQueryify", function() {
it('should run the test!', function () {
script.src = './path/to/jquery.js'
script.onload = function () {
expect(typeof window.$).toEqual('function')
asyncSpecDone()
}
window.document.head.appendChild(script)
asyncSpecWait()
})
})
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)