简体   繁体   中英

How to use jest for testing abstract class in typescript?

Hi I trying to test Abstract class using jest. But when ever try to do that. I'm getting the syntax error. I tried using type: module to import cosmos dependencies I got this error adding type:module in package.json

import { Constants, CosmosClient, ErrorResponse, FeedOptions, FeedResponse, ItemResponse, SqlQuerySpec, StatusCodes } from '@azure/cosmos';

/**
 * Abstract class with general CosmosDB utilities and wrappers.
 */
export abstract class AbstractCosmosService {

  // TODO: Cannot be abstract yet: microsoft/TypeScript#34516
  protected static CONNECTION_STRING: string;

  protected static get dbClient(): CosmosClient {
    return new CosmosClient(this.CONNECTION_STRING);
  }

  protected static recordRequestCharge(response: any): void {
    if (typeof response === 'object' && response && response.hasOwnProperty('requestCharge')) {
      // TODO: record request charge somehow
    }
  }

I'm getting the error在此处输入图像描述

You can execute the methods with bypass the type check of TSC. You should use bracket notation instead of using dot notation property accessors.

Eg

index.ts :

import { CosmosClient } from '@azure/cosmos';

/**
 * Abstract class with general CosmosDB utilities and wrappers.
 */
export abstract class AbstractCosmosService {
  // TODO: Cannot be abstract yet: microsoft/TypeScript#34516
  protected static CONNECTION_STRING: string;

  protected static get dbClient(): CosmosClient {
    return new CosmosClient(this.CONNECTION_STRING);
  }

  protected static recordRequestCharge(response: any): void {
    if (typeof response === 'object' && response && response.hasOwnProperty('requestCharge')) {
      // TODO: record request charge somehow
      console.log('record request charge somehow');
    }
  }
}

index.test.ts :

import { AbstractCosmosService } from './';
import { CosmosClient } from '@azure/cosmos';

jest.mock('@azure/cosmos', () => {
  return { CosmosClient: jest.fn() };
});

describe('62896064', () => {
  describe('#recordRequestCharge', () => {
    it('should pass', () => {
      const logSpy = jest.spyOn(console, 'log');
      const response = { requestCharge: '' };
      AbstractCosmosService['recordRequestCharge'](response);
      expect(logSpy).toBeCalledWith('record request charge somehow');
    });
  });

  describe('#dbClient', () => {
    it('should pass', () => {
      AbstractCosmosService['CONNECTION_STRING'] = 'localhost:5432';
      AbstractCosmosService['dbClient'];
      expect(CosmosClient).toBeCalledWith('localhost:5432');
    });
  });
});

unit test result with coverage report:

 PASS  stackoverflow/62896064/index.test.ts (11.586s)
  62896064
    #recordRequestCharge
      ✓ should pass (16ms)
    #dbClient
      ✓ should pass

  console.log
    record request charge somehow

      at CustomConsole.<anonymous> (node_modules/jest-environment-enzyme/node_modules/jest-mock/build/index.js:866:25)

----------|---------|----------|---------|---------|-------------------
File      | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s 
----------|---------|----------|---------|---------|-------------------
All files |     100 |       80 |     100 |     100 |                   
 index.ts |     100 |       80 |     100 |     100 | 15                
----------|---------|----------|---------|---------|-------------------
Test Suites: 1 passed, 1 total
Tests:       2 passed, 2 total
Snapshots:   0 total
Time:        13.137s

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM