I often perform chai requests with authorization:
chai.request(baseUrl).get(`/resource`).set('authorization', `Bearer ${token}`)
I would like to factorize the autorization in a function called withAuth
, so I can reuse it more conveniently:
chai.request(baseUrl).get(`/resource`).withAuth()
But I'm not sure how to do that? I tried:
let withAuth = () => {set('authorization', `Bearer ${token}`)}
But this doesn't work.
Use addChainableMethod
of Assertion prototype :
chai.Assertion.addChainableMethod('withAuth',(request) => request.set('authorization', `Bearer ${token}`));
You can refer to this documentation and have a look on the utility method addChainableMethod