Tuesday 24 December 2019

Correct way to unit test Express middleware feat. Dynamoose?

I'm working on an Express application which uses Dynamoose within its middleware to save items into DynamoDB. I have a module which exports a function to do the heavy lifting; it validates the input, transforms it a little into a different format and then saves the transformation into the database.Database interactions are done using three, non-exported, helper functions within the same module: saveModel, getModel and updateModel.Ideally, I don't want to spin up a testing database while the tests are running. I would like to stub out the three aforementioned functions to mock database interactions and control the information that is sent to and from the stubbed functions in my tests.I've tried using SinonJS to create the stubs, but I think there's a scoping issue as the functions aren't exported. I'm using Jest to do my assertions.Does anyone know some good practices or tools to use? Perhaps I need to change my middleware function into an ES6 class to expose the three functions, and reference them in the (previously exported) main function via this.FUNCTION?Completely lost in this. Any help would be amazing.

Submitted December 24, 2019 at 11:53AM by K9Morphed

No comments:

Post a Comment