2017年1月18日 星期三

[nodejs] 最簡單的方法使用 nodetest for your node js code

How to use nodeunit for your nodejs code
GitHub: Download
Google Doc: This Document.

Install

npm install nodeunit

Syntax

test.ok(測試, [message]);

Run

nodeunit example.js
example.js
module.exports = {
   // 第一個測試
   'Test 1' : function(test) {
       test.expect(1);    // 一個 tester 執行
       test.ok(true, "This shouldn't fail");  // Assertion 1:  PASS
       test.done();       // 這個測試做完了
   },

   // 第二個測試
   'Test 2' : function(test) {
       test.expect(2);    // 個 tester 分別執行
       test.ok(1 === 1, "This shouldn't fail");    // Assertion 1: PASS
       test.ok(false, "This should fail");         // Assertion 2: Fail
       test.done();
   }
};

Verification

Reference