te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>javascript - How to get current test index in jest - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to get current test index in jest - Stack Overflow

programmeradmin3浏览0评论

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?

Share Improve this question edited Nov 30, 2021 at 4:48 Kaushik R Bangera asked Nov 29, 2021 at 3:27 Kaushik R BangeraKaushik R Bangera 5815 silver badges12 bronze badges 2
  • 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 as test.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
Add a ment  | 

3 Answers 3

Reset to default 4

Get 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) )
发布评论

评论列表(0)

  1. 暂无评论