'it'和& # 39;测试# 39;在开玩笑吗?

我的测试组中有两个测试。其中一个测试使用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的官方APItestit似乎是可以互换的。正如@gwildu所描述的在这里,你应该为了可读性而选择其中一个。

176564 次浏览

Jest文档状态#EYZ0是test的别名所以从功能的角度来看它们是完全一样的。它们的存在都是为了让你在考试中写出一个可读的英语句子。

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

显然,“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;

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

我相信这两者都是为了允许1)“;样式测试如下:

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

文档:

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

<强> # EYZ0 < / >强

你写什么:

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

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

yourModule > if it does this thing

<强> # EYZ0 < / >强

你写什么:

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符号。

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

我猜,这只是惯例。test用于单元测试,it用于集成测试。

正如笑话文档所说,它们是相同的: # EYZ0 < / p >

测试(名称,fn,超时)

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

describe只是当你喜欢你的测试被组织成组: # EYZ0 < / p >

描述(名称、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();
});
});

这不是必需的—您可以直接在顶层编写测试块。但是如果你喜欢把你的测试组织成小组,这就很方便了。

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

看到# EYZ0

以下是文件节选:链接

# EYZ0

别名:it(name, fn, timeout)

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