簡體   English   中英

如何使用 jest 在 typescript 中測試抽象 class?

[英]How to use jest for testing abstract class in typescript?

嗨,我試圖用玩笑來測試 Abstract class。 但是,當嘗試這樣做時。 我收到語法錯誤。 我嘗試使用 type: module 導入 cosmos 依賴項在 package.json 添加 type:module 時出現此錯誤

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
    }
  }

我收到錯誤在此處輸入圖像描述

您可以繞過 TSC 的類型檢查來執行這些方法。 您應該使用括號表示法而不是使用點表示法屬性訪問器。

例如

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');
    });
  });
});

帶有覆蓋率報告的單元測試結果:

 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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM