JavaScript 前端測試覆蓋率
JavaScript 前端測試覆蓋率
如果您還沒建置好 JavaScript 前端測試環境,請移步至建立 JavaScript 單元測試環境
Blanket.js是一款易於安裝,易於配置且易於使用的 JavaScript 程式覆蓋率庫,可在瀏覽器中和與 Node.js一起使用。
Blanket.js是一款易於安裝,易於配置且易於使用的 JavaScript 程式覆蓋率庫,可在瀏覽器中和與 Node.js一起使用。
除此之外也是唯一找到可以與 Mocha 前端測試搭配的套件。
Blanket.js 的使用非常簡單,只需要兩個步驟即可。
一、將 Blanket.js 添加到您的 HTML 測試文件:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script src="...path/blanket.js" data-cover-adapter="...path/mocha-blanket.js"></script> |
__注意__
mocha-blanket.js 是讓 blanket.js 可以在 mocha 測試之後產生覆蓋率的匹配器。
二、在受測物件的檔案後方加上 data-cover 屬性
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script src="../../assets/js/taskStorage.js" data-cover></script> <!-- 添加 data-cover 屬性 --> |
test_taskStorage.html 配置完成的樣子
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<title>Cow tests</title> | |
<link rel="stylesheet" media="all" href="../../node_modules/mocha/mocha.css"> | |
</head> | |
<body> | |
<div id="mocha"><p><a href=".">Index</a></p></div> | |
<div id="messages"></div> | |
<div id="fixtures"></div> | |
<script src="../../node_modules/mocha/mocha.js"></script> | |
<script src="blanket.min.js" data-cover-flags="branchTracking" data-cover-adapter="mocha-blanket.js"></script> | |
<script src="../../node_modules/chai/chai.js"></script> | |
<script src="../../assets/js/taskStorage.js" data-cover></script> | |
<script>mocha.setup('bdd')</script> | |
<script src="test_taskStorage.js"></script> | |
<script>mocha.run();</script> | |
</body> | |
</html> |
配置完成,執行 test_taskStorage.html 就會自動產生測試涵蓋率的區塊:
參考文章:
前端覆蓋率套件 Blanket.js 已經失修。有機會要找新的套件取代。
留言
張貼留言