Skip to content

assert

assert เป็นโมดูลในตัวของ Node.js ที่ใช้สำหรับการตรวจสอบเงื่อนไข (assertions) ในระหว่างการพัฒนา

import { strict, ok, deepStrictEqual, throws, rejects, doesNotThrow } from 'node:assert';

APIคำอธิบายลักษณะคำอธิบายเพิ่มเติม
strict()ตรวจสอบเงื่อนไขทั่วไปSynchronousใช้ตรวจสอบค่าที่คาดหวัง (เหมือน assert())
ok()ตรวจสอบเงื่อนไขทั่วไปSynchronousใช้ตรวจสอบค่าที่คาดหวัง (เหมือน assert())
strictEqual()ตรวจสอบความเท่ากันแบบ strictSynchronousใช้ === สำหรับการเปรียบเทียบ
deepStrictEqual()ตรวจสอบ object/array แบบลึกSynchronousตรวจสอบโครงสร้างและค่าทั้งหมดแบบ recursive
throws()ตรวจสอบว่าฟังก์ชัน throw errorSynchronousใช้ทดสอบ error cases
rejects()ตรวจสอบว่า Promise rejectAsynchronousใช้ทดสอบ async error cases
doesNotThrow()ตรวจสอบว่าฟังก์ชันไม่ throw errorSynchronousใช้ทดสอบว่าฟังก์ชันทำงานปกติ

ตัวอย่าง

การใช้ assert พื้นฐาน

js
import { ok, strictEqual } from "assert";

// ตรวจสอบค่าพื้นฐาน
const x = 5;
ok(x === 5, "ค่า x ควรเป็น 5");

// ตรวจสอบความเท่ากันแบบ strict
strictEqual(typeof x, "number", "x ควรเป็นตัวเลข");

การตรวจสอบ object และ array

js
import { deepStrictEqual } from "assert";

const obj1 = { a: 1, b: { c: 2 } };
const obj2 = { a: 1, b: { c: 2 } };

// ตรวจสอบ object แบบลึก
deepStrictEqual(obj1, obj2, "object ควรมีโครงสร้างและค่าเหมือนกัน");

// ตรวจสอบ array
const arr1 = [1, 2, 3];
const arr2 = [1, 2, 3];
deepStrictEqual(arr1, arr2, "array ควรมีค่าเหมือนกัน");

การทดสอบ error cases

js
import { doesNotThrow, throws } from "assert";

function divide(a, b) {
  if (b === 0) throw new Error("หารด้วยศูนย์ไม่ได้");
  return a / b;
}

// ตรวจสอบว่าฟังก์ชัน throw error ตามที่คาดหวัง
throws(
  () => divide(10, 0),
  {
    name: "Error",
    message: "หารด้วยศูนย์ไม่ได้",
  },
  "ควร throw error เมื่อหารด้วยศูนย์",
);

// ตรวจสอบว่าฟังก์ชันทำงานปกติ
doesNotThrow(
  () => divide(10, 2),
  "ไม่ควร throw error เมื่อหารด้วยจำนวนอื่น",
);

การทดสอบ async/await

js
import { rejects } from "assert";

async function asyncDivide(a, b) {
  if (b === 0) throw new Error("หารด้วยศูนย์ไม่ได้");
  return a / b;
}

// ตรวจสอบว่า Promise reject ตามที่คาดหวัง
await rejects(
  asyncDivide(10, 0),
  {
    name: "Error",
    message: "หารด้วยศูนย์ไม่ได้",
  },
  "ควร reject เมื่อหารด้วยศูนย์",
);

// ตรวจสอบว่า Promise resolve ค่าที่ถูกต้อง
const result = await asyncDivide(10, 2);
strictEqual(result, 5, "ผลลัพธ์ควรเป็น 5");

การใช้ assert กับ custom error messages

js
import { doesNotThrow, throws } from "assert";

function getUser(id) {
  if (!id) throw new Error("ต้องระบุ ID");
  return { id, name: "John Doe" };
}

// ใช้ custom message
doesNotThrow(
  () => getUser(1),
  "getUser ควรทำงานปกติเมื่อมี ID",
);

throws(
  () => getUser(),
  /ต้องระบุ ID/,
  "getUser ควร throw error เมื่อไม่มี ID",
);

การตรวจสอบ type

js
import { strictEqual } from "assert";

function processValue(value) {
  strictEqual(typeof value, "string", "ค่าต้องเป็น string");
  return value.toUpperCase();
}

// ใช้งาน
const upper = processValue("hello");
strictEqual(upper, "HELLO", "ควรแปลงเป็นตัวใหญ่");

// จะ throw error หากค่าไม่ใช่ string
throws(
  () => processValue(123),
  /ค่าต้องเป็น string/,
  "ควร throw error เมื่อค่าไม่ใช่ string",
);