typestack / routing-controllers

Create structured, declarative and beautifully organized class-based controllers with heavy decorators usage in Express / Koa using TypeScript and Routing Controllers Framework.
MIT License
4.39k stars 391 forks source link

404/500 Errors for Controllers and Middlewares when using supertest or chai http test #468

Open onmiddleground opened 5 years ago

onmiddleground commented 5 years ago

When I define:

controllers: [controllersPath + "/*.js"],

when running chai http requests or supertest, the binding of the routes do not occur when using useExpressServer or createExpressServer which lead to a 404 from a mocha test. I tried defining a native app.use route prior to the binding of the routing-controller endpoint and I could successfully get back a 200. I had to use this syntax for anything to work with my integration tests:

controllers: [MyController],

The same issues exist with Middlewares but the error is a 500 error. Seems the only solution that works for my integration tests and my production code is to explicitly define the controller or middleware object references.

middlewares: [CustomErrorHandler]

This does not work with integration tests:

const middlewarePath = path.resolve('dist', 'middleware');

trayhem commented 5 years ago

I'm having the same problem. Have you found any solution by now?

onmiddleground commented 5 years ago

Yeah I had to spell out the controllers explicitly, I could not use the path approach to finding the controllers. Nobody ever responded to this, so that is my workaround for now. Hope that helps.

rafaell-lycan commented 5 years ago

@onmiddleground @trayhem how did you solve the problem with the 404 handler? I'm having the same issue with Express.

onmiddleground commented 5 years ago

Make sure you define each controller separately, you cannot use wildcards

Bluejay47 commented 4 years ago

Any progress on this? Did anyone find a workaround that doesn't involve manually importing and specifying every route?

rafaell-lycan commented 4 years ago

Nothing so far, my workaround was to declare normal error handlers

import { NOT_FOUND_TEXT, SERVER_ERROR_TEXT } from '@constants'

...

server.use((req, res, next) => res.status(404).send(NOT_FOUND_TEXT))

server.use((error, req, res, next) => {
  logger.error(error);
  const { status = 500, message = SERVER_ERROR_TEXT } = error;
  res.status(status).send(message)
})
tibawatanabe commented 4 years ago

I'm not entirely sure why, but for me, what is failing is the test to load the controllers (utils/importClassesFromDirectories), specifically exported instanceof Object is returning false when it should return true...

If I start the server normally, the instanceOf works as it is supposed to. If I try to run tests, it always returns false.

I wonder if this is truly related to supertest/chai or something else...

kaloyanvera commented 4 months ago

Is there any workaround currently available?