Skip to main content
Problem 22

Write Tests for a Currency Amount Formatter

EASYTEST
TypeScript+3

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:

  1. Passes against the correct implementation (the one you can see).
  2. 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.

Requirements
  • Use Vitest (describe, it, expect) — already available in the runtime.
  • Import formatAmount from '../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.
Examples
Example 1
Input
formatAmount(1234, 'USD')
Output
'$12.34'
Note

1234 cents → 12.34 dollars.

Example 2
Input
formatAmount(-50, 'USD')
Output
'-$0.50'
Note

Minus sign goes before the currency symbol.

Example 3
Input
formatAmount(1234, 'JPY')
Output
'¥1234'
Note

JPY is zero-decimal — the input is the whole-yen amount.

Constraints
  • Do not modify src/formatters.ts.
  • All assertions go in tests/formatters.test.ts.
Follow-up

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?

Hints
Related Practice
Track
Testing

Keep moving through related testing problems and build a stronger search-friendly practice loop around this topic.

View track →
Console output will appear here...