我的测试组中有两个测试。一个测试使用它,另一个使用测试。它们的工作原理似乎非常相似。它们之间的区别是什么?
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的官方API,测试和它似乎是可以互换的。正如这里所描述的@gwildu,出于可读性考虑,您应该选择其中一种。
正如笑话文档所说,它们是一样的:
它的别名
测试(名称,fn,超时)
同样在别名下:it(name, fn, timeout)
描述只是为了让你的测试被分成几组
描述
描述(名字,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();
});
});
这不是必需的—您可以直接在顶层编写测试块。但是如果你喜欢把你的测试组织成小组,这就很方便了。
Const request = require('supertest');
Const app = require('../app')
Const {it, describe} = require('@jest/globals');
Const {sequelize} = require('../models');
Const {hash} = require('../helpers/bcrypt')
beforeAll(async()=>{等待sequize . queryinterface。bulkInsert('Customers',[{用户名:"nikita ",电子邮件:"nikita@mail.com",密码:hash("nikita"), createdAt: new Date(), updatedAt: new Date()}])})
afterAll(async()=>{等待sequel . queryinterface . afterAll)bulkDelete('Customers', null, {truncate: true, cascade: true, restartIdentity: true})})
describe('POST /customers/register', () => { it('should response with status 201', async ()=> { let customer = { username: "hello" , email:"hello@mail.com", password:"hello", } let response = await request(app) .post('/customers/register').send(customer) expect(response.status).toBe(201) // console.log(response.body, 'ini ressss') expect(response.body).toEqual({ message: "Input data Customer succeed", id: expect.any(Number), email: expect.any(String) }) });
it('should response with status 400', async()=>{ let customer = { username: "hello", password:"hello" } let response = await request(app) .post('/customers/register').send(customer) expect(response.status).toBe(400) // console.log(response.body,"<<<"); expect(response.body[0].message).toBe('Please input email') })