提问者:小点点

笑话中的“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);
        })
  });
});

更新:

看起来test在Jest的官方API中,但It不是。


共3个答案

匿名用户

Jest文档状态ittest的别名。所以它们是完全一样的。

匿名用户

它们做同样的事情,但是它们的名字不同,因此它们与测试名称的交互作用也不同。

测试

你写什么:

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

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

yourModule > does this thing

你写的内容:

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

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

yourModule > should do this thing

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

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

匿名用户

正如其他答案所阐明的,它们做的是同样的事情。

我相信提供这两种测试是为了允许1)“RSpec”风格的测试,例如:

const myBeverage = {
  delicious: true,
  sour: false,
};

describe('my beverage', () => {
  it('is delicious', () => {
    expect(myBeverage.delicious).toBeTruthy();
  });

  it('is not sour', () => {
    expect(myBeverage.sour).toBeFalsy();
  });
});

或者2)“xUnit”样式的测试,如:

function sum(a, b) {
  return a + b;
}

test('sum adds 1 + 2 to equal 3', () => {
  expect(sum(1, 2)).toBe(3);
});

文档:

  • https://jestjs.io/docs/en/api.html#descripename-fn
  • https://jestjs.io/docs/en/api.html#testname-fn-timeout