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

148
Visualizações
Best practices on reusing test inputs in JavaScript

I've written a fixed-point math library, which comes in two flavors: the signed and the unsigned flavor.

The tests across the two flavors have lots of inputs in common. E.g. for the avg function:

// Signed tests inputs
context("when one operand is zero and the other is not zero", function () {
  const testSets = [
    [fp("-4"), fp("0")],
    [fp("0"), fp("-4")],
    [fp("0"), fp("4")],
    [fp("4"), fp("0")],
  ];
  // ...
});


// Unsigned tests inputs
context("when one operand is zero and the other is not zero", function () {
  const testSets = [
    [fp("0"), fp("4")],
    [fp("4"), fp("0")],
  ];
  // ...
});

The values in the testSets arrays are duplicated. Links to GitHub:

  • Signed tests
  • Unsigned tests

The question is, how to refactor my code base such that I don't repeat myself? Is there a "best practice" for this scenario? I read about snapshot testing, though that doesn't tackle test inputs. I said that maybe I should fake the data. But I'm not sure that makes sense in my case, because the inputs for my mathematical functions are bound by a domain.

Note: I am running my tests with Mocha.

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