2016-04-05 63 views
1

jQuery 3.0 Beta Released如何測試jQuery 3.0 beta在瀏覽器中是否與Promises/A +兼容?

jQuery.Deferred is now Promises/A+ compatible jQuery.Deferred objects have been updated for compatibility with Promises/A+ and ES2015 Promises, verified with the Promises/A+ Compliance Test Suite .


How To Run

The tests can run in either a Node.js environment or, if you set things up correctly, in the browser.

如何運行在瀏覽器中測試,沒有nodejs驗證?


注意,尚未實現運行在瀏覽器中測試,而nodejs。 @JaredSmith的幫助對使用nodejs運行測試是必不可少的。

+0

請詢問僅爲後一個問題。 – Bergi

+0

您可以輕鬆地將jQuery的延遲實現放入nodejs模塊並在那裏測試,它不應該有所作爲。 – Bergi

+0

@Bergi對'nodejs'或'nodejs'模塊幾乎沒有經驗。 _「如果你在瀏覽器中設置正確。」_因此目前的查詢。更新OP到單個問題 – guest271314

回答

2

在評論方式適應了jQuery推遲構造,以滿足spec的API引用的適配器。這樣做可能是如下的方式(採取或多或少從here):

var promisesAplusTests = require("promises-aplus-tests"); 
var jq     = require('jquery'); 
var jsdom    = require('jsdom'); 
jsdom.env('<p></p>', function(err, window) { 
    if (err != null) { 
     throw err; 
    } else { 
     var $ = jq(window); 
     var adapter = {}; 
     adapter.deferred = function() { 
      var deferred = $.Deferred(); 

      return { 
       promise: deferred.promise(), 
       resolve: deferred.resolve.bind(deferred), 
       reject: deferred.reject.bind(deferred) 
      }; 
     }; 
     promisesAplusTests(adapter, function (err) { 
      // All done; output is in the console. 
      // Or check `err` for number of failures: 
      if (err) { 
       console.log(err); 
      } 
     }); 
    } 
}); 

此時你需要通過NPM安裝的依賴並運行該文件。請注意,使用jquery 需要一個適當的文件(因此需要jsdom)。

或者你也可以採取簡單的方法:

  • 混帳克隆的jQuery回購
  • CD jQuery的目錄
  • NPM安裝
  • 永遠等待它完成,忽略所有警告
  • npm測試 - 運行jquery單元測試(包括A +套件)

在瀏覽器中運行,儘管規範自述文件中的glib註釋將涉及一些工作。您需要使用像browserify這樣的commonJS模塊加載器。 Browserify將填充本地節點斷言庫,我相信文件系統api。摩卡應該在瀏覽器中正常工作。然後上面的代碼應該運行。

如果您想完全避免node.js(而不只是運行測試) ,那麼您將需要做更多工作。

第1步。查找或寫入兼容的斷言庫並將其包含在腳本標記中。

第2步。在腳本標記中加入摩卡。

第3步。爲require,assert,fs等編寫自己的存根。每一個都是一個問題。

第4步。使用適配器代碼。

是否值得做或不做節點是你的呼喚。

+0

您可以包含一個詳細的方法來解決,例如,將單個測試轉換爲能夠在瀏覽器中返回結果?是否需要使用'require'庫?你可以創建一個stacksnippets來演示結果嗎? – guest271314

+0

必要嗎?沒有它不必要*,但另一種方法是編輯測試的源代碼,以使用類似於分配全局變量的東西。您還必須將斷言庫替換爲另一個斷言庫。只需使用browserify。或者移植所有的測試(我可以回答你的其他問題)。 –

+0

基於'browserify' nodejs的?該問題試圖在不使用'node.js'的情況下返回預期結果。可以將browserify用作獨立庫嗎? – guest271314

1

A +兼容Promise和舊jQuery Deferreds之間最大的區別是A + Promises具有適當的錯誤處理。你可以通過運行這個簡單的測試來驗證jQuery 3承諾現在有適當的錯誤處理。

function test(name, $) { 
 
    var $name = $('<td>').text(name); 
 
    var $result = $('<td>'); 
 
    var $row = $('<tr>') 
 
     .append($name) 
 
     .append($result) 
 
     .appendTo('tbody'); 
 
    
 
    var def = $.Deferred(); 
 
    def.rejectWith(new Error("Error")); 
 
    def.then(undefined, function() { 
 
    $result.text('Fail').css('color', 'red'); // This will be overriden by success case 
 
    }).then(function() { 
 
    $result.text('Success').css('color', 'green'); 
 
    }); 
 
} 
 

 
test('jQuery 1', $1); 
 
test('jQuery 2', $2); 
 
test('jQuery 3', $3);
<script src="https://code.jquery.com/jquery-1.12.3.min.js"></script> 
 
<script> 
 
    var $1 = $; 
 
    $.noConflict(); 
 
</script> 
 
<script src="https://code.jquery.com/jquery-2.2.3.min.js"></script> 
 
<script> 
 
    var $2 = $; 
 
    $.noConflict(); 
 
</script> 
 
<script src="https://code.jquery.com/jquery-3.0.0-beta1.js"></script> 
 
<script> 
 
    var $3 = $; 
 
    $.noConflict(); 
 
</script> 
 

 
<table> 
 
    <thead> 
 
    <tr> 
 
     <td>Version</td> 
 
     <td>Result</td> 
 
    </tr> 
 
    </thead> 
 
    <tbody> 
 
    </tbody> 
 
</table>

+1

如何在瀏覽器中運行Compliance Test Suite? – guest271314

+0

@ guest271314對不起,我誤解了這個問題。你問的問題的答案是「你將不得不編寫一個適配器才能滿足測試套件期望的接口。」我會改變我的答案。 – acbabis

0

How to run the tests in browser, without nodejs to verify ?


Note, have not yet achieved running tests in browser without nodejs. @JaredSmith 's assistance was essential to running tests at all using nodejs.

已經能夠實現利用和Unofficial mirror of the WebKit SVN repository -> master -> LayoutTests -> js -> promises-tests調整文件的問題說明的要求。

試圖儘可能地改變。更新promises-tests版本到2.1.12.0.5出現在webkit回購;包括.js來自其他回購部分的文件到resources目錄;在adapter.js內添加jQuery-3.0.0-pre源;評論promises-in-workers.html哪些測試PromiseWorker環境jQuery()初始化檢查document功能。

通過下載jquery-3.0.0-promises-tests-in-browser運行測試,在瀏覽器中打開index.html

$(document).ready(function() { 
    $("title, h3") 
    .html("jQuery version " + jQuery().jquery + " Promises/A+ Compliance Tests"); 
    var tests = ["promises-tests-2-1-2.html" 
       , "promises-tests-2-1-3.html" 
       , "promises-tests-2-2-1.html" 
       , "promises-tests-2-2-2.html" 
       , "promises-tests-2-2-3.html" 
       , "promises-tests-2-2-4.html" 
       , "promises-tests-2-2-5.html" 
       , "promises-tests-2-2-6.html" 
       , "promises-tests-2-2-7.html" 
       , "promises-tests-2-3-1.html" 
       , "promises-tests-2-3-2.html" 
       , "promises-tests-2-3-3.html" 
       , "promises-tests-2-3-4.html" 
       , "Promise-types.html" 
       // TODO test `jQuery.Deferred()` in `Worker` 
       // see http://stackoverflow.com/q/10491448/ 
       /* , "promises-in-workers.html" */ ]; 
    $.each(tests, function(index, test) { 
    $("<iframe>", { 
     width: "100%" 
     }).on("load", function(e) { 
     setTimeout(function() { 
      e.target.height = e.target.contentWindow 
          .document.body.scrollHeight * 1.1 + "px"; 
     }, 2000 * (1 + index)); 
     // if (index === tests.length - 1) { 
     // $(e.target.contentWindow.document.body) 
     // .prepend("<span style='font-family:Times New Roman'>" 
     //    + "Promises in Workers Test</span>") 
     // } 
     }) 
     .attr("src", test).appendTo("body") 
    }) 
}); 

Demonstration