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)
更新:
它似乎test是Jest的官方API,但事实it并非如此.
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)
文件:
它们的作用相同,但它们的名称不同,并且它们与测试名称的交互作用也不同。
测试
你写什么:
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读取未编写的失败测试结果确实很有意义。它有助于更快地了解测试内容。
正如笑话文档所说,它们是相同的: 它别名
测试(名称,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)
这不是必需的 - 您可以直接在顶层编写测试块。但是,如果您希望将测试组织成组,这会很方便。
| 归档时间: |
|
| 查看次数: |
43711 次 |
| 最近记录: |