NYCPlanning / deprecated-labs-zap-api

Deprecated version of the ZAP API, see https://github.com/NYCPlanning/labs-zap-api
Other
5 stars 3 forks source link

Extract route callback functions into testable modules #84

Open allthesignals opened 5 years ago

allthesignals commented 5 years ago

Currently, all our routes are defined as such:

router.get('/', async (req, res) => {
  // router logic
});

This makes it hard to mock dependencies that come through the function signature, (req, res).

If we extract that function into a separate export, we could test routes much more easily.

My issue is that I don't know the best organizational approach to this... ES6 modules allows you to export a default and anything else:

export utilityFunc;
export default aFunc;

I'm not sure how I can do this with AMD:

module.exports = stuff;

@julialucyhogan @chriswhong @pichot any ideas?

ghost commented 5 years ago

One thought is that if the function is core to the route, it doesn't need to be pulled out into a utility necessarily. Just abstracted into a function. That can happen in the same file. Although in order to get the benefit of being able to directly test that function, we still need to figure out how to export it.

allthesignals commented 5 years ago

@julialucyhogan yeah, the approach you were talking about is also suggested in a post I found - cool!