How to Make Authenticated Requests on Supertest

Use beforeEach funtion to create header with token to set for each test.

let headers
beforeEach(async () => {
const newUser = {
username: 'janedoez',
name: 'Jane Z. Doe',
password: 'password',
}
await api
.post('/api/users')
.send(newUser)
const result = await api
.post('/api/login')
.send(newUser)
headers = {
'Authorization': `bearer ${result.body.token}`
}
})

Then in each test, set header.

await api
.post('/api/blogs')
.send(newBlog)
.set(headers)
.expect(201)
.expect('Content-Type', /application\/json/)

Source -> Supertest