Skip to content

Japa Testing Library

  • เฟรมเวิร์กสำหรับทดสอบ Node.js ที่น้ำหนักเบา
  • ใช้งานง่าย
  • รองรับทั้ง unit testing และ integration testing
  • มี assertion library ในตัว

Features

bash
bun add @japa/runner
bash
npm install @japa/runner
bash
yarn add @japa/runner
bash
pnpm add @japa/runner

Installation

Basic Test Example

ts
// Import test runner
import { test } from "@japa/runner";

// Simple test case
test("add two numbers", ({ assert }) => {
  assert.equal(1 + 1, 2);
});

Grouping Tests

ts
test.group("Math operations", () => {
  test("addition", ({ assert }) => {
    assert.equal(2 + 2, 4);
  });

  test("subtraction", ({ assert }) => {
    assert.equal(5 - 3, 2);
  });
});

Lifecycle Hooks

ts
test.group("Database", (group) => {
  group.each.setup(async () => {
    // Setup database connection
    await Database.start();
    return () => Database.stop();
  });

  test("create user", async ({ assert }) => {
    const user = await User.create({ name: "John" });
    assert.exists(user.id);
  });
});

APIs

Test Structure

ts
test("test title", (context) => {
  // Test logic
  context.assert.equal(1, 1);
});

Assertions

ts
assert.equal(actual, expected); // เปรียบเทียบค่า
assert.isTrue(value); // ตรวจสอบค่าเป็น true
assert.isFalse(value); // ตรวจสอบค่าเป็น false
assert.isNull(value); // ตรวจสอบค่าเป็น null
assert.isUndefined(value); // ตรวจสอบค่าเป็น undefined

Lifecycle Hooks

ts
test.group("group name", (group) => {
  group.each.setup(() => {/* setup */});
  group.each.teardown(() => {/* cleanup */});

  test("test case", () => {/* test */});
});

Configuration

ts
// japa.config.ts
export default {
  files: ["tests/**/*.spec.ts"],
  timeout: 2000,
  retries: 2,
};

Comparison

FeatureJapa 🏗️Vitest ⚡Playwright 🌐
TypeUnit/IntegrationUnit/ComponentE2E/Browser
SpeedFast 🚀Very Fast ⚡⚡Slower 🐢
Assertions✅ Built-in✅ Built-in❌ Requires additional
Browser❌ No❌ No✅ Yes
Parallel✅ Yes✅ Yes✅ Yes
LanguageJavaScript/TS 💻JavaScript/TS 💻Multiple 🌍
Best ForAPI/Backend tests 🛠️Frontend components 🖥️Full browser tests 🌐
  • 🏗️ Japa: Ideal for Node.js backend/API testing / เหมาะสำหรับทดสอบ backend และ API ของ Node.js
  • Vitest: Best for Vue/React component tests / เหมาะที่สุดสำหรับทดสอบ component ของ Vue/React
  • 🌐 Playwright: Perfect for full browser automation and E2E tests / ดีที่สุดสำหรับการทดสอบแบบ end-to-end และ automation บน browser

Last updated: