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

137
Views
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
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!