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?