Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

186
Visualizações
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
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda