Skip to main content
MediumTooling & TestingTest

Write Tests for a Currency Exchange Calculator

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

What you will practice

TypeScriptAsync PatternsTestingVitestPure Functions

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.

Starter files

src/CurrencyCalculator.tsReference starter
tests/CurrencyCalculator.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 Inverted Rate, Tests Catch Missing Rounding, Tests Catch No Chaining.

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.

Example behavior

Input
calc.setRate('USD', 'EUR', 1.1);
calc.convert(10, 'USD', 'EUR');
Output
11.00

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

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

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

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?