|

Day 33 of 100: JS Capstone — A Pure JavaScript API Test Suite

Day 33 of 100 — 100 Days of Playwright with TypeScript Challenge. Phase: JavaScript Fundamentals. JavaScript (Days 1-33) → TypeScript (Days 34-63) → Playwright (Days 64-96) → AI Framework Capstone (Days 97-100).

🎭 Want to master this with real projects? Join the Playwright Automation Mastery course at The Testing Academy.


JavaScript phase finale. Build a runnable mini API test suite using only the last 32 days — no frameworks.

Contents

The Complete Script

// api-suite.js — node api-suite.js
const BASE = 'https://jsonplaceholder.typicode.com';
const results = [];

async function check(name, fn) {
  const t0 = Date.now();
  try {
    await fn();
    results.push({ name, status: 'PASS', ms: Date.now() - t0 });
  } catch (e) {
    results.push({ name, status: 'FAIL', ms: Date.now() - t0, error: e.message });
  }
}

function assertEqual(actual, expected, label) {
  if (actual !== expected)
    throw new Error(`${label}: expected ${expected}, got ${actual}`);
}

await check('GET /users returns 200 with 10 users', async () => {
  const res = await fetch(`${BASE}/users`);
  assertEqual(res.status, 200, 'status');
  const users = await res.json();
  assertEqual(users.length, 10, 'count');
});

await check('POST /posts creates resource', async () => {
  const res = await fetch(`${BASE}/posts`, {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify({ title: `qa-${Date.now()}`, userId: 1 }),
  });
  assertEqual(res.status, 201, 'status');
});

await check('GET /users/999 returns 404', async () => {
  const res = await fetch(`${BASE}/users/999`);
  assertEqual(res.status, 404, 'status');
});

// Report
const passed = results.filter(r => r.status === 'PASS').length;
console.log(`\n${passed}/${results.length} passed`);
for (const r of results) {
  console.log(`${r.status} ${r.name} (${r.ms}ms)` + (r.error ? ` — ${r.error}` : ''));
}
process.exit(passed === results.length ? 0 : 1);

🚀 Level Up Your Playwright

From locators to CI pipelines — build a production-grade Playwright + TypeScript framework step by step.

Concepts You Just Used

  • async/await + fetch (Days 24-25)
  • Template literals, Date.now() unique data (Days 13, 30)
  • Array filter for reporting (Day 9)
  • Custom error throwing (Day 20)
  • Destructuring, arrow functions throughout (Days 7, 11)
  • Exit codes — CI-ready (Day 28 scripts)

JavaScript phase complete. Tomorrow: TypeScript — the same language with a safety net.

Tomorrow (Day 34): TypeScript begins — why types catch bugs before runtime.

🎓 Master Playwright End to End

Join hundreds of SDETs building real automation frameworks. Lifetime access, hands-on projects, and a job-ready portfolio.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.