Skip to main content

Types and interfaces for test data

Why This Matters

Test data travels: from a fixture file into a helper, from the API into an assertion, from one test into the next. Every hop is a chance to use the wrong field name or the wrong kind of value. A type describes the shape of the data once, and the checker holds every hop to it.

Describe the data you get

The Shop API returns products like this (real response from GET /api/shop/products):

{
  "id": 5,
  "sku": "AC-HEAD-05",
  "name": "Bluetooth Headphones",
  "price": 59.99,
  "originalPrice": 59.99,
  "category": "Accessories",
  "rating": 5,
  "onSale": false,
  "stock": 20,
  "inStock": true
}

src/types.ts describes that shape as an interface:

export interface Product {
  id: number;
  sku: string;
  name: string;
  description: string;
  price: number;
  originalPrice: number;
  image: string;
  category: "Electronics" | "Accessories" | "Office Supplies" | "Furniture";
  rating: number;
  onSale: boolean;
  stock: number;
  inStock: boolean;
}

Union types: only the values that exist

Look at category. It is not just string: it lists the four categories the shop has. That is a union type. A typo such as "Electronic" or a category that does not exist is now an editor error:

const filter: Product["category"] = "Electronic";
// error TS2820: Type '"Electronic"' is not assignable to type
//   '"Electronics" | "Accessories" | "Office Supplies" | "Furniture"'. Did you mean '"Electronics"'?

Union types are ideal for anything with a fixed set of values: order statuses, user roles, browser names.

Pick only what you need

A cart line does not need the whole product, only its id, name and price. Pick builds a smaller type from a bigger one:

export interface CartLine {
  product: Pick<Product, "id" | "name" | "price">;
  quantity: number;
}

Now a test can create a cart line with three fields instead of twelve, and still cannot misspell any of them.

type or interface?

Both describe shapes. Use interface for objects (it reads well and gives clear error messages) and type for everything else, such as unions:

type OrderStatus = "open" | "paid" | "cancelled";

Do not spend long on the choice; consistency within a project matters more.

Typing what the API returns

When a test calls the API (module 4), the JSON it gets back has no type: TypeScript cannot know what a server will send. You tell it what you expect:

const body = (await response.json()) as { items: Product[]; total: number };

as is a promise from you to the checker, not a check. If the API changes, the types stay happy and the test fails at runtime. Module 4 adds a real check with a schema; for now, remember that as describes and does not verify.

Exercise

Add a type for the cart that GET /api/shop/cart returns. The real response for an empty cart is:

{ "items": [], "itemCount": 0, "subtotal": 0, "shipping": 0, "tax": 0, "total": 0, "currency": "USD" }

Call it Cart and put it in src/types.ts. You may type items as unknown[] for now.

Show suggested solution
export interface Cart {
  items: unknown[];
  itemCount: number;
  subtotal: number;
  shipping: number;
  tax: number;
  total: number;
  currency: "USD";
}

currency is typed as the single value "USD" because that is the only currency the shop uses; if that ever changes, the type is the first place a test will tell you. In module 4 you replace unknown[] with a proper type for a cart item, once you have seen one from the API.