¿En qué tipo de locura asíncrona me he metido?
¡La prueba falla, luego registra el token de acceso!
import assert from "assert"; import "dotenv/config"; import { expect } from "chai"; var unirest = require("unirest"); describe("some tests", function () { let accessToken: string | undefined; beforeEach(function () { const email = process.env.EMAIL; const password = process.env.PASSWORD; const login = async function () { const req = await unirest( "POST", `${process.env.API_BASE_URL}/auth/local/login` ) .headers({ "Content-Type": "application/json", }) .send(JSON.stringify({ email, password })) .end(function (res: any) { if (res.error) throw new Error(res.error); accessToken = JSON.parse(res.raw_body).access_token; console.log(accessToken); // this logs after the test fails? why? }); }; login(); }); it("should be able to login and get an access token", async function () { expect(accessToken).to.not.be.undefined; }); });el error es
AssertionError: expected undefined not to be undefinedTambién intenté devolver el inicio de sesión () pero aún se registra después de que falla
beforeEach(function () { const email = process.env.EMAIL; const password = process.env.PASSWORD; const login = async function () { const req = await unirest( "POST", `${process.env.API_BASE_URL}/auth/local/login` ) .headers({ "Content-Type": "application/json", }) .send(JSON.stringify({ email, password })) .end(function (res: any) { if (res.error) throw new Error(res.error); accessToken = JSON.parse(res.raw_body).access_token; console.log(accessToken); // this logs after the test fails? why? }); }; return login(); });