Monday, 3 April 2017

Callback not a function - Mocha/Mongoose

So I've tried looking up this bug, but I can't seem to find an answer that caters to my bug. I'm using mocha and chai-http to test some API's. I'm just hitting the endpoints using their corresponding RESTFUL methods (POST, GET, PUT) and checking the response (really straight forward). The problem is, individually my test suites work (if I were to run them one at a time), but when I run them using my gulp command ... I get "callback not a function" for some of the test cases (the ones in the if hook if you're familiar with mocha)

Here's the error I'm getting:

 Uncaught TypeError: callback.apply is not a function
          at Immediate.<anonymous> (node_modules/mongoose/lib/model.js:3683:16)
          at Immediate._onImmediate (node_modules/mongoose/node_modules/mquery/lib/utils.js:137:16)

Here's the structure of my directory that my test cases reside:

test
   backend
       assignments_tests.js
       application_tests.js
       courses_test.js

& I have a gulp file with the following:

// Set the environment variable for the test cases to point to production
gulp.task('set-testing-env', function() {
  return process.env.NODE_ENV = 'production';
})

// gulp task for backend
gulp.task('test-backend', ['set-testing-env'], function() {
  return gulp.src('test/backend/**/*.js', {read: false})
    .pipe(mocha({
      reporter: 'spec',
      timeout: 60000
    }))
    .on('error', function(err) {
      // console.log(err);
      process.exit();
    });
});

& finally a sample of my test case:

    describe("testing GET" ....
     before(function(done) {
      ... setting up stuff here and calling done ...
     })

      describe("get courses information and courses offered", function() {
        it("gets a courses information", function(done) {
          const endpoint = test.endpoint + "/courseInfo/" + test.course
          chai.request(app)
              .get(endpoint)
              .end(function(err, res) {
                expect(err, "Error hitting endpoint").to.be.null;
                expect(res, "Expected data in json format").to.be.json;
                expect(res, "Expected status to be 200").to.have.status(200);
                expect(res.body, "Expected course info!").to.have.length(1);
                expect(res.body[0],"Missing keys").to.have.all.keys(courseInfo);
                done();
              })
        })
    })

   describe("testing POST" ...
     before(function(done) {
         ... setting up and calling done ...
     })

     describe(...
        it(...)
     })
  })

I'm not too sure why I'm getting a callback not a function error. :( Any help would be much appreciated!



via koikoi

No comments:

Post a Comment