2011-06-02 32 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() 
    }) 
})