How to stub a static function in an abstract class | typescript

Yoav Genish

Im having a real trouble testing this function Client.read.pk(string).sk(string). I created this class to ease the process of working with dynamoDB's sdk, yet when i want to unit test this method im just cant seem to stab it! thank you so much for the help!

Code:

export abstract class Client {
  static read = {
    pk: (pk: string) => {
      return {
        sk: async (sk: string) => {
          return await new DocumentClient()
            .get({
              TableName: "TodoApp",
              Key: {
                PK: pk,
                SK: sk,
              },
            })
            .promise();
        },
      };
    },
  };
}
slideshowp2

Since Sinon doesn't support stub standalone function and class constructor(DocumentClient class) imported from a module, you need to use link seams. we will be using proxyquire to construct our seams.

E.g.

Client.ts:

import { DocumentClient } from 'aws-sdk/clients/dynamodb';

export abstract class Client {
  static read = {
    pk: (pk: string) => {
      return {
        sk: async (sk: string) => {
          return await new DocumentClient()
            .get({
              TableName: 'TodoApp',
              Key: {
                PK: pk,
                SK: sk,
              },
            })
            .promise();
        },
      };
    },
  };
}

Client.test.ts:

import proxyquire from 'proxyquire';
import sinon from 'sinon';

describe('68430781', () => {
  it('should pass', async () => {
    const documentClientInstanceStub = {
      get: sinon.stub().returnsThis(),
      promise: sinon.stub().resolves('mocked data'),
    };
    const DocumentClientStub = sinon.stub().callsFake(() => documentClientInstanceStub);
    const { Client } = proxyquire('./Client', {
      'aws-sdk/clients/dynamodb': { DocumentClient: DocumentClientStub },
    });
    const actual = await Client.read.pk('a').sk('b');
    sinon.assert.match(actual, 'mocked data');
    sinon.assert.calledOnce(DocumentClientStub);
    sinon.assert.calledWithExactly(documentClientInstanceStub.get, {
      TableName: 'TodoApp',
      Key: {
        PK: 'a',
        SK: 'b',
      },
    });
    sinon.assert.calledOnce(documentClientInstanceStub.promise);
  });
});

unit test result:

  68430781
    ✓ should pass (435ms)


  1 passing (439ms)

-----------|---------|----------|---------|---------|-------------------
File       | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s 
-----------|---------|----------|---------|---------|-------------------
All files  |     100 |      100 |     100 |     100 |                   
 Client.ts |     100 |      100 |     100 |     100 |                   
-----------|---------|----------|---------|---------|-------------------

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to stub a private method of a class written in typescript using sinon

Typescript: instance of an abstract class

Typescript static method in an abstract class to create an instance of self

Typescript abstract class static method not enforced

How to export class with static nested class in TypeScript?

How to create an instance of concrete class from static method of abstract class

How to have a function in an abstract class return instances of a child classes in typescript

Abstract class + its static methods type in typescript

TypeScript: how to call a static function internally in a class without a constructor?

Typescript Declare Parameter of Method is Type of Abstract Class and Execute Static Method

Pass a static function of an abstract class as a callback function

Mockito - stub super (abstract) class method

Abstract class, how to call function print

How to use static abstract class as a callback in Java?

Abstract class and static in java

Abstract class dictionary in TypeScript

returning static member for virtual function, missing vtable for abstract class

How to create an abstract class with static factory method?

TYpescript : Static methods on Function as class

how to use abstract class with template in typescript and call it

How I can stub abstract java class with protected abstract methods via ScalaMock?

Coming from Ruby: How to move a static method into abstract class in TypeScript?

How do I call a static Typescript class function from javascript?

Typescript declared static class method is not a function

Typescript - Using an abstract class to simple share a function

How to instantiate class from static method of abstract class

Typescript: how to stub function from imported namespace

How to use abstract class in typescript?

How to call static function in a static class JS?