所以我试图用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