Many of the adverts we studied ask for testing, including entry level roles at a fintech and a National Service placement in Accra. Tests are small programs that check your code does what you expect. They let you change code later without fear, and they show employers you think like a professional.
Kinds of tests
- Unit tests check one small piece, such as a function that calculates a total.
- Component or integration tests check pieces working together, such as a form that shows an error when a field is empty.
- End to end tests drive a real browser through a full journey, such as adding to cart and checking out.
Start with unit tests. They are the quickest to write and run.
A first unit test
Free test runners such as Vitest work with modern JavaScript projects. Suppose you have:
// cart.js
export function total(items) {
return items.reduce((sum, i) => sum + i.price * i.qty, 0);
}A test file next to it:
// cart.test.js
import { test, expect } from "vitest";
import { total } from "./cart.js";
test("adds price times quantity", () => {
const items = [{ price: 15, qty: 2 }, { price: 5, qty: 1 }];
expect(total(items)).toBe(35);
});
test("empty cart is zero", () => {
expect(total([])).toBe(0);
});