How to get token via test with jest

Asked

Viewed 7 times

-1

I’ve been having trouble finding a solution when developing automated testing with Facebook jest when it comes to getting token to test authentication. With this code does not return the token. But there must be a way that I did not find to get the token using JWT and return successful authentication.

    it('should return jwt token when authenticated', async () => {
    const user = await factory.create('User', {
      password: '123456',
    });

    const response = await request(app).post('/api/signin').send({
      username: user.username,
      password: '123456',
    });

    expect(response.body).toHaveProperty('acesssToken');
  });
  it('should be able to access private routes when authenticated', async () => {
    const user = await factory.create('User', {
      password: '123123'
    })

    const response = await request(app)
      .get('/api/users')
      .set('Authorization', 'Bearer')

    expect(response.status).toBe(200);
  });
  • Why not call the code that returns the Token in the second test? Something like const responseToken = ( await request(app).post('/api/signin').send({...}) ).body.acesssToken.

  • Well thought, it’s the first time I use the jest. I’m studying it.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.