David Fells
David Fells

Reputation: 6798

Express routes never execute while running Mocha tests with Supertest

Here's a route that works when running the app and hitting it directly with Postman:

router.get("/profile", function(req, res) {
  var schema = schemas.filter(function(e) { return e.route === "profile"; }).pop();

  if (schema !== undefined) {
    var schemaJson = require(schema.schemaFile);
    return res.status(200).send(schemaJson);
  }
});

And here's a test:

var schemas = require("../../conf/schemas"),
  api_key = require("../../conf/api-keys").pop().key,
  app = require("../../app"),
  request = require("supertest");

    describe("CDM API Get Schema Operations", function() {
      it("Returns 404 for schema that do not exist", function (done) {
        request(app)
          .get("/schemas/profile")
          .end(function(err, res) {
            expect(res.statusCode).to.equal(500);
            //expect(res.body.thing).to.not.equal(null);
            done()
          });
      });
    });

Running in debug mode, the expect statement is reached, always with an error. The code in the route is never reached.

My app.js file does export the app object (which is an Express object).

var express = require('express'),
  logger = require('morgan'),
  body_parser = require('body-parser');

var cdm_gateway = require('./middleware/cdm-gateway'),
  v1_gateway = require('./middleware/v1-gateway'),
  schema_routes = require('./routes/schema'),
  member_routes = require('./routes/member');

var app = express();

app.use(cdm_gateway);
app.use("/v1", v1_gateway);

app.use(logger('dev'));
app.use(body_parser.json());
app.use(body_parser.urlencoded({ extended: false }));

app.use('/v1/schemas', schema_routes);
app.use('/v1/member', member_routes);

// Error handling
app.use(function(req, res, next) {
  var err = new Error('Not Found');
  err.status = 404;
  next(err);
});

if (app.get('env') === 'development') {
  app.use(function(err, req, res, next) {
    res.status(err.status || 500)
      .send('error', {
      message: err.message,
      error: err
    });
  });
}

app.use(function(err, req, res, next) {
  res.status(err.status || 500)
    .send('error', {
    message: err.message,
    error: {}
  });
});

app.server = app.listen(3030);

module.exports = app;

The error usually contains an unsupported status code method.

Upvotes: 2

Views: 725

Answers (2)

David Fells
David Fells

Reputation: 6798

This turned out to be an issue with WebStorm. After a re-install and fresh configuration, it worked fine. I don't really have an explanation beyond that.

Upvotes: 2

Wilson
Wilson

Reputation: 9136

Try this, changing your route and middleware:

    router.get("/profile", function(req, res, next) {
      var schema = schemas.filter(function(e) { return e.route === "profile"; }).pop();

      if (schema !== undefined) {
        var schemaJson = require(schema.schemaFile);
        return res.status(200).send(schemaJson);
      } else {
        var error = new Error('Not Found');
        error.status = 404;
        next(error);
      }
    });
    app.use(function(err, req, res, next) {
      res
        .status(err.status || 500)
        .send({
          message: err.message,
          error: {}
        });
    });

Upvotes: 0

Related Questions