I'm trying to test the routes in my express
app that are protected by a jwt middleware. I've tried simulating a request to get the jwt token in a beforeAll
call:
let token = "";
beforeAll((done) => {
supertest(app)
.get("/authentication/test")
.end((err, response) => {
console.log(response.body); // "fewiu3438y..." (successfully got token)
token = response.body.token; // Tried to update token variable with jwt token
done();
});
});
console.log(token); // "" (I haven't updated the token variable)
So when I try to run subsequent tests, I don't have a valid authentication token to use in the headers:
describe("Simple post test using auth", () => {
test.only("should respond with a 200 status code", async () => {
console.log({ POSTTest:token }); // "" still not set, so test will fail.
const response = await supertest(app).post("/tests/simple").send();
expect(response.statusCode).toBe(200);
});
});
Is there a way to update a variable, or else set all headers using a beforeAll
? Or is there a better method I'm not aware of?