単純なプロジェクトの基本的な単体テスト環境をセットアップしようとしていますが、何時間もの調査の後で克服できなかった奇妙なハードルにぶつかっています。
何らかの理由で、Error: NETWORK_ERR: XMLHttpRequest Exception 101
XHR を使用してテンプレートをロードしようとしている EJS から直接返されますが、たまにしか返されません。
ケースは単純です。EJS テンプレートをレンダリングするバックボーン ビューをインスタンス化するバックボーン アプリケーションがあります。これは次のようになります。
BackboneView = Backbone.View.extend({
initialize: function() {
this.template = new EJS({url: '/app/views/root/root.template.ejs'});
}
});
grunt-contrib-jasmine をセットアップするために、 Gruntfile に次のスニペットがあります。
jasmine: {
cms: {
src: "app/**/*.js"
options: {
specs: "tests/fe/spec/*Spec.js",
vendor: [
"vendor/jquery/dist/jquery.js",
"vendor/underscore/underscore.js",
"vendor/backbone/backbone.js",
"vendor/ejs/ejs.js",
],
keepRunner : true,
outfile : "tests/fe/_SpecRunner.html",
host : "http://0.0.0.0:8000"
}
}
}
ほとんどの場合、テスト スイートを実行できます。
$ grunt jasmine
Running "jasmine:cms" (jasmine) task
Testing jasmine specs via PhantomJS
log: came in request!, /app/views/root/root.template.ejs
log: 200
log: /app/views/root/root.template.ejs
website.header
header.test()
- test should return 10...
log:
✓ test should return 10
1 spec in 0.008s.
>> 0 failures
Done, without errors.
しかし、時々、コンソールがこれを私に吐き出します:
$ grunt jasmine
Running "jasmine:cms" (jasmine) task
Testing jasmine specs via PhantomJS
log: came in request!, /app/views/root/root.template.ejs
log: ERROR in request, Error: NETWORK_ERR: XMLHttpRequest Exception 101
log: /app/views/root/root.template.ejs
>> Error caught from PhantomJS. More info can be found by opening the Spec Runner in a browser.
Warning: There is no template at /app/views/root/root.template.ejs Use --force to continue.
Aborted due to warnings.
デバッグに役立つように、EJS エンジンの関連部分にログが追加されました。それは次のようになります。
// The newRequest function for reference
// to see how EJS builds its XHR object
EJS.newRequest = function(){
var factories = [
function() {
return new ActiveXObject("Msxml2.XMLHTTP");
},
function() { return new XMLHttpRequest(); },
function() { return new ActiveXObject("Microsoft.XMLHTTP"); }
];
for(var i = 0; i < factories.length; i++) {
try {
var request = factories[i]();
if (request != null) return request;
}
catch(e) { continue;}
}
}
// And the relevant method in which I added
// some debug trace to see what was happening
EJS.request = function(path){
console.log("came in request!", path);
var request = new EJS.newRequest()
request.open("GET", path, false);
try{request.send(null);}
catch(e){console.log("ERROR in request", e); return null;}
console.log(request.status);
if ( request.status == 404 || request.status == 2 ||
(request.status == 0 && request.responseText == '') ) {
return null;
}
return request.responseText
}
連続して 6 ~ 7 回実行できgrunt jasmine
、シームレスに動作します。突然試行が失敗し、その後の試行で通常に戻る可能性が高くなります。また壊れるまで。そして、これは私がどこにも何も触れずに!
私は本当にこの問題に取り組むことに希望を失い始めています. ここに私が経験したいくつかのソースがありますが、うまくいきませんでした:
- ファントム JS 同期 AJAX 要求: NETWORK_ERR: XMLHttpRequest 例外 101
- grunt-contrib-jasmine と PhantomJS のセキュリティ
- NETWORK_ERROR: XMLHttpRequest 例外 101
- https://github.com/ariya/phantomjs/issues/10028
- https://github.com/ariya/phantomjs/issues/11145
何が原因であるか、または問題をデバッグするために次に何をすべきかについてのヒントを知っている人はいますか?
PS: 私の質問はコード スニペットに少し圧倒されているように感じます。何らかの方法で質問を改善できるかどうか教えてください。