Jest run async endpoints beforeall, afterall tests

Viewed 42

[Junior dev!!]

->Node v18.08
->jest 29.0.1
->MySQL

Hi i m running several jest test in diferent folders of each endpoint on my API. I have several files where i m creating an account => test several endpoints => clean DB.. I have at least 14 files like these. So.. When an endpoint in those 14 file is not working the test doesn't go until the end and clean the DB so i need to go back and change the user name.. A clearly waste of time..

I'm learning about the Hook beforeAll and afterAll but they are not working. i will show here my code without the hooks (test working)

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

And when i want to add the hooks doesn't work

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

what do you think. Could you help me ? Because the info on jest is only showing how to do it with a function.. Can we test something inside beforeAll & afterAll ??

1 Answers

I have something similar in a project that I'm working on. I had to make sure that the beforeAll was an async function and put the await.

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

Another thing is that I have done a test using a test function inside the beforeAll and it has returned an error, so maybe just executing the post without the test may work.

Edit: Reading the docs about the test inside the beforeAll and beforeEach, it says that you can not use a test inside it.

Related