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?
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?
-
2
Based on the docs, it doesn't seem to be possible. I would use the automatic mapper like you're using now, but I would make it a function that can be reused, and fix the issue of mutating
eachArr
:const indexer = (table) => table.map((row, idx) => [...row, idx]);
and calling it astest.each(indexer(table))(...)
– Samathingamajig Commented Nov 30, 2021 at 4:55 - @Samathingamajig, yes having a function makes sense and thanks for pointing out the mutating issue. – Kaushik R Bangera Commented Nov 30, 2021 at 6:49
3 Answers
Reset to default 4Get the index of the current array element in the test suite
const MOCK_DATA = [
{
key: 'item_1',
value: 'value_1',
},
{
key: 'item_2',
value: 'value_2',
},
{
key: 'item_3',
value: 'value_3',
},
]
describe.each(MOCK_DATA)('item № %#',item => {
it('your test case', () => {
/* ... */
/**
* one of ways to get the index of the current array element
*
* ↓↓↓
*/
const currentIndex = MOCK_DATA.indexOf(item)
/* ... */
});
})
I'm not sure if the index of the test case can differ from the index of the array element used in the test case. Therefore, the option below is also appropriate.
Get the index of the current test case in the test suite
The workaround by @kaushik-r-bangera
In case someone else's looking for this, declaring the index outside should do the trick:
let currentTestIndex = 0
test.each(["Adam", "Ron", "Roy"])( "Student: %s", ( name, done ) => {
if ( currentTestIndex == 0 ) expect( name ).toEqual( "Adam" )
if ( currentTestIndex == 2 ) expect( name ).toEqual( "Roy" )
currentTestIndex += 1
return done()
})
I stumbled upon this answer, which shows how to get the name of the current test
console.log(expect.getState().currentTestName);
So now we just add this index place holder in the name of the test
%# - Index of the test case.
function getCurrentTestIndex() {
const testName = expect.getState().currentTestName;
const testIndexPos = testName.lastIndexOf(":")+1;
const currentTestIndex = parseInt( testName.substring( testIndexPos ) )
return currentTestIndex;
}
test.each(["Adam", "Ron", "Roy"])( "Student: %s :%#", ( name, done ) => {
const currentTestIndex = getCurrentTestIndex();
if ( currentTestIndex == 0 ) expect( name ).toEqual( "Adam" )
if ( currentTestIndex == 2 ) expect( name ).toEqual( "Roy" )
return done()
})
You could keep the getCurrentTestIndex()
in global if you want to use it across your test files and would like to avoid writing import statement everywhere
global.getCurrentTestIndex = getCurrentTestIndex;
So now we are getting the index from jest itself, but we have to add the index in the test name and extract it.
But it would certainly be nicer to get currentTestIndex
in similar manner as how we can get the currentTestName
.
In case if you are looking for one liner
getCurrentTestIndex = _ => parseInt( expect.getState().currentTestName.split(":").at(-1) )