所以我試圖用jsdom建立一個摩卡測試,經過多次嘗試調試後,我將問題縮小到jsdom執行絕對URL腳本(例如http://code.jquery.com/jquery-2.1.3.min.js
)而不是相對URL腳本(例如js/script.js
)。JSDom沒有加載相關腳本
我test.js低於:
var assert = require("assert");
var fs = require('fs');
var jsdom = require("jsdom");
describe('Foo Test', function(){
it('Foo Check', function(done){
this.timeout(5000);
jsdom.env({
html: fs.readFileSync('index.htm')
,features: {
FetchExternalResources: ["script"]
,ProcessExternalResources: ["script"]
}
,done: function(errors, window){
if(errors != null) console.log('Errors', errors);
var $ = window.$; // $ is defined
var foo = window.foo; //foo is undefined
assert(foo);
done();
}
});
});
});
和錯誤: Uncaught AssertionError: undefined == true
熱媒:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Foo</title>
<script src="http://code.jquery.com/jquery-1.11.2.min.js"></script>
<script src="js/script.js"></script>
</head>
<body>
</body>
</html>
的script.js:
var foo = true;
很酷知道,謝謝。 – Alex
有機會嘗試它,它沒有path.join工作,因爲test.js在它自己的文件夾中(所以它試圖在與root相對的測試文件夾中找到index.htm)。 – Alex