|

Day 61 of 100: TS Capstone Part 2 — Factories, Cleanup, Assembled Suite

Day 61 of 100 — 100 Days of Playwright with TypeScript Challenge. Phase: TypeScript. 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.


Capstone part 2: typed factories with Partial overrides, a cleanup registry, and the assembled typed suite.

Contents

Typed Factory + Cleanup

import type { User, NewUser } from './types';

export function makeNewUser(overrides: Partial = {}): NewUser {
  const stamp = Date.now();
  return {
    name: `user-${stamp}`,
    email: `user-${stamp}@test.io`,
    ...overrides,
  };
}

export class CleanupRegistry {
  private tasks: Array<() => Promise> = [];
  add(task: () => Promise) { this.tasks.push(task); }
  async runAll(): Promise {
    for (const task of this.tasks.reverse()) {
      try { await task(); } catch (e) { console.warn('cleanup failed:', e); }
    }
  }
}

🚀 Level Up Your Playwright

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

The Assembled Typed Suite

import { ApiClient, ApiError } from './api-client';
import { isUser, isUserArray } from './types';
import { makeNewUser, CleanupRegistry } from './factory';

const api = new ApiClient('https://jsonplaceholder.typicode.com');
const cleanup = new CleanupRegistry();

async function main() {
  const users = await api.get('/users', isUserArray);
  console.log(`PASS list: ${users.length} users`);

  const created = await api.post('/users', makeNewUser(), isUser);
  cleanup.add(async () => console.log(`would delete user ${created.id}`));
  console.log(`PASS create: id ${created.id}`);

  try {
    await api.get('/users/99999', isUser);
    console.log('FAIL: expected 404');
  } catch (e) {
    if (e instanceof ApiError && e.status === 404) console.log('PASS 404 path');
    else throw e;
  }

  await cleanup.runAll();
}

main().catch(e => { console.error(e); process.exit(1); });

TypeScript phase nearly complete. Compare with Day 33’s untyped version: same behavior, but typos, shape drift, and missing awaits are now compile-time failures.

Tomorrow (Day 62): TS phase review — the 10 patterns to retain.

🎓 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.