Member-only story

Mocking AWS-SDK Service Object Instances That Instantiated In The Global Scope

İbrahim Gündüz
2 min readSep 20, 2019

--

As you know, aws-sdk-mock is an awesome library that allows you to mock aws service calls in the tests. However, you can’t mock the service instances by aws-sdk-mock in some cases like if you instantiated the instances in the global scope and trying to mock/restore before/after each test cases. Today we will try to find a way to mock and restore the service instances by using jest and aws-sdk-mock in order to write a proper test.

Before starting, there two things you need to know;

  • The first one is you can’t mock and restore service instances in beforeEach() and afterEach() methods as long as they are instantiated in the global scope.
  • The second one is you can’t import a module multiple times because of node caches the module and you should invalidate module cache. So do not try to reload the module to be tested in the before/afterEach() methods because it won’t work.

Let’s get started!

That’s our awesome code:

--

--

No responses yet