2011-06-02 75 views
2

用jasmine-node与jsdom的jQueryify功能是否可以使用?我想要做的是使用NodeJS来测试一些JavaScript,这取决于DOM的存在。如何在jasmine-node中使用jsdom.jQueryify?

这是我试过的一个简化案例。当我运行该脚本,茉莉节点识别规范,但不运行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测试的东西?

回答

2

好的,基于一个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() 
    }) 
})