Skip to main content

Variables, functions, objects and arrays

Why This Matters

Most test code is not clever. It is data (a product, a user, an expected total) and small helpers that turn that data into a check. If you can read and write variables, functions, objects and arrays fluently, you can read nearly every Playwright test you will meet.

const, and why tests rarely need let

const product = "Bluetooth Headphones";
const price = 59.99;

const means the name cannot be pointed at another value later. In tests that is almost always what you want: an expected value that changes halfway through a test is a bug waiting to happen. Use let only when a value really must change, such as a counter in a loop.

Objects: data with names

An object groups related values:

const headphones = { id: 5, name: "Bluetooth Headphones", price: 59.99 };

headphones.name;   // "Bluetooth Headphones"
headphones.price;  // 59.99

A cart line in the example repository is an object that contains another object:

const line = { product: headphones, quantity: 2 };
line.product.price * line.quantity;  // 119.98

Arrays and the three methods you need

An array is an ordered list. Tests use three array methods all the time:

const prices = [59.99, 24.99, 199.99];

prices.map((p) => p * 2);          // [119.98, 49.98, 399.98]  transform each item
prices.filter((p) => p < 100);     // [59.99, 24.99]            keep some items
prices.find((p) => p > 100);       // 199.99                    the first match

And one to combine a list into a single value:

prices.reduce((sum, p) => sum + p, 0);   // 284.97

Functions: small, named helpers

(p) => p * 2 above is an arrow function: a function without a name, passed to map. When you want to reuse logic, give it a name and export it. This is the real helper from src/cart.ts:

export function subtotal(lines: CartLine[]): number {
  return toCents(lines.reduce((sum, line) => sum + line.product.price * line.quantity, 0));
}

Read it from the inside out: for every line, multiply price by quantity, add it to the running sum starting at 0, then round to whole cents. The : CartLine[] and : number are types; lesson 4 covers them.

Why round to cents?

Computers store 0.1 + 0.2 as 0.30000000000000004. Money in tests must compare exactly, so the helper rounds every result to two decimals:

export function toCents(amount: number): number {
  return Math.round(amount * 100) / 100;
}

A test without a browser

Playwright’s test runner also runs tests that never open a browser. The module 1 tests in the repository check the cart maths against what the shop’s checkout shows:

test("total adds 21% tax, like the shop's checkout", () => {
  // The checkout shows: Subtotal $59.99, Tax (21%) $12.60, Total $72.59
  expect(totalPrice([{ product: headphones, quantity: 1 }])).toBe(72.59);
});

Run them:

npx playwright test --project=unit
Running 4 tests using 1 worker

  ✓  1 [unit] › tests/module-1/cart-maths.spec.ts:10:1 › subtotal adds price × quantity for every line (7ms)
  ✓  2 [unit] › tests/module-1/cart-maths.spec.ts:18:1 › total adds 21% tax, like the shop's checkout (3ms)
  ✓  3 [unit] › tests/module-1/cart-maths.spec.ts:23:1 › an empty cart costs nothing (6ms)
  ✓  4 [unit] › tests/module-1/cart-maths.spec.ts:27:1 › parsePrice reads the price as the page shows it (7ms)

  4 passed (660ms)

Later modules use the same helpers to check the totals the shop shows in the browser.

Exercise

Add a function itemCount(lines) to src/cart.ts that returns the total number of items in the cart (two headphones and one lamp is 3). Write a test for it in tests/module-1/cart-maths.spec.ts, including the empty cart.

Show suggested solution
export function itemCount(lines: CartLine[]): number {
  return lines.reduce((count, line) => count + line.quantity, 0);
}
test("itemCount adds up the quantities", () => {
  expect(itemCount([{ product: headphones, quantity: 2 }, { product: lamp, quantity: 1 }])).toBe(3);
  expect(itemCount([])).toBe(0);
});

The shop’s cart button shows the same number (“3 items”), so in module 2 you can compare the page with this helper.