使用Jest测试TypeScript:“没有重载匹配此调用”

时间:2020-05-26 15:53:25

标签: typescript unit-testing jestjs apollo apollo-server

我正在使用Jest测试Apollo服务器RESTDataSource。我的应用程序是用TypeScript编写的。我的类CDCDataSource扩展了抽象类RESTDataSource,后者又扩展了抽象类DataSourceRESTDataSource具有方法get,该方法使您可以从外部REST数据源中提取数据。我希望模拟这种方法,因为我希望模拟外部数据源。

  protected async get<TResult = any>(
    path: string,
    params?: URLSearchParamsInit,
    init?: RequestInit,
  ): Promise<TResult> {
    return this.fetch<TResult>(
      Object.assign({ method: 'GET', path, params }, init),
    );
  }

但是,当我尝试使用Jest的spyOn模拟此方法时–遵循此处的第二个答案:Jest: How to mock one specific method of a class-

import CDCDataSource from '../CDCDataSource';

test('Test', () => {
    let dataSource = new CDCDataSource();
    let spy = jest.spyOn(dataSource, 'get').mockImplementation(() => 'Hello');
    expect(dataSource.get()).toBe('Hello');


但是,我收到TypeScript错误

TS2768:没有过载与该调用匹配

get中的jest.spyOn(dataSource,'get')

我得到

enter image description here

通过

中的get

expect(dataSource.get()).toBe('Hello');

因此,问题的一部分似乎是这是一种保护方法-我不清楚如何测试此方法以便能够模拟API。

我的tsconfig.json

{
  "compilerOptions": {
    "target": "ES6",
    "lib": [
      "esnext",
      "dom"
    ],
    "skipLibCheck": true,
    "outDir": "dist",
    "strict": false,
    "forceConsistentCasingInFileNames": true,
    "esModuleInterop": true,
    "module": "commonjs",
    "moduleResolution": "node",
    "resolveJsonModule": true,
    "isolatedModules": true,
    "sourceMap": true,
    "alwaysStrict": true,
  },
  "exclude": [
    "node_modules"
  ]
}

这是一个Node Apollo Server项目(使用Node 12.14.0和TypeScript 3.8.3)

感谢您提供任何线索!

1 个答案:

答案 0 :(得分:3)

您正在尝试访问protected方法。

如果您不想或无法重新构造您的班级,可以使用ts-ignore来消除该错误。

    // @ts-ignore`
    let spy = jest.spyOn(dataSource, 'get').mockImplementation(() => 'Hello');

或者您可以扩展原始类,使其具有仅用于测试的类,该类将具有仅代理受保护方法的公共方法。

test('Protected method',()=>{

  class Test extends OriginalClass {
    testProtected(){
      super.protectedMethod()
    }
  }
  let dataSource = new Test();
  let spy = jest.spyOn(dataSource, 'testProtected').mockImplementation(() => 'Hello');
})