Write Tests for a Currency Amount Formatter
You're writing tests for a Stripe-style amount formatter. The function is in src/formatters.ts — it's read-only. You can read it, but you can't change it.
Your job is in tests/formatters.test.ts. Write a test suite that:
- Passes against the correct implementation (the one you can see).
- Catches buggy implementations the judge will swap in — each variant breaks one specific edge case.
A test suite that only checks formatAmount(1234, 'USD') === '$12.34' will only catch one of three bugs. Good tests cover the boundaries: zero, negative, currencies with no decimals.
- Use Vitest (
describe,it,expect) — already available in the runtime. - Import
formatAmountfrom'../src/formatters'. - Every test in your suite must pass when run against the reference implementation.
- Your suite must produce at least one failing test against each of three buggy variants: off-by-one decimal placement, minus sign placed after the currency symbol, and JPY treated as a two-decimal currency.
formatAmount(1234, 'USD')
'$12.34'
1234 cents → 12.34 dollars.
formatAmount(-50, 'USD')
'-$0.50'
Minus sign goes before the currency symbol.
formatAmount(1234, 'JPY')
'¥1234'
JPY is zero-decimal — the input is the whole-yen amount.
- Do not modify
src/formatters.ts. - All assertions go in
tests/formatters.test.ts.
Real production codebases use property-based testing (fast-check, hypothesis) for functions like this — generate random inputs and check invariants instead of hand-picking cases. How would you express 'the output always starts with the right currency symbol' as a property?
Keep moving through related testing problems and build a stronger search-friendly practice loop around this topic.
View track →