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

151
Views
Stub object naming conventions

I need to perform a suite of unit tests to a function that sorts an array based on a combination of 4 parameters. The nature of the functions makes it so that it has a lot of edge cases and I need to test all of them.

For this, I'm planning to create a set of Stub arrays both for the inputs and the outputs of the function. But I'm having trouble naming these stub objects.

My first thought was having 2 stubs for each test case, one for the initial array and other for the sorted array. Maybe doing something like this:

//test case: should prioritize duration over raw priority

export const DifferentPriorityAndDurationStub = () => {
  const t1 = {priority: 1, duration: 20};
  const t2 = {priority: 2, duration: 30};
  return [t1, t2];
};

export const DifferentPriorityAndDurationStubResult= () => {
  const t1 = {priority: 1, duration: 20};
  const t2 = {priority: 2, duration: 30};
  return [t2, t1];
};

Then, in the actual test:

describe('Custom Sorter', () => {
    it('should prioritize duration over raw priority', () => {
      const originalArray = DifferentPriorityAndDurationStub();
      const result = DifferentPriorityAndDurationStubResult();
      expect(customSorter.sort( originalArray )).toEqual(result );
    });
  });

My problem with this approach is that if someone needs to work on the function, the test's code would not tell them exactly what is in the inputs and what is in the output, they would need to go to the stubs file. I think this makes it difficult to understand how the sort functions should work in this specific case.

The other way I thought about was something like:

export const Priority1Duration20 = () => {
  return {priority: 1, duration: 20};
};

export const Priority2Duration30 = () => {
  return {priority: 2, duration: 30};
};

And in my test do something like this:

describe('Custom Sorter', () => {
    it('should prioritize duration over raw priority', () => {
      const Priority1Duration20 = Priority1Duration20();
      const Priority2Duration30 = Priority2Duration30();
      expect(customSorter.sort( [Priority1Duration20, Priority2Duration30] ))
            .toEqual([Priority2Duration30,Priority1Duration20]);
    });
  });

I think this seems more readable if someone needs to understand how the code works. But it also worries me that I need to make changes to the Stubs file and the test file to change or fix certain test cases. In the first approach I'd just need to fix the stub and that would be all.

Any ideas? how would you approach this?

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!