In this project we'll explore different ways to use fixtures (essentially static JSON objects.)
- Clarity in tests.
- Tests should look more like actual implementation.
- Explore helper functions to generate fixtures.
We'll be given an array of transactions on which we'll perform operations.
-
processTotals(txs: Transactions[])
- Calculatetotal
perBaseTransaction
, by addingtax
andtip
to atotal
field.
-
sumTotal(txs: Transactions[])
- Sum of alltotal
s in list of transactions. - Detect invalid values (negative taxes, etc.)
- Group totals by category.
// ✅ Stable, inline values
expect(result[0].total).toBe(130.0);
expect(result[1].total).toBe(140.0);
// 🟡 No mutations*, direct field mapping to focus on the value that changed
expect(result.map(tx => tx.total)).toMatchObject([
130.0,
140.0,
]);
// 🔴 Repeated Calculations, may reference `result` variable
expect(result[0].total).toBe((100.0 * 1.1) + 20.0);
expect(result[1].total).toBe((100.0 * 1.1) + 30.0);