Jest testy pro kalkulačku slev
Vývojáři okamžitě získají Jest testy s plným pokrytím včetně hraničních a chybových případů místo ručního psaní boilerplate.
Zobrazit náhled vstupu a výstupu
Vstup
- Kód k otestování
- function calculateDiscount(price, userTier, couponCode) { let discount = 0; if (userTier === 'gold') discount += 0.15; else if (userTier === 'silver') discount += 0.10; if (couponCode === 'SAVE20') discount += 0.20; if (discount > 0.5) discount = 0.5; if (price < 0) throw new Error('Price cannot be negative'); return +(price * (1 - discount)).toFixed(2); }
- Programovací jazyk
- JavaScript
- Testovací framework
- Jest
- Zaměření reportáže
- Úplné pokrytí (vše výše uvedené)
- Styl testu
- AAA (Arrange-Act-Assert)
Výstup (úryvek)
describe('calculateDiscount', () => {
it('applies gold tier discount', () => {
// Arrange
const price = 100;
// Act
const result = calculateDiscount(price, 'gold', null);
// Assert
expect(result).toBe(85);
});
it('caps total discount at 50%', () => {
expect(calculateDiscount(100, 'gold', 'SAVE20')).toBe(65);
});
it('throws on negative price', () => {
expect(() => calculateDiscount(-5, 'gold', null)).toThrow('Price cannot be negative');
});
});