Mocha (JavaScript框架)

MochaJavaScript單元測試框架,能在Node.js瀏覽器環境運行。[4]可以搭配大多數斷言庫使用,例如Chai。通過添加和運行測試用例,從而保證代碼質量。

Mocha
當前版本
  • 10.4.0 (2024年3月26日;穩定版本)[1]
編輯維基數據鏈接
源代碼庫https://github.com/mochajs/mocha
編程語言JavaScript
類型JavaScript函式庫
許可協議MIT許可證[2]
網站https://mochajs.org/

斷言庫 編輯

Mocha 與大多數 javascript 斷言庫可以使用 [1]頁面存檔備份,存於網際網路檔案館),包括:

  • should.js
  • express.js
  • chai
  • better-assert
  • unexpected

使用和示例[5] 編輯

npm install --global mocha #全局安装
npm install --save-dev mocha #局部安装
mkdir test/unit #新建测试文件夹
code test/unit/test.js #使用VSCode打开文件,当然你也可以使用别的编辑器
編寫測試用例 [2]頁面存檔備份,存於網際網路檔案館 編輯
var assert = require('assert');
describe('Array', function () {
  describe('#indexOf()', function () {
    it('should return -1 when the value is not present', function () {
      assert.equal([1, 2, 3].indexOf(4), -1);
    });
  });
});

外部連結 編輯

參考資料 編輯

  1. ^ Release 10.4.0. 2024年3月26日 [2024年4月23日]. 
  2. ^ LICENSE. [2020-07-31]. (原始內容存檔於2020-07-31) (英語). 
  3. ^ Releases · mochajs/mocha. github.com. [2020-07-31]. (原始內容存檔於2020-11-12) (英語). 
  4. ^ Mocha. [2020-07-31]. (原始內容存檔於2020-12-07) (英語). 
  5. ^ Mocha - the fun, simple, flexible JavaScript test framework. mochajs.org. [2022-03-18]. (原始內容存檔於2020-12-07).