Skip to main content
Problem 41

Write Tests for a Currency Exchange Calculator

MEDIUMTEST
TypeScript+3

You are given a CurrencyCalculator class in src/CurrencyCalculator.ts. It supports registering exchange rates and converting amounts between currencies.

Your job is to write a Vitest test suite in tests/CurrencyCalculator.test.ts that:

  • Passes on the correct reference implementation.
  • Catches each of the 4 known bugs described below.

The judge runs your tests against the reference and against 4 intentionally broken implementations. A test suite that only checks the happy path will miss the bugs.

The 4 bugs your tests must catch:

  1. Inverted rateconvert applies the rate in the wrong direction (divides instead of multiplies, or swaps from/to).
  2. Missing roundingconvert returns a raw floating-point result without rounding to 2 decimal places.
  3. No chainingconvert only looks up direct rates; it does not find a path through an intermediate currency.
  4. Same-currency bugconvert(amount, 'USD', 'USD') returns a wrong value instead of amount rounded to 2 decimal places.
Requirements
  • Write tests in tests/CurrencyCalculator.test.ts. The test file is the only editable file.
  • Tests must pass on the reference implementation — no false positives.
  • At least one test must fail on the inverted-rate variant.
  • At least one test must fail on the missing-rounding variant.
  • At least one test must fail on the no-chaining variant.
  • At least one test must fail on the same-currency-bug variant.
Examples
Example 1
Input
calc.setRate('USD', 'EUR', 1.1);
calc.convert(10, 'USD', 'EUR');
Output
11.00
Note

10 * 1.1 = 11.0, rounded to 2 decimal places = 11.00. An inverted-rate bug would return ~9.09 instead.

Example 2
Input
calc.setRate('USD', 'EUR', 1.1);
calc.setRate('EUR', 'GBP', 0.9);
calc.convert(100, 'USD', 'GBP');
Output
99.00
Note

100 USD × 1.1 = 110 EUR × 0.9 = 99.0 GBP. A no-chaining implementation throws or returns an error.

Example 3
Input
calc.setRate('USD', 'EUR', 3.33333);
calc.convert(1, 'USD', 'EUR');
Output
3.33
Note

1 * 3.33333 = 3.33333 — must be rounded to 3.33. A missing-rounding bug returns 3.33333.

Constraints
  • Do not modify src/CurrencyCalculator.ts.
  • Use Vitest imports (describe, it, expect, beforeEach) — no other test frameworks.
  • Tests must be deterministic — no randomness or time-dependent assertions.
Follow-up

Your suite tests 4 known bugs. What other edge cases or invariants of a currency calculator are worth testing even when no specific bug has been reported?

Hints
Console output will appear here...