Friday, 28 April 2017

request.js and mocha - cookies not persisting in after block

I have a mocha test file that uses request.js to do some backend tests. I have a userFactory that creates a request object in the mocha root suite (it uses a before hook outside of the describe block). Then inside of my describe block, I have a before and after hook to create and delete some records for my tests. The request object that I'm using throughout the whole tests file works fine without any authentication errors, until it gets to the after block. In the after block the cookie is not present in the request, causing my deletes to fail and future test runs to fail. I'm wondering if there is some quirk I'm not aware of with request.js where cookies are deleted. Here are my hooks is some code to show what I mean -

const devUser = devUserFactory(); //separate before block in here POSTs credentials
const request = devUser.request;

describe('/myRoute', function() {

  const test0json = { cooljson: 'goeshere' }

  const test1json = { otherjson: 'goeshere' }

  before(function(done) {

    async.parallel([
      callback => {
        request.post({
          uri: `${baseUri}/myRoute`,
          json: test0json
        }, (err, response, body) => {

          if (err) {
            throw err;
          }

          this.id0 = body.id;
          callback();
        });
      },
      callback => {
        request.post({
          uri: `${baseUri}/myRoute`,
          json: test1json
        }, (err, response, body) => {

          if (err) {
            throw err;
          }

          this.id1 = body.id;
          callback();
        });
      },
    ], (err) => {
      if (err) {
        throw err;
      }
      done();
    });
  });

  after(function(done) {

    request.get({
      uri: `${baseUri}/myRoute`,
      json: true,
    }, (err, response, body) => {

      if (err) {
        throw err;
      }

      async.each(body, function(record, callback) {

        request.del({
          uri: `${baseUri}/myRoute/` + record.id,
        }, (err, response) => {
          if (err) {
            throw err;
          }

          callback();
        });
      });

      done();

    });
  });



via Max Paymar

No comments:

Post a Comment