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.
- 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
Type check
Lint & unit tests
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:
{
"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 ciin CI: Installs strictly frompackage-lock.jsoncleanly 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:
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:
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
Why does vite build alone NOT catch TypeScript type errors unless you also run tsc --noEmit?
Why should package-lock.json always be committed to Git?