【问题标题】:How to get current test index in jest如何在笑话中获取当前测试索引
【发布时间】:2023-02-02 01:25:07
【问题描述】:

如何在jest中获取test.each中当前测试的索引

这就是我现在正在做的

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()
} )

如何在不手动指定索引的情况下执行

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()
} )

编辑:

现在这样做,通过使用 map 创建另一个数组并将索引添加为第一个元素

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()
});

还有其他方法可以从jest 本身获取索引,而无需像上面那样创建新数组吗?

【问题讨论】:

  • 根据docs,好像不行。我会像你现在使用的那样使用自动映射器,但我会让它成为一个可以重复使用的函数,并解决变异 eachArr: const indexer = (table) => table.map((row, idx) => [...row, idx]); 并将其称为 test.each(indexer(table))(...) 的问题
  • @Samathingamajig,是的,有一个功能很有意义,感谢您指出变异问题。

标签: javascript jestjs


【解决方案1】:

如果其他人正在寻找这个,在外面声明索引应该可以解决问题:

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()
})

【讨论】:

  • 就是感觉一定有更好的办法
【解决方案2】:

我偶然发现了这个答案,它展示了如何获得name of the current test

console.log(expect.getState().currentTestName);


所以现在我们只需在测试名称中添加这个index place holder

%# - 测试用例的索引。


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()
})

如果你想在你的测试文件中使用它并且想避免在任何地方编写 import 语句,你可以将 getCurrentTestIndex() 保留在全局中

global.getCurrentTestIndex = getCurrentTestIndex;


所以现在我们从 jest 本身获取索引,但我们必须在测试名称中添加索引并提取它。

但是,以与获取 currentTestName 类似的方式获取 currentTestIndex 肯定会更好。


如果您正在寻找一个班轮

getCurrentTestIndex = _ => parseInt( expect.getState().currentTestName.split(":").at(-1) )

【讨论】:

    猜你喜欢
    • 2019-10-02
    • 2018-12-23
    • 1970-01-01
    • 2019-04-13
    • 2011-06-28
    • 2018-08-09
    • 2019-09-06
    • 2020-10-20
    • 2018-08-13
    相关资源
    最近更新 更多