如何使用jsmine.jQueryify与jasmine-node? [英] How to use jsdom.jQueryify with jasmine-node?
问题描述
是否可以使用jsdom的jQueryify功能来使用jasmine-node?我正在尝试做的是使用NodeJS来测试一些依赖于DOM存在的JavaScript。
Is it possible to user jasmine-node with the jQueryify feature of jsdom? What I'm trying to do is use NodeJS to test some JavaScript that depends on the presence of the DOM.
这是我尝试过的减少的案例。当我运行脚本时,jasmine-node识别规范,但不运行 expect()
:
Here is a reduced case of what I tried. When I run the script, jasmine-node recognizes the spec, but doesn't run the 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中假设类似浏览器的环境?
Alternately, is there a different/better way to test stuff in NodeJS that assumes a browser-like environment?
推荐答案
好的,基于一个这个半相关问题的答案,我能够得到 expect()
来执行。我想我的问题的答案不是使用内置的jQueryify函数,而是手动引入jQuery。
OK, based on one this answer to a semi-related question, I was able to get the expect()
to execute. I guess the answer to my question is not to use the built-in jQueryify function, but to pull in jQuery 'manually'.
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()
})
})
这篇关于如何使用jsmine.jQueryify与jasmine-node?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!