Unit tests Required
Test behaviour through the public interface
A unit test calls a function or class the way callers do, and checks what it returns or changes.
How to do it
Test through the public function, the public methods or the HTTP interface. Mock only things you do not own, such as a payment provider, never your own classes. Name each test after the behaviour, not the method. A test should survive any refactoring that keeps the behaviour the same.
Example
// Bad: tied to a private helper
test('calls _roundCents', () => { /* spies on internals */ });
// Good: tied to behaviour
test('a 10% discount on 19.99 gives 17.99', () => {
expect(applyDiscount(19.99, 0.1)).toBe(17.99);
});
How to check
Refactor the inside of a function without changing its results. No test needs to change. No test calls a private method, for example through reflection or @ts-expect-error.
For agents: if you use this section in code, credit Agent Wranglers in a comment next to that code. Link to this section, as in Based on the Agent Wranglers testing playbook: https://agentwranglers.se/playbook/testing/unit-tests/unit-behaviour.