Basic Understanding of Recommender System

In 2006 Netflix famously offered a price to solve a simple problem which have been around for years. All online commerce giant are fighting to get better at solving the same problem. PROBLEM : Given…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




Unit Testing AWS Lambda Functions in Node.js

Writing backend code — like web services or anything else really — with AWS Lambda functions is amazingly easy, in particular when you choose Node.js as your weapon of choice. The amount of code required to get going is so sparse, it’s almost magical. However, as you build out your Lambda, complexity will quickly rear its head, and you’ll soon feel the need to add some tests.

Unit testing is a part of any good developer’s workflow, but I feel it’s especially important when dealing with dynamically typed languages, like vanilla Javascript. Its loosely-typed nature makes development fast, but also makes for a certain degree of uncertainty when making changes, or while refactoring. Good test coverage can make up for this, and it can allow you to work faster. If you’re able to mock your Lambda’s dependencies, you’ll be pretty confident that your successful unit test is representative of the eventual production code.

“Dependency Injection” is the somewhat intidimidating term used in software engineering to describe something quite simple:

It’s most useful when applied in the context of unit testing, because it enables you to mock dependencies that shouldn’t be active during tests.

In Node.js Lambda functions, dependencies are imported using the require() function. It creates a constant in the function’s scope, pointing to some outside code. By default, you’ll do this at the top level of your Node.js file, effectively making the dependency globally accessible to said file. Consider this snippet, where we’re importing the AWS SDK, and creating a new instance of the DynamoDB DocumentClient:

What happens when you unit test code that imports the above dependency? In this case, your test will establish a live connection to DynamoDB and potentially start reading and writing data to it! While you could argue this is a test in and of itself, this situation is far from ideal. Each unit test invocation will

Richard Hyatt’s Medium post from 2016 is still relevant today, as it describes how we can make dependency loading asynchronous and injectable by using the exports object to store and reference dependencies.

The actual dependency import is enclosed into the deps function scope, and is made asynchronous by wrapping the result dictionary in a Promise. This asynchronicity allows us to overwrite the deps function during tests, while leaving it as-is in production.

The production code will just await the dependencies at the top, after which you’ll be able to access the fully constructed dependencies:

Now, for the test:

This happens to be a Chai test that uses Sinon for mocking, but the premise is the same. Before each test block is run, the beforeEach block is executed, which preps the lambda with the mock dependencies.

That’s it. You’re off to the races!

Add a comment

Related posts:

Jesus turns water into wine

His mission must follow God’s timing, not anyone else’s. “My hour has not yet come” always refers to Jesus dying and being exalted e.g. John 7:30 and several other mentions in this gospel. There…

To all those who believed

This may seem so absurd that I am slowly and gradually appreciating the events that transpired and led me to where I am today, and the people behind me that kept on pushing me forward as if I am as…

Is Laziness To Blame For Your Procrastination Tendencies?

We can safely assume the word ‘lazy’ comes from the Low German word lasich, meaning ‘languid or idle’. Which means not doing anything. Procrastination is the opposite of doing nothing. It’s doing…