(Jest)test.each()不支持async body?

ruarlubt  于 2023-06-27  发布在  Jest
关注(0)|答案(1)|浏览(144)

(我在这里问之前报告为一个错误,在仓库中所述。
test.each()方法在与异步体一起使用时似乎给了我错误的结果。

示例

这正确地成功:

async function getVal(n) {
  return n === 1 ? "one" : "two";
}

test.each([
  [1, "one"],
  [2, "two"]
])("getVal()", async function (n, expected) {
  const val = await getVal(n);

  expect(val).toEqual(expected);
});

如果我改变了第一个期望值,测试就会失败(所以它仍然有效)。

test.each([
  [1, "SOME VALUE"],
  [2, "two"]
])("getVal()", async function (n, expected) {
  const val = await getVal(n);

  expect(val).toEqual(expected);
});

但是如果我改变了第二个期望值,测试就会错误地说它成功了(就好像它根本没有真正检查第二次迭代)。

test.each([
  [1, "one"],
  [2, "SOME VALUE"]
])("getVal()", async function (n, expected) {
  const val = await getVal(n);

  expect(val).toEqual(expected);
});

我错过了什么吗?

92vpleto

92vpleto1#

问题出在我使用的Test Explorer扩展(Jest Test Explorer)上。
如果它的第一个用例成功,它将报告test.each()成功(不管其他用例如何)。

相关问题