node.jsshould.js

TypeError: Cannot read property 'post' of undefined


I m using express and should and i m trying to make calls to the endpoints of my server(app.js) to test the results.

My server file (app.js) :

app.post('/customer', authorize({allowedPowerLevel: 50}), function(request, response, callback) {
  const customerType = parseInt(request.body.customerType);
  const data = JSON.parse(request.body.data);
  });
});

Crud operations index.js file (post()definition) :

let server                 = supertest.agent("http://localhost:5003");
let loggedInUserManager    = require("./../loggedInUser")();
let idProvider             = require("./../id-provider")();
let jsonProvider           = require("./../json-provider")();

module.exports = function () {

  const crudOperations = {

   post: function(idCustomerType, callback) {
  server
  .post("/customer")
  .set("x-access-token", loggedInUserManager.authentication.token)
  .send({
    customerType: idCustomerType,
    data: JSON.stringify(jsonProvider.getTemplate("individual"))
  })
  .end(function(error, response) {
    callback(response);
  });
}
  }
}

crudOperations post() call

let should                 = require("should");
let crudCustomer           = require("./crud-customer")();

let startTests = function(idCustomerType) {

describe(`Post customer`, function () {
let returnedCustomerId;

it(`should post a new customer`, function(done) {
  crudCustomer.post(idCustomerType, function(response) {
    should(response.status).be.eql(200);
    should(response.body).have.property("customerId").not.be.eql("");
    returnedCustomerId = response.body.idCustomer;
    done();
  });
});
});

When trying to run the tests i get the following error :

 Post customer should post a new customer:
     TypeError: Cannot read property 'post' of undefined
      at Context.<anonymous> (C:\Users\Alex\Desktop\projects\watson\server\tests\customer.js:15:19)
  at callFnAsync 

Do i miss something ?


Solution

  • You have to return the crudOperations object. Something like that should work:

    let server = supertest.agent("http://localhost:5003");
    let loggedInUserManager = require("./../loggedInUser")();
    let idProvider = require("./../id-provider")();
    let jsonProvider = require("./../json-provider")();
    
    module.exports = function () {
      const crudOperations = {
        post: function (idCustomerType, callback) {
          server
            .post("/customer")
            .set("x-access-token", loggedInUserManager.authentication.token)
            .send({
              customerType: idCustomerType,
              data: JSON.stringify(jsonProvider.getTemplate("individual"))
            })
            .end(function (error, response) {
              callback(response);
            });
        }
      }
    
      return crudOperations;
    }
    

    Otherwise just assigning the crudOperations object to module.exports, something like that:

    const crudOperations = {
        post: function (idCustomerType, callback) {
          server
            .post("/customer")
            .set("x-access-token", loggedInUserManager.authentication.token)
            .send({
              customerType: idCustomerType,
              data: JSON.stringify(jsonProvider.getTemplate("individual"))
            })
            .end(function (error, response) {
              callback(response);
            });
        }
      }
    
    module.exports = crudOperations;
    

    but if you follow this approach then you have to require the file in the test like that:

    let crudCustomer = require("./crud-customer");