【问题标题】:How to use jsdom.jQueryify with jasmine-node?如何将 jsdom.jQueryify 与 jasmine-node 一起使用?
【发布时间】:2011-06-02 22:01:47
【问题描述】:

可以使用 jsdom 的 jQueryify 功能来使用 jasmine-node 吗?我正在尝试做的是使用 NodeJS 来测试一些依赖于 DOM 存在的 JavaScript。

这是我尝试过的简化案例。当我运行脚本时,jasmine-node 识别规范,但不运行 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 中测试东西是否有不同/更好的方法?

【问题讨论】:

    标签: javascript node.js bdd jasmine jsdom


    【解决方案1】:

    好的,基于一个this answer to a semi-related question,我能够让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()
        })
    })
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-09
      • 1970-01-01
      • 2022-11-04
      • 1970-01-01
      相关资源
      最近更新 更多