ข้ามไปยังเนื้อหา

การทดสอบและเครื่องมือ

การเขียนโค้ดเป็นเพียงครึ่งหนึ่งของงาน การรู้วิธีทดสอบ ดีบัก วัดผล และตั้งค่าสภาพแวดล้อม Node.js จะเปลี่ยนสคริปต์ที่ทำงานได้ให้กลายเป็นแอปพลิเคชันที่ดูแลรักษาง่ายและพร้อม production

Node 18+ มี test runner ในตัว, inspector protocol ที่ทรงพลัง, และ perf_hooks สำหรับจับเวลา ซึ่งหมายความว่าคุณทำได้มากโดยไม่ต้องติดตั้งอะไรเพิ่มเติม

บทเรียนหัวข้อ
Built-in Test Runnernode:test, node:assert, test(), describe/it, node --test
การดีบักnode --inspect, Chrome DevTools, breakpoints, เมธอดต่าง ๆ ของ console
ประสิทธิภาพperf_hooks, performance.now(), PerformanceObserver, การหลีกเลี่ยงการบล็อก
Ecosystem และ ToolchainTypeScript runners, ESLint/Prettier/Biome, nodemon, env files
┌─────────────────────────────────────────────────┐
│ Your Node.js Project │
│ │
│ Source code (.ts / .js / .mjs) │
│ │ │
│ ▼ │
│ Linter / Formatter ──► ESLint, Prettier, │
│ Biome │
│ │ │
│ ▼ │
│ TypeScript runner ──► tsx, ts-node, │
│ --experimental- │
│ strip-types │
│ │ │
│ ▼ │
│ Test runner ──► node:test (built-in) │
│ or Vitest / Jest │
│ │ │
│ ▼ │
│ Debugger ──► node --inspect + │
│ Chrome DevTools │
│ │ │
│ ▼ │
│ Performance ──► perf_hooks, │
│ PerformanceObserver │
└─────────────────────────────────────────────────┘

Built-in ของ Node ครอบคลุมความต้องการพื้นฐานได้เกือบทั้งหมด นี่คือแนวทางการตัดสินใจเบื้องต้น:

Terminal window
# ไม่ต้องติดตั้ง dependency — ใช้ built-in runner
node --test src/**/*.test.js
# TypeScript โดยไม่ต้อง build (Node 20.6+)
node --experimental-strip-types src/app.ts
# HMR + watch mode ที่เร็วกว่า
npx tsx watch src/app.ts
# Lint + format ในเครื่องมือเดียว (zero config)
npx biome check --apply src/
Node.js เวอร์ชันไหนที่แนะนำ built-in `node:test` runner เป็นครั้งแรก?
โมดูล built-in ใดที่ใช้วัดเวลาการทำงานใน Node.js?
flag ใดที่ทำให้ Node 20.6+ รันไฟล์ TypeScript ได้โดยตรงโดยไม่ต้อง build?