4. Architecture, Testing & Tooling

Modern Tooling: npm, Vite, ESLint & Unit Testing with Vitest

Set up professional JavaScript and TypeScript workflows: understand package.json and lockfiles, bundle fast with Vite, enforce tsc + ESLint quality gates, and write Vitest unit tests with mocks.

3 min readUpdated 2026-12-01
In This Lesson
  • Understand package.json, package-lock.json, and standard npm quality scripts
  • Explain how Vite serves native ESM in dev and bundles optimized assets for production
  • Write unit tests and mock fetch/functions cleanly with Vitest (describe, it, expect, vi.fn)

Knowing JavaScript and TypeScript syntax is half the job—the other half is shipping reliable projects using standard industry tooling: npm, Vite, ESLint, tsc, and Vitest.

1. The Frontend Quality Pipeline

Modern Frontend Tooling: Quality Gate Before Deploy
1. tsc --noEmit

Type check

2. eslint + vitest

Lint & unit tests

3. vite build

Minified dist/ bundle

Run tsc --noEmit (types), ESLint (bugs/style), and Vitest (unit tests) before Vite bundles your optimized production assets.

Every professional TypeScript repository includes scripts in package.json so CI and teammates run identical checks:

json
{
  "scripts": {
    "dev": "vite",
    "typecheck": "tsc --noEmit",
    "lint": "eslint src/",
    "test": "vitest run",
    "build": "tsc --noEmit && vite build"
  }
}
  • package-lock.json: Always commit your lockfile to Git! It pins exact dependency versions so builds behave identically on your laptop, teammates' laptops, and CI servers.
  • npm ci in CI: Installs strictly from package-lock.json cleanly instead of mutating versions.
  • Why Vite? In dev mode, Vite serves native ES Modules on demand in milliseconds (no slow full-app rebuild on every keystroke). For production (vite build), Rollup bundles, tree-shakes unused code, and minifies JS/CSS.

2. Unit Testing with Vitest (describe, it, expect, vi.fn)

Vitest shares Vite's TypeScript config so you test .ts modules directly with zero extra setup:

ts
import { describe, expect, it, vi } from "vitest";
import { loadCourseTitle } from "./api";

describe("loadCourseTitle", () => {
  it("fetches and formats course title cleanly", async () => {
    const fakeFetch = vi.fn().mockResolvedValue({
      ok: true,
      json: async () => ({ title: "  Modern TypeScript  " }),
    });

    const title = await loadCourseTitle("ts-101", fakeFetch);
    expect(title).toBe("Modern TypeScript");
    expect(fakeFetch).toHaveBeenCalledWith("/api/courses/ts-101");
  });
});

Notice dependency injection (fakeFetch passed as a parameter, defaulting to fetch in production)—that makes testing async API helpers effortless and fast without hitting real networks!

Try It Yourself in the Playground

Build and run a lightweight describe / it / expect unit test suite (including a custom createSpy() mock!) right inside the JS playground:

JavaScriptTry It Live: Mini Vitest-Style Unit Test Runner & Spy
Output

Click “Run Code” to execute this snippet right in your browser.

Mini-Challenge: Why should package.json build scripts run tsc --noEmit && vite build rather than vite build alone?

Quick Checkpoints

Check Your Understanding

Why does vite build alone NOT catch TypeScript type errors unless you also run tsc --noEmit?

Check Your Understanding

Why should package-lock.json always be committed to Git?