How to use jsdom.jQueryify with jasmine-node?

人盡茶涼 提交于 2019-12-07 22:58:34

问题


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.

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)
        })
    })
})

Alternately, is there a different/better way to test stuff in NodeJS that assumes a browser-like environment?


回答1:


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()
    })
})


来源:https://stackoverflow.com/questions/6220889/how-to-use-jsdom-jqueryify-with-jasmine-node

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!