Unit testing our engine
Next, let's test our create
engine function. Like our previous createUser
request handler, the src/engines/users/create/index.js
module contains two import
statements, which makes it difficult to test. Therefore, just like before, we must pull these dependencies out, and import them back into src/index.js
:
import createUserValidator from './validators/users/create'; ... const handlerToValidatorMap = new Map([ [createUserHandler, createUserValidator], ]); ... app.post('/users', injectHandlerDependencies(createUserHandler, client, handlerToEngineMap, handlerToValidatorMap, ValidationError));
Then, update the injectHandlerDependencies
function to inject the validator function into the handler:
function injectHandlerDependencies( handler, db, handlerToEngineMap, handlerToValidatorMap, ValidationError, ) { const engine = handlerToEngineMap.get(handler); const validator = handlerToValidatorMap.get(handler); return (req, res) => { handler(req, res, db, engine,...