Thuta Learning
Next.js
AdvancedWeb Developmentအလယ်အလတ်

Testing နှင့် Debugging Workflow

Next.jsသင်ခန်းစာ 2240 မိနစ်

ဒီခန်းပြီးရင် ဘာတတ်သွားမလဲ

  • Bug အမျိုးအစားတစ်ခုချင်းစီကို ဘယ် test layer က ဖမ်းနိုင်လဲ ခွဲခြားနိုင်ဖို့
  • Server Action ထဲက rule တွေကို plain function အဖြစ် ခွဲထုတ်ပြီး Vitest နဲ့ unit test ရေးနိုင်ဖို့
  • Client Component ကို Testing Library နဲ့၊ async Server Component ပါတဲ့ flow ကို Playwright နဲ့ စစ်နိုင်ဖို့
  • Server code ရဲ့ log နဲ့ client code ရဲ့ log ကို မှန်ကန်တဲ့ နေရာမှာ ရှာပြီး debugger ချိတ်နိုင်ဖို့

ရိုးရိုးလေး ရှင်းပြမယ်

ရိုးရိုးလေး ရှင်းရရင်

အိမ်တစ်လုံး ဆောက်ပြီးရင် စစ်ဆေးရေးမှူးက နည်းလမ်း တစ်မျိုးတည်းနဲ့ မစစ်ပါဘူး။ အုတ်တစ်ချပ်ချင်းကို ခိုင်လားစစ်တယ်၊ အခန်းတစ်ခန်းချင်းရဲ့ တံခါးနဲ့ မီးခလုတ်ကို စမ်းတယ်၊ နောက်ဆုံးမှ ဧည့်သည်တစ်ယောက်လို အိမ်ရှေ့ကနေ မီးဖိုချောင်အထိ လမ်းလျှောက်ကြည့်ပါတယ်။

Next.js app မှာလည်း ဒီအတိုင်းပါပဲ။ TypeScript က data ပုံသဏ္ဌာန် မှားတာကို run မလုပ်ခင်ကတည်းက ဖမ်းပါတယ်။ Unit test က validation လို rule သေးသေးလေးတွေကို စက္ကန့်ပိုင်းအတွင်း စစ်ပါတယ်။ Component test က button နဲ့ form ကို user လို နှိပ်ကြည့်ပြီး end-to-end test ကတော့ browser အစစ်နဲ့ "note အသစ်ရေးပြီး သိမ်းတယ်" လို journey တစ်ခုလုံးကို လျှောက်ကြည့်ပါတယ်။

Problem → Why → Solution

ပြဿနာ: render လုပ်လို့ မရတဲ့ test နဲ့ skip လုပ်ထားတဲ့ bug

Myanmar Notes ရဲ့ /notes page က database ကနေ note တွေကို await နဲ့ ယူတဲ့ async Server Component ပါ။ Developer တစ်ယောက်က React app မှာ လုပ်ခဲ့သလို Testing Library နဲ့ page ကို တိုက်ရိုက် render လုပ်တဲ့ test ရေးလိုက်ပါတယ်။ Test က ထူးဆန်းတဲ့ error နဲ့ fail ဖြစ်ပါတယ်၊ ဒါမှမဟုတ် database connection မရှိလို့ ရပ်သွားပါတယ်။

အချိန်မရှိလို့ test.skip နဲ့ ပိတ်ထားလိုက်ပါတယ်။ နှစ်ပတ်အကြာမှာ title ကို trim မလုပ်တဲ့ bug တစ်ခု production ကို ရောက်သွားပါတယ်၊ ဘာလို့လဲဆိုတော့ အဲဒီ rule ကို စစ်ဖို့ ကျန်ခဲ့တဲ့ test တစ်ခုတည်းက skip ဖြစ်နေလို့ပါ။

app/notes/page.test.tsx
import { render, screen } from "@testing-library/react";
import { expect, test } from "vitest";
import NotesPage from "./page"; // async Server Component that queries the DB

test.skip("lists notes", () => {
  // An async component returns a Promise, which this render cannot handle
  render(<NotesPage />);
  expect(screen.getByText("Mohinga recipe")).toBeTruthy();
});

ဘာကြောင့် ဒီနည်းက အလုပ်မဖြစ်တာလဲ

Next.js ရဲ့ testing guide အရ Vitest လို tool တွေက async Server Component ကို လက်ရှိ မထောက်ပံ့သေးပါဘူး။ Page တစ်ခုထဲမှာ data ယူတာ၊ rule စစ်တာ၊ UI ဆွဲတာ အကုန် ရောနေတော့ test လုပ်ရ ခက်ပါတယ်။ ဖြေရှင်းနည်းက rule တွေကို plain function အဖြစ် ခွဲထုတ်ပြီး unit test လုပ်တာ၊ page တစ်ခုလုံးကိုတော့ browser အစစ်နဲ့ E2E test လုပ်တာပါ။

Core concept: bug တစ်မျိုးချင်းကို layer တစ်ခုစီက ဖမ်းပါတယ်

Test layer တွေက တစ်ခုနဲ့တစ်ခု အစားထိုးလို့ မရပါဘူး၊ ဖမ်းနိုင်တဲ့ bug အမျိုးအစား မတူကြလို့ပါ။ အောက်ကို ဆင်းလေ မြန်လေ၊ စျေးပေါလေ ဖြစ်ပြီး အပေါ်ကို တက်လေ user မြင်တဲ့ အတိုင်းနဲ့ ပိုနီးလေ၊ နှေးလေ ဖြစ်ပါတယ်။ ဒါကြောင့် test အများစုကို အောက်ပိုင်း layer တွေမှာ ထားပြီး အရေးကြီးတဲ့ journey အနည်းငယ်ကိုပဲ E2E နဲ့ စစ်ကြပါတယ်။

Layerဖမ်းနိုင်တဲ့ bugNext.js မှာ tool
Type checkProp မှားပို့တာ၊ undefined ဖြစ်နိုင်တာကို မေ့တာtsc --noEmit (next build လည်း စစ်ပါတယ်)
UnitValidation၊ format၊ permission rule မှားတာVitest ဒါမှမဟုတ် Jest
ComponentButton မနှိပ်ရ၊ label ပျောက်၊ state မပြောင်းTesting Library + jsdom
End-to-endRoute၊ server၊ database ချိတ်ဆက်မှု ပျက်တာPlaywright (ဒါမှမဟုတ် Cypress)

Next.js မှာ အဓိက စည်းမျဉ်းက component အမျိုးအစားပေါ် မူတည်ပါတယ်။ Client Component နဲ့ synchronous Server Component ကို Vitest နဲ့ Testing Library သုံးပြီး jsdom ထဲမှာ render လုပ်လို့ ရပါတယ်။ Async Server Component ကိုတော့ unit test tool တွေက မထောက်ပံ့သေးလို့ E2E test နဲ့ စစ်ဖို့ Next.js docs က အကြံပြုပါတယ်။

ဒါက "async page တွေကို test မလုပ်နဲ့" လို့ ဆိုလိုတာ မဟုတ်ပါဘူး။ Page ထဲက စဉ်းစားရတဲ့ အပိုင်း (title စစ်တာ၊ search param ကို page number ပြောင်းတာ၊ ဘယ်သူ edit လုပ်ခွင့်ရှိလဲ ဆုံးဖြတ်တာ) ကို lib/ folder ထဲက plain function တွေအဖြစ် ခွဲထုတ်ပါ။ Page နဲ့ Server Action က အဲဒီ function တွေကို ခေါ်ရုံပဲ ဖြစ်သွားပြီး rule တွေကို database မလိုဘဲ millisecond ပိုင်းအတွင်း test လုပ်လို့ ရလာပါတယ်။

Debugging ကလည်း layer တစ်ခုပါ

Test fail ဖြစ်ရင် ပထမဆုံး လုပ်ရမှာက bug ကို ထပ်ခါထပ်ခါ ဖြစ်အောင် ပြန်လုပ်နိုင်ဖို့ပါ။ Expected result၊ actual result၊ အဆင့်တွေ၊ environment (browser၊ Node version၊ dev လား production build လား) ကို ရေးမှတ်ထားရင် bug ကို ပြန်ရှာရတာ အများကြီး လွယ်ပါတယ်။ ပြီးမှ ဒီ bug ကို ဖမ်းမယ့် test ကို အရင်ရေးပြီး fix လုပ်ပါ။

Mental model: စစ်ဆေးရေး အလွှာလေးထပ်
  1. Type check က ပုံသဏ္ဌာန် မှားတာကို ဖမ်း
  2. Unit test က rule တွေကို ချုပ်ထား
  3. Component test က user လို နှိပ်ကြည့်
  4. E2E က journey အစစ်ကို လျှောက်

Bug တစ်ခုကို ဖမ်းနိုင်တဲ့ အောက်ဆုံး layer မှာ test ရေးပါ။ အဲဒီ layer က အမြန်ဆုံးနဲ့ ခိုင်မာဆုံး ဖြစ်လေ့ရှိပါတယ်။

အခြေခံ ပုံစံ: Vitest unit test

typescript
import { describe, expect, it } from "vitest";
import { functionUnderTest } from "./module";

describe("functionUnderTest", () => {
  it("describes one behaviour in plain words", () => {
    expect(functionUnderTest(someInput)).toEqual(expectedValue);
  });
});

describe က test တွေကို အုပ်စုဖွဲ့ပြီး it တစ်ခုစီက behaviour တစ်ခုကို စစ်ပါတယ်။ expect(actual) ပြီးရင် toEqual (object တွေကို value အလိုက် နှိုင်းယှဉ်) ဒါမှမဟုတ် toBe (တိတိကျကျ တူညီမှု) လို matcher ကို ဆက်ရေးပါတယ်။

ဥပမာ: Server Action ထဲက title rule ကို test လုပ်ခြင်း

Note ဖန်တီးတဲ့ Server Action ထဲမှာ ရှိခဲ့တဲ့ title စစ်တဲ့ rule ကို validateTitle ဆိုတဲ့ plain function အဖြစ် ခွဲထုတ်ထားပါတယ်။ Server Action က ဒီ function ကို ခေါ်ရုံပဲ ဖြစ်လို့ rule ကို database မပါဘဲ test လုပ်လို့ ရပါပြီ။

Project အစစ်မှာ test file က vitest ကနေ describe၊ it၊ expect ကို import လုပ်ပါတယ်။ ဒီ lesson ရဲ့ code ကို install ဘာမှ မလုပ်ဘဲ npx tsx နဲ့ run လို့ ရအောင် အဲဒီ function သုံးခုကို လိုင်းအနည်းငယ်နဲ့ အတုလုပ်ထားပါတယ်။ Test body တွေကတော့ Vitest မှာ ရေးမယ့်အတိုင်း တစ်ထပ်တည်းပါ။

validate-title.test.ts
import { deepStrictEqual } from "node:assert/strict";

// lib/validate-title.ts: the rule, pulled out of the Server Action
export type TitleResult =
  | { ok: true; title: string }
  | { ok: false; error: string };

export function validateTitle(raw: string): TitleResult {
  const title = raw.trim();
  if (title.length === 0) return { ok: false, error: "Title is required" };
  if (title.length < 3) return { ok: false, error: "Title is too short" };
  if (title.length > 80) return { ok: false, error: "Title is too long" };
  return { ok: true, title };
}

// In a project the test file starts with:
//   import { describe, expect, it } from "vitest";
// This small stand-in lets the file run with plain tsx instead.
const describe = (name: string, fn: () => void) => { console.log(name); fn(); };
const it = (name: string, fn: () => void) => {
  try { fn(); console.log(`  ✓ ${name}`); }
  catch (err) { console.log(`  ✗ ${name}: ${(err as Error).message}`); }
};
const expect = (actual: unknown) => ({
  toEqual: (expected: unknown) => deepStrictEqual(actual, expected),
});

describe("validateTitle", () => {
  it("rejects a title that is too short", () => {
    expect(validateTitle("Hi")).toEqual({ ok: false, error: "Title is too short" });
  });

  it("treats a title of only spaces as missing", () => {
    expect(validateTitle("   ")).toEqual({ ok: false, error: "Title is required" });
  });

  it("trims and accepts a valid title", () => {
    const result = validateTitle("  Next.js Notes ");
    expect(result).toEqual({ ok: true, title: "Next.js Notes" });
  });

  it("rejects a title longer than 80 characters", () => {
    const tooLong = "x".repeat(81);
    expect(validateTitle(tooLong)).toEqual({ ok: false, error: "Title is too long" });
  });
});

Code ကို တစ်ပိုင်းချင်း ရှင်းပြမယ်

  1. A result type instead of a thrown error| { ok: false; error: string };

    validateTitle က error ကို throw မလုပ်ဘဲ ok နဲ့ error ပါတဲ့ object ကို ပြန်ပေးပါတယ်။ Server Action က ဒီ object ကို form state အဖြစ် တိုက်ရိုက် ပြန်ပို့နိုင်ပြီး test ကလည်း toEqual နဲ့ ရှင်းရှင်းလင်းလင်း စစ်နိုင်ပါတယ်။

  2. Trim before measuringconst title = raw.trim();

    Space သုံးခုပဲ ရိုက်ထားတဲ့ title ကို length 3 လို့ မယူမိအောင် အရင် trim လုပ်ပါတယ်။ "only spaces" test က ဒီ edge case ကို ကာကွယ်ထားတာပါ။

  3. The stand-in for Vitestconst expect = (actual: unknown) => ({

    Vitest ရဲ့ expect(...).toEqual(...) လိုပဲ deepStrictEqual နဲ့ value အလိုက် နှိုင်းယှဉ်ပါတယ်။ Project အစစ်မှာ ဒီလိုင်းတွေကို ဖျက်ပြီး vitest ကနေ import လုပ်ရုံပါပဲ။

  4. One behaviour per testit("trims and accepts a valid title", () => {

    Test နာမည်က function ဘယ်လို အလုပ်လုပ်လဲ မဟုတ်ဘဲ ဘာလုပ်ရမလဲ (behaviour) ကို ဖော်ပြပါတယ်။ Fail ဖြစ်ရင် နာမည်ကို ဖတ်ရုံနဲ့ ဘာပျက်လဲ သိနိုင်ပါတယ်။

  5. Boundary valuesconst tooLong = "x".repeat(81);

    Bug အများစုက နယ်နိမိတ်နားမှာ ပုန်းနေတတ်ပါတယ်။ 80 လုံးက ရပြီး 81 လုံးက မရဘူးဆိုတာကို စစ်ထားရင် နောက်တစ်ယောက်က < နဲ့ <= ကို မှားပြောင်းမိတာကို ဖမ်းနိုင်ပါတယ်။

You should see
validateTitle
  ✓ rejects a title that is too short
  ✓ treats a title of only spaces as missing
  ✓ trims and accepts a valid title
  ✓ rejects a title longer than 80 characters

Next.js project မှာ Vitest ထည့်ခြင်း

၁။ Package တွေ install လုပ်ပါ

Next.js docs က Vitest၊ React plugin၊ jsdom၊ Testing Library နဲ့ tsconfig path alias (@/...) ကို နားလည်စေမယ့် plugin ကို dev dependency အဖြစ် ထည့်ဖို့ ပြထားပါတယ်။

၂။ vitest.config.mts ဖန်တီးပါ

Project root မှာ config file ထားပြီး test environment ကို jsdom လို့ သတ်မှတ်ပါ။ Pure function test တွေသာ ရှိရင် jsdom မလိုဘဲ node environment နဲ့လည်း ရပါတယ်။

၃။ Script ထည့်ပြီး run ပါ

package.json ထဲမှာ "test": "vitest" ထည့်ပါ။ pnpm test က watch mode နဲ့ run ပြီး CI မှာတော့ တစ်ကြိမ်တည်း run ဖို့ vitest run ကို သုံးပါ။

bash
pnpm add -D vitest @vitejs/plugin-react jsdom @testing-library/react \
  @testing-library/dom vite-tsconfig-paths
vitest.config.mts
import { defineConfig } from "vitest/config";
import react from "@vitejs/plugin-react";
import tsconfigPaths from "vite-tsconfig-paths";

export default defineConfig({
  plugins: [tsconfigPaths(), react()],
  test: { environment: "jsdom" },
});

Client Component ကို Testing Library နဲ့ စစ်ခြင်း

"use client" ပါတဲ့ NoteForm လို component က browser ထဲမှာ အလုပ်လုပ်တာမို့ jsdom ထဲမှာ render လုပ်ပြီး user လို စာရိုက်၊ button နှိပ်ကြည့်လို့ ရပါတယ်။ Server Action ကို prop အဖြစ် လက်ခံတဲ့ပုံစံနဲ့ ရေးထားရင် test ထဲမှာ vi.fn() mock တစ်ခုကို ထည့်ပေးလိုက်ရုံပါပဲ။ Element တွေကို CSS class နဲ့ မရှာဘဲ label နဲ့ role နဲ့ ရှာတာက user မြင်တဲ့အတိုင်း စစ်တာ ဖြစ်ပါတယ်။

app/notes/new/note-form.test.tsx
import { fireEvent, render, screen } from "@testing-library/react";
import { expect, test, vi } from "vitest";
import { NoteForm } from "./note-form";

test("shows the validation message for a short title", async () => {
  const save = vi.fn();
  render(<NoteForm onSave={save} />);

  fireEvent.change(screen.getByLabelText("Title"), { target: { value: "Hi" } });
  fireEvent.click(screen.getByRole("button", { name: "Save note" }));

  expect(await screen.findByRole("alert")).toHaveProperty(
    "textContent", "Title is too short");
  expect(save).not.toHaveBeenCalled();
});

Playwright နဲ့ End-to-end test

E2E test က browser အစစ်ကို ဖွင့်ပြီး app ကို URL နဲ့ ဝင်ပါတယ်။ ဒါကြောင့် async Server Component၊ Server Action၊ redirect၊ database အကုန်လုံး တကယ် ချိတ်ဆက်ပြီး အလုပ်လုပ်လား စစ်နိုင်တဲ့ တစ်ခုတည်းသော layer ပါ။ Next.js docs က E2E test တွေကို next dev မဟုတ်ဘဲ next build ပြီး next start နဲ့ run ထားတဲ့ production build ပေါ်မှာ run ဖို့ အကြံပြုပါတယ်၊ user တွေ တကယ်ရမယ့် behaviour နဲ့ ပိုနီးလို့ပါ။

e2e/create-note.spec.ts
import { expect, test } from "@playwright/test";

test("a signed-in user can create a note", async ({ page }) => {
  await page.goto("/dashboard/notes/new");
  await page.getByLabel("Title").fill("Shan noodles");
  await page.getByRole("button", { name: "Save note" }).click();

  await expect(page).toHaveURL(/\/dashboard\/notes\/[\w-]+$/);
  await expect(page.getByRole("heading", { name: "Shan noodles" })).toBeVisible();
});

Playwright ကို server ကိုယ်တိုင် စခိုင်းပါ

playwright.config.ts ထဲက webServer option မှာ command (ဥပမာ pnpm build && pnpm start) နဲ့ url ကို ထည့်ထားရင် test မစခင် app ကို စပြီး ပြီးရင် ပိတ်ပေးပါတယ်။ Login လိုတဲ့ flow တွေအတွက် test-only user နဲ့ database ကို သီးသန့် သုံးပါ။

Debugging: log တွေ ဘယ်မှာ ပေါ်လဲ

Next.js မှာ code တစ်ချို့က server ပေါ်မှာ run ပြီး တစ်ချို့က browser ထဲမှာ run တာမို့ console.log ရဲ့ output က နေရာ နှစ်ခုမှာ ပေါ်ပါတယ်။ Browser console ထဲမှာ Server Action ရဲ့ log ကို ရှာနေရင် ဘယ်တော့မှ တွေ့မှာ မဟုတ်ပါဘူး။ Development မှာ Next.js version အသစ်တွေက Server Component log တွေကို browser console ထဲမှာ Server label နဲ့ ပြန်ပြပေးတတ်ပေမယ့် မူလ နေရာက terminal ပါ။

Code ရှိတဲ့ နေရာLog ကို ရှာရမယ့် နေရာ
Server Component၊ Server Action၊ Route Handlernext dev run ထားတဲ့ terminal (production မှာ hosting log)
Client Component ရဲ့ event handler နဲ့ effectBrowser DevTools ရဲ့ Console tab
Request တွေ၊ response statusDevTools ရဲ့ Network tab

console.log ထက် ပိုလိုရင် server code ကို Node inspector နဲ့ debug လုပ်နိုင်ပါတယ်။ NODE_OPTIONS='--inspect' next dev နဲ့ စပြီး Chrome မှာ chrome://inspect ကို ဖွင့်ကာ breakpoint ထားနိုင်ပါတယ် (VS Code ရဲ့ debugger နဲ့လည်း ချိတ်လို့ ရပါတယ်)။ Production မှာ error ဖြစ်ရင် user ကို error message အပြည့် မပြဘဲ digest ဆိုတဲ့ ID ပဲ ပြပါတယ်။ အဲဒီ digest ကို server log ထဲမှာ ရှာရင် အပြည့်အစုံ stack trace ကို တွေ့ပါမယ်။

လက်တွေ့ project တွေမှာ

လက်တွေ့ project တွေမှာ

Team အများစုမှာ pull request တိုင်း CI က type check၊ lint နဲ့ unit/component test ကို minute အနည်းငယ်အတွင်း run ပါတယ်။ ဒီ check တွေ မအောင်ရင် merge လုပ်ခွင့် မပေးပါဘူး။ E2E test တွေက ပိုနှေးလို့ "sign in၊ note ဖန်တီး၊ note ဖျက်" လို အရေးကြီးဆုံး journey ငါးခုလောက်ကိုပဲ ရွေးပြီး production build ဒါမှမဟုတ် preview deployment ပေါ်မှာ run ကြပါတယ်။

Bug report ရောက်လာရင် ကောင်းတဲ့ team တွေက fix မလုပ်ခင် အဲဒီ bug ကို ပြန်ဖြစ်စေတဲ့ fail test တစ်ခု အရင်ရေးပါတယ်။ Test က fail ဖြစ်တာ မြင်ရမှ fix လုပ်ပြီး pass ဖြစ်တာ စစ်ပါတယ်။ ဒါမှ တစ်နေ့ refactor လုပ်ရင်း bug အဟောင်း ပြန်ဝင်လာတာ (regression) ကို test က ချက်ချင်း သတိပေးနိုင်ပါတယ်။ လတွေကြာလာတာနဲ့အမျှ ဒီလို bug ပြန်ဖြစ်စေတဲ့ test သေးသေးလေးတွေက test suite ထဲမှာ အတန်ဖိုးအရှိဆုံး အပိုင်း ဖြစ်လာပါတယ်။

အမှားများ

အမှားများတဲ့ အချက်တွေ

  • Async Server Component ကို Vitest ထဲမှာ တိုက်ရိုက် render လုပ်ဖို့ ကြိုးစားပြီး Next.js ရဲ့ module တဝက်ကို mock လုပ်တာ။ Rule တွေကို plain function အဖြစ် ခွဲထုတ်ပြီး page တစ်ခုလုံးကို E2E နဲ့ စစ်တာက ပိုရိုးရှင်းပြီး ပိုယုံကြည်ရပါတယ်။
  • Implementation detail ကို စစ်တာ: CSS class နဲ့ element ရှာတာ၊ component အတွင်းက state variable ကို စစ်တာ။ Behaviour မပြောင်းဘဲ refactor လုပ်ရုံနဲ့ test ကျိုးပြီး တကယ့် bug ကိုတော့ မဖမ်းနိုင်ပါဘူး။
  • Fail ဖြစ်နေတဲ့ test ကို အကြောင်းရင်း မရှာဘဲ test.skip ဒါမှမဟုတ် ဖျက်ပစ်တာ။ Skip လုပ်ထားတဲ့ test ဆိုတာ ဘာကိုမှ မကာကွယ်တဲ့ test ပါ။ တကယ် skip လုပ်ရရင် ticket link နဲ့ comment ရေးထားပါ။
  • E2E test ကို next dev ပေါ်မှာပဲ run တာ။ Dev server က route တွေကို လိုအပ်မှ compile လုပ်လို့ နှေးပြီး timeout ဖြစ်လွယ်သလို production မှာပဲ ပေါ်တဲ့ caching ပြဿနာကိုလည်း မမြင်ရပါဘူး။

Best Practices

အကောင်းဆုံး အလေ့အကျင့်များ

  • Server Action နဲ့ page ထဲက ဆုံးဖြတ်ချက်တွေကို lib/ ထဲက plain function အဖြစ် ထားပါ
  • Test တစ်ခုမှာ behaviour တစ်ခုပဲ စစ်ပြီး နာမည်ကို ဝါကျအပြည့်နဲ့ ရေးပါ
  • E2E ကို အရေးကြီးဆုံး journey တွေအတွက်ပဲ သုံးပြီး production build ပေါ်မှာ run ပါ
  • Bug တိုင်းအတွက် fix မလုပ်ခင် fail ဖြစ်တဲ့ test တစ်ခု အရင်ရေးပါ

Mini challenge

ကိုယ်တိုင် လုပ်ကြည့်ရအောင်: page number ကို test လုပ်ပါ

/notes?page=3 လို URL ကနေ page number ကို ယူတဲ့ parsePage function နဲ့ အဲဒါကို item range အဖြစ် ပြောင်းတဲ့ pageRange function ကို ရေးပြီး normal၊ edge နဲ့ မှားတဲ့ input တွေအတွက် test ရေးပါ။ Next.js မှာ searchParams ရဲ့ value တစ်ခုက string၊ string array ဒါမှမဟုတ် undefined ဖြစ်နိုင်တာကို သတိထားပါ။

  • ?page မပါရင် 1 ကို ပြန်ပေးပါ
  • "0"၊ "-2"၊ "abc"၊ "2.5" လို မမှန်တဲ့ value တွေအတွက် 1 ကို ပြန်ပေးပါ
  • ?page=2&page=5 လို array ဖြစ်လာရင် ပထမ value ကို သုံးပါ
  • pageRange(3, 10) က { start: 20, end: 30 } ဖြစ်ကြောင်း test လုပ်ပါ
pagination.test.ts
// lib/pagination.ts
export function parsePage(value: string | string[] | undefined): number {
  // TODO: take the first value if it is an array
  // TODO: accept only whole numbers >= 1, otherwise return 1
  return 1;
}

export function pageRange(page: number, pageSize: number) {
  // TODO: return { start, end } for slicing the notes array
  return { start: 0, end: 0 };
}

// TODO: paste the describe / it / expect stand-in from the lesson
// (or import them from "vitest" in a real project), adding a toBe matcher

// TODO: describe("parsePage", ...) with one it(...) per requirement
// TODO: describe("pageRange", ...)

ရလဒ်: parsePage အောက်မှာ ✓ လေးခု၊ pageRange အောက်မှာ ✓ တစ်ခု ပေါ်ပါတယ်။ parsePage ထဲက regex ကို ဖျက်ကြည့်ရင် "falls back to 1" test က ✗ နဲ့ fail ဖြစ်တာ မြင်ရပါမယ်။

အရိပ်အမြွက် ကြည့်ရန်

/^\d+$/ regex က digit သီးသန့်ပဲ ပါတဲ့ string ကို စစ်ပေးလို့ "-2" နဲ့ "2.5" ကို တစ်ခါတည်း ပယ်ပေးပါတယ်။ ပြီးမှ Number() နဲ့ ပြောင်းပြီး 1 ထက် ငယ်လား စစ်ပါ။ toBe matcher ကို node:assert ရဲ့ equal နဲ့ ရေးနိုင်ပါတယ်။

အဖြေ ကြည့်ရန်
pagination.test.ts
import { deepStrictEqual, equal } from "node:assert/strict";

// lib/pagination.ts
// searchParams values arrive as string | string[] | undefined.
export function parsePage(value: string | string[] | undefined): number {
  const raw = Array.isArray(value) ? value[0] : value;
  if (raw === undefined || !/^\d+$/.test(raw)) return 1;
  const page = Number(raw);
  return page >= 1 ? page : 1;
}

export function pageRange(page: number, pageSize: number) {
  const start = (page - 1) * pageSize;
  return { start, end: start + pageSize };
}

// Stand-in for: import { describe, expect, it } from "vitest";
const describe = (name: string, fn: () => void) => { console.log(name); fn(); };
const it = (name: string, fn: () => void) => {
  try { fn(); console.log(`  ✓ ${name}`); }
  catch (err) { console.log(`  ✗ ${name}: ${(err as Error).message}`); }
};
const expect = (actual: unknown) => ({
  toBe: (expected: unknown) => equal(actual, expected),
  toEqual: (expected: unknown) => deepStrictEqual(actual, expected),
});

describe("parsePage", () => {
  it("defaults to page 1 when ?page is missing", () => {
    expect(parsePage(undefined)).toBe(1);
  });
  it("reads a normal page number", () => {
    expect(parsePage("3")).toBe(3);
  });
  it("falls back to 1 for zero, negatives and text", () => {
    for (const bad of ["0", "-2", "abc", "2.5", ""]) expect(parsePage(bad)).toBe(1);
  });
  it("uses the first value of ?page=2&page=5", () => {
    expect(parsePage(["2", "5"])).toBe(2);
  });
});

describe("pageRange", () => {
  it("turns page 3 of size 10 into items 20 to 30", () => {
    expect(pageRange(3, 10)).toEqual({ start: 20, end: 30 });
  });
});

Quick Quiz 1 / 3

Database ကနေ note တွေ await နဲ့ ယူပြီး ပြတဲ့ async Server Component page ကို စစ်ချင်တယ်။ Next.js docs အကြံပြုချက်အတိုင်း ဘယ်နည်းက အသင့်တော်ဆုံးလဲ။

Quick Quiz 2 / 3

Server Action ထဲမှာ console.log("saving", title) ထည့်ပြီး form ကို submit လုပ်လိုက်တယ်။ Log ကို အသေချာဆုံး ဘယ်မှာ တွေ့မလဲ။

Quick Quiz 3 / 3

Lesson ထဲက validateTitle ကို သုံးရင် validateTitle(" ab ") က ဘာပြန်ပေးမလဲ။

မှတ်ထားရမယ့် အချက်များ

  • Type check၊ unit၊ component၊ E2E တစ်ခုစီက bug အမျိုးအစား မတူတာကို ဖမ်းလို့ အလွှာလိုက် သုံးပါ
  • Server Action နဲ့ page ထဲက rule တွေကို plain function အဖြစ် ခွဲပြီး Vitest နဲ့ မြန်မြန် test လုပ်ပါ
  • Async Server Component ပါတဲ့ flow တွေကို production build ပေါ်မှာ Playwright နဲ့ E2E test လုပ်ပါ
  • Server code ရဲ့ log က terminal မှာ၊ client code ရဲ့ log က browser console မှာ ပေါ်ပါတယ်

Next.js — Testing — Next.js

Next.js — Testing — Next.js

နောက်ဆုံးပြင်ဆင်ရက်

Progress ကို ဤ browser (ဤစက်) တွင်သာ သိမ်းထားပါသည်။ အခြားစက်များနှင့် sync မလုပ်ပါ။

ဤသင်ခန်းစာတွင် အမှားတွေ့ပါက အကြောင်းကြားရန်သင့် email app ကို ဤသင်ခန်းစာလိပ်စာ ဖြည့်ပြီးသားဖြင့် ဖွင့်ပေးပါမည် — သင်ကိုယ်တိုင် ပို့မှသာ ပို့မည်ဖြစ်သည်။