在开玩笑中'it'和'test'有什么区别?

C.L*_*Lee 194 javascript unit-testing jestjs

我的测试组中有两个测试.一个使用它,另一个使用测试,它们似乎工作非常相似.他们之间有什么区别?

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);
        })
  });
});
Run Code Online (Sandbox Code Playgroud)

更新:

它似乎testJest的官方API,但事实it并非如此.

mus*_*ons 267

这里说的文档:是别名.所以他们完全一样.ittest

  • 我认为这个答案有点误导——“it”和“test”并不完全相同。正如 @gwilde 答案 /sf/answers/3925059071/ 中所解释的,这些别名鼓励开发人员将测试名称编写为可读的英语句子 - 因此随机使用“it”或“test”会搞砸。 (7认同)

Ric*_*ich 13

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

我相信提供这两者是为了允许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();
  });
});
Run Code Online (Sandbox Code Playgroud)

或2)“ xUnit ”风格的测试,例如:

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

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

文件:


gwi*_*ldu 9

它们的作用相同,但它们的名称不同,并且它们与测试名称的交互作用也不同。

测试

你写什么:

describe('yourModule', () => {
  test('if it does this thing', () => {}
  test('if it does the other thing', () => {}
})
Run Code Online (Sandbox Code Playgroud)

如果发生故障,您会得到什么:

describe('yourModule', () => {
  test('if it does this thing', () => {}
  test('if it does the other thing', () => {}
})
Run Code Online (Sandbox Code Playgroud)

你写什么:

describe('yourModule', () => {
  it('should do this thing', () => {}
  it('should do the other thing', () => {}
})
Run Code Online (Sandbox Code Playgroud)

如果发生故障,您会得到什么:

yourModule > if it does this thing
Run Code Online (Sandbox Code Playgroud)

因此,这是关于可读性,而不是功能。在我看来,it读取未编写的失败测试结果确实很有意义。它有助于更​​快地了解测试内容。

  • 有些人还更喜欢 `it('does this thing', () => {})` 而不是 `it('should do this thing', () => {}` 因为它更短 (8认同)
  • `test('thing should do x')` 的字面意思是“测试该事物应该执行 X”——因此测试读起来就像人们所说的那样。`test('thing does x')` 也可以。 (3认同)

adr*_*rhc 9

您可以替换it()xit()暂时排除测试的执行;使用it()and比使用andxit()更有说服力。test()xit()

请参阅聚焦和排除测试

  • 还有一个“xtest”,所以两者非常相似。 (4认同)

Sey*_*don 8

正如笑话文档所说,它们是相同的: 它别名

测试(名称,fn,超时)

同样在别名下:it(name, fn, timeout)

并且describe仅用于当您希望将测试组织成组时: 描述

描述(名称,fn)

describe(name, fn)创建一个将几个相关测试组合在一起的块。例如,如果您有一个 myBeverage 对象,它应该是美味但不酸的,您可以使用以下命令对其进行测试:

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

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

  test('is not sour', () => {
    expect(myBeverage.sour).toBeFalsy();
  });
});
Run Code Online (Sandbox Code Playgroud)

这不是必需的 - 您可以直接在顶层编写测试块。但是,如果您希望将测试组织成组,这会很方便。