Skip to main content
EasyTooling & TestingTest

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...

What you will practice

TypeScriptTestingVitestEdge Cases

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.

Starter files

src/formatters.tsReference starter
tests/formatters.test.tsEditable tests

What the judge checks

  • Runs in the node environment with the node-vitest runner.
  • Uses a 15000ms judge budget.
  • Behavior rules include: Tests Pass On Reference, Tests Catch Off By One Decimal, Tests Catch Negative Sign Placement, Tests Catch Jpy Zero Decimal.

Constraints

  • Do not modify `src/formatters.ts`.
  • All assertions go in `tests/formatters.test.ts`.

Example behavior

Input
formatAmount(1234, 'USD')
Output
'$12.34'

1234 cents → 12.34 dollars.

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

Minus sign goes before the currency symbol.

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?