Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

290
Views
¿Cómo puedo pasar mi token de portador a las próximas pruebas?

Quiero obtener mi token de portador, guardarlo y pasarlo a las siguientes pruebas, pero cuando trato de hacer esto, el token no se pasa y cuando imprimo token en la prueba puedo ver "indefinido"

 import supertest from "supertest"; const baseUrl = "https://api.staging.myapi"; let token; before(() => { supertest("https://identitytoolkit.googleapis.com") .post( "/mykeyUrl" ) .send({ email: "user", password: "password", returnSecureToken: true, }) .set("Authorization", "bearer " + token) .end((err, response) => { token = response.body.idToken; }); }); describe("Create an unit", () => { console.log(token) it("should create an unit", async () => { console.log(token) const res = await supertest(baseUrl) .post("myUrl") .send({ name: "SuperTestUni", location: "Warsaw", }) .set("Authorization", "bearer " + token) .expect(201) }); });

¿Cómo puedo pasar este token a la próxima prueba?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Intenta usar 'async' antes:

 before(async () => { const response= await supertest("https://identitytoolkit.googleapis.com") .post( "/mykeyUrl" ) .send({ email: "user", password: "password", returnSecureToken: true, }) .set("Authorization", "bearer " + token) .end((err, response); token = response.body.idToken; });

Tengo un caso similar que funciona bien:

 let token = ""; beforeAll(async () => { const response = await request(baseUrl).get("/auth").send({ username: "test@example.com", password: "password", }); token = response.body.access_token; }); describe("Posts endpoint", () => { it("should be able to create a post", async () => { const response = await request(baseUrl) .post("/posts") .send({ title: "foo", body: "bar", user_id: 2139, }) .set("Authorization",`Bearer ${token}`); expect(response.statusCode).toBe(201); }); });
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!