Jest 中的“it”和“test”有什么区别?

新手上路,请多包涵

我的测试组中有两个测试。其中一项测试使用 it ,另一项使用 test 。他们两个的工作方式似乎非常相似。它们之间有什么区别?

 describe('updateAll', () => {
  it('no force', () => {
    return updateAll(TableName, ["fileName"], {compandId: "test"})
        .then(updatedItems => {
          let undefinedCount = 0;
          for (let item of updatedItems) {
            undefinedCount += item === undefined ? 1 : 0;
          }
          // console.log("result", result);
          expect(undefinedCount).toBe(updatedItems.length);
        })
  });

  test('force update', () => {
    return updateAll(TableName, ["fileName"], {compandId: "test"}, true)
        .then(updatedItems => {
          let undefinedCount = 0;
          for (let item of updatedItems) {
            undefinedCount += item === undefined ? 1 : 0;
          }
          // console.log("result", result);
          expect(undefinedCount).toBe(0);
        })
  });
});


更新 - 2022 年 11 月:

根据 Jest 的官方 API, 似乎 testit 是可以互换的。正如 @gwildu 此处 所述,为了 可读性,您应该选择一个而不是另一个。

原文由 C.Lee 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 4.1k
2 个回答

Jest 文档 状态 ittest 的别名。所以从功能的角度来看,它们是完全一样的。它们的存在都是为了让您的测试能够形成可读的英语句子。

原文由 musicformellons 发布,翻译遵循 CC BY-SA 4.0 许可协议

他们做同样的事情,但他们的名字不同,而且他们与测试名称的交互也不同。

test

你写什么:

 describe('yourModule', () => {
  test('if it does this thing', () => {});
  test('if it does the other thing', () => {});
});

如果某事失败,你会得到什么:

 yourModule > if it does this thing

it

你写什么:

 describe('yourModule', () => {
  it('should do this thing', () => {});
  it('should do the other thing', () => {});
});

如果某事失败,你会得到什么:

 yourModule > should do this thing

所以它是关于可读性而不是关于功能。

在我看来, it 在阅读不是您自己编写的失败测试的结果时确实有意义。它有助于更快地了解测试的内容。

一些开发人员还将 Should do this thing 缩短为 Does this thing 这有点短,并且在语义上也适合 it 表示法。

原文由 gwildu 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题