Why TypeScript for tests
Why This Matters
A test suite is a program that nobody runs until something breaks. If a typo in that program only shows up at runtime, you find it at the worst moment: in the middle of a CI run, after ten minutes of setup, in a test that has nothing to do with the change you made. TypeScript moves that class of mistake to the moment you type it.
What TypeScript adds, and what it does not
TypeScript is JavaScript with types. Every JavaScript program is valid TypeScript; TypeScript adds a checker that reads your code before it runs and complains when the pieces do not fit.
For tests that means three things:
- Typos in names fail in your editor, not in the pipeline.
- Autocomplete knows your data. Type
product.and the editor listsprice,stock,inStock: no need to look up the API docs again. - Refactoring is safe. Rename a field and the checker shows every test that still uses the old name.
What TypeScript does not do: it does not check what the application does. A test can be perfectly typed and still assert the wrong thing. Types catch mistakes in your test code; assertions catch mistakes in the product.
No build step
You do not compile anything yourself. Playwright runs .ts test files directly: it strips the types on the fly and runs the JavaScript that is left. The type check is a separate command, so it can run in your editor and in CI:
npx tsc --noEmit
--noEmit means “check only, write no output files”.
See it catch a typo
Clone the example repository and install it:
git clone https://github.com/learn-automated-testing/playwright-practice.git
cd playwright-practice
npm ci
Open src/types.ts. It describes a product exactly as the Shop API of practiceautomatedtesting.com returns it:
export interface Product {
id: number;
sku: string;
name: string;
price: number;
// ...
stock: number;
inStock: boolean;
}
Now write a test that reads prise instead of price and run the check. This is the real output:
tests/module-2/_typo.spec.ts(5,12): error TS2551: Property 'prise' does not exist on type 'Product'. Did you mean 'price'?
In plain JavaScript that line would have read undefined, the assertion would have failed with expected 59.99, received undefined, and you would have started debugging the shop instead of your test.
Reading a type error
Type errors look intimidating until you read them in three parts:
- Where: file, line and column,
(5,12). - What:
Property 'prise' does not exist on type 'Product'. - A hint, often:
Did you mean 'price'?
Most type errors in test code are one of three kinds: a misspelled name, a value of the wrong type (a string where a number belongs), or a missing field in an object.
Exercise
- In the repository, introduce a typo in
src/cart.ts, for exampleline.quantty. - Run
npm run typecheckand read the error in the three parts above. - Run
npx playwright test --project=unit. What happens, and why does the test runner not stop you?
Show suggested answer
npm run typecheck reports Property 'quantty' does not exist on type 'CartLine'. The test run, however, still starts: Playwright strips the types without checking them, so the typo reaches runtime, line.quantty is undefined, and the maths tests fail with NaN. That is why the example repository runs npm run typecheck as a separate step in CI: the runner is fast because it does not check, so something else has to.