Javascript 开玩笑中的“它”和“测试”有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/45778192/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 03:12:58  来源:igfitidea点击:

What is the difference between 'it' and 'test' in jest?

javascriptunit-testingjestjs

提问by C.Lee

I have two tests in my test group. One uses it the other one uses test, and they seem to be working very similarly. What is the difference between them?

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

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);
        })
  });
});

UPDATE:

更新:

It seems that testis in the official API of Jest, but itis not.

似乎testJest 的官方 API 中,但it不是。

回答by musicformellons

In the docsit says here: itis an alias of test. So they are exactly the same.

文档中,在这里说:ittest. 所以它们完全一样。

回答by gwildu

They do the same thing, but their names are different and with that their interaction with the name of the test.

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

test

测试

What your write:

你写什么:

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

What you get if something fails:

如果出现故障,您会得到什么:

yourModule > if it does this thing

it

What you write:

你写什么:

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

What you get if something fails:

如果出现故障,您会得到什么:

yourModule > should do this thing

So it's about readability not about functionality. In my opinion, itreally has a point when it comes to read the result of a failing test that you haven't written yourself. It helps to faster understand what the test is about.

所以它是关于可读性而不是关于功能。在我看来,it在阅读不是自己编写的失败测试的结果时确实有一定的意义。它有助于更​​快地了解测试的内容。

回答by Rich

As the other answers have clarified, they do the same thing.

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

I believe the two are offered to allow for either 1) "RSpec" style tests like:

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

or 2) "xUnit" style tests like:

或 2) " xUnit" 样式测试,如:

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

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

Docs:

文档:

回答by SeyyedKhandon

as the jest docs says, they are the same: https://jestjs.io/docs/en/api#testname-fn-timeout

正如笑话文档所说,它们是相同的:https: //jestjs.io/docs/en/api#testname-fn-timeout

test(name, fn, timeout)

Also under the alias: it(name, fn, timeout)

测试(名称,fn,超时)

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

and describe is just for when you prefer your tests to be organized into groups: https://jestjs.io/docs/en/api#describename-fn

和描述仅适用于您希望将测试组织成组的情况:https: //jestjs.io/docs/en/api#describename-fn

describe(name, fn)

描述(名称,fn)

describe(name, fn)creates a block that groups together several related tests. For example, if you have a myBeverage object that is supposed to be delicious but not sour, you could test it with:

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();
  });
});

This isn't required - you can write the test blocks directly at the top level. But this can be handy if you prefer your tests to be organized into groups.

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

回答by kishor borate

Jest haven't mentioned why they have two versions for the exact same functionality. My guess is, it's only for convention. test for unit tests it for integration tests.

Jest 没有提到为什么他们有两个版本来实现完全相同的功能。我的猜测是,它仅用于约定。测试单元测试它的集成测试。

回答by u8155716

They are the same thing. I am using TypeScript as the programming language, and when I look into the definition file from jest package source code from /@types/jest/index.d.ts, I can see the following codes. Obviously there are lots of different names of 'test', you can use any of them.

他们是一样的东西。我使用 TypeScript 作为编程语言,当我查看来自 /@types/jest/index.d.ts 的 jest 包源代码的定义文件时,我可以看到以下代码。显然,“测试”有很多不同的名称,您可以使用其中的任何一个。

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;