Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

185
Views
How to prevent Jest from combining results?

I currently have two jest test cases the first one passes, but the second fails because its adding the sum of the previous case to that one

test('Subtotal to be 386.95 & VAT to be 54.173', () => {
    cart = ["tshirt", "shoes", "jacket", "blouse", "pants"]
    expect(calculateSubTotal(cart)).toBe(386.95);
    expect(calculateVAT(cart)).toBe(54.173)
})

test('Subtotal to be 30.99 & VAT to be 4.388', () => {
    cart = ["tshirt"]
    expect(calculateSubTotal(cart)).toBe(30.99);
    expect(calculateVAT(cart)).toBe(4.338)
});
expect(received).toBe(expected) // Object.is equality

    Expected: 30.99
    Received: 417.94

calculateSubTotal implementation:

cart.js

let products = {...} // products map by product name
let subTotal = 0 // line number 60

function calculateSubTotal(cart) {
    // line number 75
    for (i = 0; i < cart.length; i++) {
        switch (cart[i]) {
            case ("tshirt"):
                subTotal += products.tshirt.price
                break
            case ("blouse"):
                subTotal += products.blouse.price
                break
            // ... other product
        }
    }
    subTotal = Number(subTotal.toFixed(2))
    return subTotal
}

module.exports.calculateSubTotal = calculateSubTotal;

// another function

What do I need to do for jest to break out of the first test and not combine the test results?

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!