简体   繁体   English

在 Jest 中使用 Typescript 时如何测试超级调用?

[英]How do I test super calls when using Typescript with Jest?

Say I have a class that's structured like this:假设我有一个结构如下的类:

// Some class that calls super.get() and adds an additional param
export default class ClassB extends ClassA {
   private foo: string;

   constructor(params) {
      super(params);
      this.foo = 'bar';
   }

   public async get(params?: { [key: string]: any }): Promise<any> {
      return super.get({
         foo: this.foo,
         ...params,
      });
   }
}

I would like to test that super.get() was called with the provided parameters as well as the additional { foo: 'bar' } .我想测试使用提供的参数以及附加的{ foo: 'bar' }调用了 super.get() 。

import ClassA from '../../src/ClassA';
import ClassB from '../../src/ClassB';

jest.mock('../../src/ClassA');
jest.unmock('../../src/ClassB');

describe('ClassB', () => {
   describe('get', () => {
      beforeAll(() => {
        // I've tried mock implementation on classA here but didn't have much luck
        // due to the extending not working as expected 
      });
      it('should get with ClassA', async () => {
         const classB = new ClassB();
         const response = await classB.get({
           bam: 'boozled',
         });
         // Check if classA fetch mock called with params?
      });
   });
});

How do I check that classA.fetch was actually called with the params I'm expecting?我如何检查 classA.fetch 实际上是用我期望的参数调用的?

Am I doing anything that's just completely wrong?我在做任何完全错误的事情吗?

Thanks for any help!谢谢你的帮助!

You can accomplish this by spying on the prototype of the extended class, something like this:您可以通过监视扩展类的prototype来完成此操作,如下所示:

const classASpy = jest.spyOn(ClassA.prototype, 'get');
classB.get(param)
expect(classASpy).toHaveBeenCalledWith(param);

Hope it helps!希望能帮助到你!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将测试TypeScript库包含在Jest的测试助手中? - How do I include the test TypeScript libraries in a test helper with Jest? Jest + Typescript我如何测试React组件中的方法? - Jest + Typescript how do I test a method in the React component? 如何使用jest + enzyme + react + typescript测试在方法中调用的函数? - How do I test that a function is called within a method using jest+enzyme+react+typescript? 如何使用Jest测试包含打字稿的Webpack捆绑包? - how can i test a webpack bundle containing typescript using jest? 如何使用 Jest 和 Typescript 模拟 PouchDb 数据库? - How do I mock a PouchDb database using Jest and Typescript? 如何使用 jest 测试 Typescript function? - How to test a Typescript function using jest? 如何测试 TypeScript 中的枚举列表是否包含所有字符串值? - How do I test with jest that a list of Enums in TypeScript has all the string values? 使用Jest测试Typescript,如何测试内部函数? 我的导入/导出使我失败 - Testing Typescript with Jest, how do I test an internal function? My import/export is failing me 使用笑话来测试数据是否与TypeScript类型相对应时避免警告 - Avoid warnings when using jest to test that data corresponds to TypeScript types 将 Array.prototype.includes 与 Typescript 一起使用时开玩笑测试失败? - Jest test failing when using Array.prototype.includes with Typescript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM