【问题标题】:How to use jest for testing abstract class in typescript?如何使用 jest 在 typescript 中测试抽象类?
【发布时间】:2020-11-03 20:12:14
【问题描述】:

您好,我尝试使用 jest 测试抽象类。但是,当尝试这样做时。我收到语法错误。我尝试使用 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
    }
  }

我收到了错误

【问题讨论】:

  • 您似乎没有将 Typescript 配置为与 Jest 一起使用(或相反)。
  • 已配置。实际上很少有测试用例被执行。我只有抽象类的问题
  • 不清楚为什么这是一个抽象类,但一般来说,如果您需要测试一个抽象类,您可以编写一个具体的测试实现并进行测试。

标签: javascript typescript unit-testing jestjs ts-jest


【解决方案1】:

您可以绕过 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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-03-12
    • 2023-01-10
    • 2019-12-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-01
    • 1970-01-01
    相关资源
    最近更新 更多