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

262
Views
How to get current test index in jest

How do I get index of current test in test.each in jest

This is how I'm doing at the moment

test.each([
  [ "Adam", 0 ], // specifying index manually
  [ "Ron", 1 ],
  [ "Roy", 2 ],
])( "Student: %s", ( name, currentTestIndex, done ) => {
  if ( currentTestIndex == 0 ) expect( name ).toEqual( "Adam" )
  if ( currentTestIndex == 2 ) expect( name ).toEqual( "Roy" );
  return done()
} )

How to do it without specifying the index manually

test.each([
  [ "Adam"], // not specifying the index
  [ "Ron" ],
  [ "Roy" ],
])( "Student: %s", ( name, currentTestIndex, done ) => {

  // how to get current test's index ?
  if ( currentTestIndex == 0 ) expect( name ).toEqual( "Adam" )
  if ( currentTestIndex == 2 ) expect( name ).toEqual( "Roy" );
  return done()
} )

Edit:

Doing it this way now, by creating another array with map and adding index as the first element

test.each([
  [ "Adam" ],
  [ "Ron"  ],
  [ "Roy"  ],
].map( (eachArr,ind) => { //creating new array and adding index as the 1st element
  eachArr.unshift( ind ); 
  return eachArr;
}))( "Student: %s", ( currentTestIndex, name, done ) => {
  if ( currentTestIndex == 0 ) expect( name ).toEqual( "Adam" )
  if ( currentTestIndex == 2 ) expect( name ).toEqual( "Roy" );
  return done()
});

any other way to get the index, from jest itself without creating a new array like above?

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!