0

I'm trying to test a authenticated route.

This is my code:

let request = require('supertest');
var superagent = require('superagent');
var agent = superagent.agent();
var theAccount = {
  name: '*********',
  role: 'admin',
  id: '115039452833383267752'
};
request = request('http://localhost:3000');

describe('Live-score', () => {

  before(function (done) {
    request
      .post('/api/login')
      .send(theAccount)
      .end(function (err, res) {
        if (err) {
          throw err;
        }
        agent.saveCookies(res);
        done();
      });
  });

  it('Should work', (done) => {
    agent.attachCookies(req);
    request
      .get('/api/live-score')
      .send(agent)
      .set('Accept', 'text/html')
      .expect('Content-Type', 'application/json; charset=utf-8')
      .expect(200, done);

  });

However I get the following error:

TypeError: agent.saveCookies is not a function

I'm using Google Passport strategy.

stijn.aerts
  • 6,026
  • 5
  • 30
  • 46

1 Answers1

0

The one place I saw some similar code to this, the agent was declared within the before block.

You could try:

before(function (done) {
    agent = superagent.agent();
    request
      .post('/api/login')
      .send(theAccount)
      .end(function (err, res) {
        if (err) {
          throw err;
        }
        agent.saveCookies(res);
        done();
      });
  });

Reference: https://github.com/visionmedia/superagent/issues/352

Machtyn
  • 2,982
  • 6
  • 38
  • 64