Skip to content Skip to sidebar Skip to footer

Extending Supertest

I like to use SuperTest to work with my auth system like this: const request = require('./valid_access_token')(require('supertest')); request(app) .get('/v1/bots')

Solution 1:

All the credit goes to https://github.com/acabreragnz

const request = require('supertest');
constTest = request.Test;

Test.prototype.authenticate = function(user) {
  const {token, xsrfToken} = user.tokens;

  returnthis
   .set('Authorization', `Bearer ${token}`)
   .set('X-XSRF-TOKEN', xsrfToken);
}

Then in your tests

return request(app)
  .post('/user/settings')
  .authenticate(user)
  .send(...)

https://github.com/visionmedia/supertest/issues/381#issuecomment-282198972

Post a Comment for "Extending Supertest"