Loading Depth Study
Preparing your workspace
A unit test checks a small, focused piece of logic such as a function or class method in isolation.
Unit tests focus on small pieces of behavior and are usually fast to run. A good unit test describes a clear input and expected result without depending on databases, external services, or the full application. Unit tests are useful for calculations, validation, formatting, business rules, and utility functions. Keeping units small and predictable makes both implementation and testing easier.
A course progress calculator can be tested with zero lessons, partially completed lessons, and fully completed lessons without starting the entire web application.
import { describe, expect, it } from 'vitest';
function calculateProgress(completed, total) {
if (total === 0) return 0;
return Math.round((completed / total) * 100);
}
describe('calculateProgress', () => {
it('calculates course progress', () => {
expect(calculateProgress(5, 10)).toBe(50);
});
});The test passes because calculateProgress(5, 10) returns 50.