-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.– Cmte Cardeal
Well thought, it’s the first time I use the jest. I’m studying it.
– Leandro