Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

258
Vistas
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
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda