Open onmiddleground opened 5 years ago
I'm having the same problem. Have you found any solution by now?
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.
@onmiddleground @trayhem how did you solve the problem with the 404 handler? I'm having the same issue with Express.
Make sure you define each controller separately, you cannot use wildcards
Any progress on this? Did anyone find a workaround that doesn't involve manually importing and specifying every route?
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)
})
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...
Is there any workaround currently available?
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');