【问题标题】:What is the difference between 'it' and 'test' in Jest?Jest 中的“it”和“test”有什么区别?
【发布时间】:2018-01-28 09:53:07
【问题描述】:

我的测试组中有两个测试。其中一项测试使用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);
        })
  });
});

似乎testthe official API of Jest 中,但it 不在。

【问题讨论】:

  • it 可能只是为了熟悉和从其他框架迁移。
  • 没有区别。文档清楚地指出 test 在别名 it 下。

标签: javascript unit-testing jestjs


【解决方案1】:

以下为文档节选:link

test(name, fn, timeout)

别名:it(name, fn, timeout)

测试文件中只需要运行测试的测试方法。为了 例如,假设有一个函数 inchesOfRain() 应该是 零。你的整个测试可能是:......

【讨论】:

    【解决方案2】:

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

    Focusing and Excluding Tests

    【讨论】:

    • 还有一个“xtest”,所以两者非常相似。
    【解决方案3】:

    Jest docs 状态为 it is an alias of test。因此,从功能的角度来看,它们完全相同。它们的存在都是为了使您的测试中可以读懂英文句子。

    【讨论】:

    • 我认为这个答案有点误导 - ittest 并不完全相同。正如@gwilde answer stackoverflow.com/a/56072272/449347 中所解释的那样,这些别名鼓励开发人员将测试名称写成可读的英文句子——所以随机使用ittest 会搞砸。
    • @k1eran 你是对的!我相应地编辑了答案。
    【解决方案4】:

    正如开玩笑的文档所说,它们是相同的: it alias

    测试(名称、fn、超时)

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

    describe 仅适用于您希望将测试组织成组的情况: 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();
      });
    });
    

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

    【讨论】:

      【解决方案5】:

      Jest 没有提到为什么他们有两个版本具有完全相同的功能。

      我的猜测是,这只是为了约定。 test 用于单元测试,it 用于集成测试。

      【讨论】:

        【解决方案6】:

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

        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 表示法。

        【讨论】:

        • 有些人更喜欢it('does this thing', () => {})而不是it('should do this thing', () => {},因为它更短
        • 另外,test('thing should do x') 可能比it('Should do X') 更受欢迎,因为it 通常含糊不清。
        • @mikemaccana 如果你写test('thing should do x'),你没有一个语义正确的句子。我认为这些符号背后的想法实际上是,您可以像说话一样阅读句子中的测试。如果你写test('Does this thing'),也一样。当然你可以这样做,但语义上的符号实际上适合 it 符号
        • test('thing should do x') 字面意思是“测试那个东西应该做 X”——所以测试读起来就像一个人会说话。 test('thing does x') 也可以。
        【解决方案7】:

        正如其他答案已经阐明的那样,他们做同样的事情。

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

        文档:

        【讨论】:

          【解决方案8】:

          它们是一样的。我使用 TypeScript 作为编程语言,当我从 /@types/jest/index.d.ts 的 Jest 包源代码中查看定义文件时,可以看到以下代码。

          显然,'test' 有很多不同的名称,你可以使用其中的任何一个。

          declare var beforeAll: jest.Lifecycle;
          declare var beforeEach: jest.Lifecycle;
          declare var afterAll: jest.Lifecycle;
          declare var afterEach: jest.Lifecycle;
          declare var describe: jest.Describe;
          declare var fdescribe: jest.Describe;
          declare var xdescribe: jest.Describe;
          declare var it: jest.It;
          declare var fit: jest.It;
          declare var xit: jest.It;
          declare var test: jest.It;
          declare var xtest: jest.It;

          【讨论】:

          • 您显示的代码并不表示ittest 是一回事。这只是意味着它们的类型是相同的。我不认为beforeAllafterAll 是同一件事,即使它们的类型相同。
          • xit and xtest 跳过测试,it, fit, test 将执行测试。谢谢你的回答。
          猜你喜欢
          • 2015-11-10
          • 2017-12-16
          • 2021-02-20
          • 2021-12-04
          • 1970-01-01
          • 2011-03-25
          • 2019-10-12
          • 2017-07-25
          • 1970-01-01
          相关资源
          最近更新 更多