lecture 9 of 110 completed

Typed data-driven tests

A typed table of cases is checked row by row before any test runs. satisfies checks a table and keeps the exact names in it.

What you'll learn

  • Why an untyped table of cases hides a wrong row
  • How to type the rows of a table
  • What satisfies checks, and what an annotation loses

A data-driven test runs the same steps for every row of a table. Here is a table for the Shelf fee label in JavaScript, with a small loop in place of the test runner.

function feeLabel(daysLate) {
  return daysLate === 0 ? "No fee" : (daysLate * 0.5).toFixed(2) + " EUR";
}

const cases = [
  { daysLate: 0, expected: "No fee" },
  { daysLate: 3, expected: 1.5 },
  { daysLate: "10", expected: "5.00 EUR" },
];

for (const item of cases) {
  const result = feeLabel(item.daysLate) === item.expected ? "pass" : "fail";
  console.log(item.daysLate + " days: " + result);
}
0 days: pass
3 days: fail
10 days: pass

Two rows are wrong, and only one fails. The second row expects a number where the label is text. That is a mistake in the table, and it looks like a bug in the application. The third row gives the days as text. JavaScript converts it for the multiplication, so the row passes, and the mistake stays in the table.

Now add types to the function and leave the table as it is.

function feeLabel(daysLate: number): string {
  return daysLate === 0 ? "No fee" : (daysLate * 0.5).toFixed(2) + " EUR";
}

const cases = [
  { daysLate: 0, expected: "No fee" },
  { daysLate: 3, expected: 1.5 },
  { daysLate: "10", expected: "5.00 EUR" },
];

for (const item of cases) {
  const result = feeLabel(item.daysLate) === item.expected ? "pass" : "fail";
  console.log(item.daysLate + " days: " + result);
}
fee.cases.ts(12,27): error TS2345: Argument of type 'string | number' is not assignable to parameter of type 'number'.
  Type 'string' is not assignable to type 'number'.

The compiler found something, and it is not much help. The error is on the line of the loop. It does not name a row. It says nothing about the row that expects a number.

With no type given, the compiler inferred one that fits every row: daysLate is a number or text, and so is expected. A table with mistakes in it gets a type that allows the mistakes. The error appears only where that loose type meets a strict function.

Type the rows

Say what a row is, and annotate the table.

interface FeeCase {
  daysLate: number;
  expected: string;
  note?: string;
}

const cases: FeeCase[] = [
  { daysLate: 0, expected: "No fee" },
  { daysLate: 3, expected: 1.5 },
  { daysLate: "10", expected: "5.00 EUR", note: "ten days" },
];
fee.cases.ts(9,18): error TS2322: Type 'number' is not assignable to type 'string'.
fee.cases.ts(10,5): error TS2322: Type 'string' is not assignable to type 'number'.

Both rows are reported, each at the wrong value, before any test runs. The third row no longer passes by accident. In a table of forty rows, that is the difference between reading one error and reading a failed test report.

note? is optional, so a row may leave it out. Use optional fields for the things only some rows need.

A generic row

Tables for different functions have the same idea: a name, an input and the expected result. One generic interface describes all of them. This is the Paged<T> idea from lecture 3.

interface Case<Input, Expected> {
  name: string;
  input: Input;
  expected: Expected;
}

const feeCases: Case<number, string>[] = [
  { name: "on time", input: 0, expected: "No fee" },
  { name: "three days late", input: 3, expected: "1.50 EUR" },
];

for (const item of feeCases) {
  console.log(item.name + ": " + item.input + " -> " + item.expected);
}
on time: 0 -> No fee
three days late: 3 -> 1.50 EUR

In a real test file

Playwright's documentation shows this pattern: loop over the table and create one test for each row. Every test needs its own name, so put a value from the row into the title.

import { test, expect } from "@playwright/test";

interface FeeCase {
  daysLate: number;
  expected: string;
}

const cases: FeeCase[] = [
  { daysLate: 0, expected: "No fee" },
  { daysLate: 3, expected: "1.50 EUR" },
];

for (const { daysLate, expected } of cases) {
  test(`fee after ${daysLate} days late`, async ({ page }) => {
    await page.goto("/loans?daysLate=" + daysLate);
    await expect(page.getByRole("status")).toHaveText(expected);
  });
}

When an annotation loses the names

Sometimes cases have names, and a test picks one. An object is the natural table.

interface FeeCase {
  daysLate: number;
  expected: string;
}

const cases: Record<string, FeeCase> = {
  onTime: { daysLate: 0, expected: "No fee" },
  threeDays: { daysLate: 3, expected: "1.50 EUR" },
};

console.log(cases.treeDays.expected);
TypeError: Cannot read properties of undefined (reading 'expected')

The name is misspelled. The compiler said nothing, and the code stops when it runs. The wording is from the engine in Node and Chrome.

The annotation is the cause. Record<string, FeeCase> says "an object with any text as a name". You wrote two names, and the annotation replaced that knowledge with "any text". So every name is accepted, including ones that do not exist.

satisfies

satisfies checks a value against a type and keeps what the compiler inferred.

interface FeeCase {
  daysLate: number;
  expected: string;
}

const cases = {
  onTime: { daysLate: 0, expected: "No fee" },
  threeDays: { daysLate: 3, expected: "1.50 EUR" },
} satisfies Record<string, FeeCase>;

console.log(cases.treeDays.expected);
fee.cases.ts(11,19): error TS2551: Property 'treeDays' does not exist on type '{ onTime: { daysLate: number; expected: string; }; threeDays: { daysLate: number; expected: string; }; }'. Did you mean 'threeDays'?

Now the compiler knows the two names, and it reports the third. The rows are still checked: a row with a wrong field is an error at satisfies.

Put simply, an annotation says "treat this value as that type". satisfies says "check that this value fits that type, and remember the value". For a table that tests pick from by name, you want the second.

Remember this

An untyped table gets a type that fits its mistakes. Give the rows an interface, and each wrong value is reported before a test runs. For a table with named cases, use satisfies, so the rows are checked and a misspelled name is an error.

Reference: Parameterize tests in the Playwright documentation.

now prove it

Reading is the setup. Solve this exercise in the editor. We check your code with the TypeScript compiler and list each type error.