Jest在所有测试之前、之后运行异步端点



[Junior dev!!]

->节点v18.08
->笑话29.0.1
->MySQL

嗨,我正在API上每个端点的不同文件夹中运行几个笑话测试。我有几个文件,我正在创建一个帐户=>测试多个端点=>清理数据库。。我至少有14个这样的文件。所以…当这14个文件中的一个端点不工作时,测试直到最后才进行并清理数据库,所以我需要返回并更改用户名。。显然是在浪费时间。。

我以前一直在学习胡克,但它们都不起作用。我将在这里显示我的代码没有挂钩(测试工作(

const axios = require("axios");
const API_URL = process.env.API_TEST_URL;
let TOKEN;
//Creating User
test("POST test register user", async () => {
const data = {
pseudo: "TEST",
password: "123",
date: "2022-08-29 16:31:25",
};
const url = `${API_URL}/register`;
const response = await axios.post(url, data);
expect(response.status).toBe(200);
expect(response.data).toHaveProperty("token");
TOKEN = response.data.token;
});
//Endpoints to test
test("POST/test", async () => {
const url = `${API_URL}/data/actions`;
const data = {
action: "running",
date: "2021-09-30 18:14:24",
};
const headers = { Authorization: `Bearer ${TOKEN}` };
const response = await axios.post(url, data, { headers });
expect(response.status).toBe(200);
});
//Deleting all info from DB
test("DELETE test account", async () => {
const url = `${API_URL}/user/delete/all-data`;
const headers = { Authorization: `Bearer ${TOKEN}` };
const response = await axios.delete(url, { headers });
expect(response.status).toBe(200);
});

当我想添加钩子时,不起作用

const axios = require("axios");
const API_URL = process.env.API_TEST_URL;
let TOKEN;
//creating before all an user
beforeAll(() => {
test("POST test register user", async () => {
const data = {
pseudo: "TEST",
password: "123",
date: "2022-08-29 16:31:25",
};
const url = `${API_URL}/register`;
const response = await axios.post(url, data);
expect(response.status).toBe(200);
expect(response.data).toHaveProperty("token");
TOKEN = response.data.token;
});
});
//testing endpoints
test("POST/action test", async () => {
const url = `${API_URL}/data/actions`;
const data = {
action: "running",
date: "2021-09-30 18:14:24",
};
const headers = { Authorization: `Bearer ${TOKEN}` };
const response = await axios.post(url, data, { headers });
expect(response.status).toBe(200);
});
// if the endpoint doesn't work afterAll clean all DB
afterAll(() => {
test("DELETE test account", async () => {
const url = `${API_URL}/user/delete/all-data`;
const headers = { Authorization: `Bearer ${TOKEN}` };
const response = await axios.delete(url, { headers });
expect(response.status).toBe(200);
});
});

你怎么看。你能帮我吗?因为jest上的信息只是显示如何使用函数来完成它。。我们可以先测试一下里面的东西吗;毕竟??

我正在处理的项目中有类似的东西。我必须确保beforeAll是一个异步函数,并放入等待。

beforeAll(async () => {
connection = await createConnection();
await connection.runMigrations();
const id = uuidv4();
const password = await hash('admin', 8);
await connection.query(
`insert into users (id, name, email, password, is_admin, created_at, driver_license)
values ('${id}', 'Admin', 'admin@test.com.br', '${password}', true, 'now()', 'XXXXXXX')`
);
});
afterAll(async () => {
await connection.dropDatabase();
await connection.close();
});

另一件事是,我在beforeAll中使用了test函数进行了一个测试,但它返回了一个错误,所以可能只执行post而不进行测试就可以了。

编辑:阅读beforeAll和beforeEach中关于测试的文档,它说您不能在其中使用测试。

最新更新