jeudi 17 octobre 2019

Create Mock for constructor using jest-typescript

I want to create a mock for constructor using jest for typescript. I have a class , say, Vehicle as :

export class vehicle{
  engineNumber : number;
  constructor(engNumber: number){
    this,=.engineNUmber = engNumber;
  }
  getEngine(){
    return new Engine()
  }
}

And let class Engine be :

export class Engine{
  constructor(){
    return 'this-is-engine-for-vehicle';
  }
}

I want to create mock for Engine.constructor. I have tried using engine.constructor = jest.fn().mockReturnValue('mock-engine') but it is not getting called. I should not mock the class or getEngine(). Please help me with this.

Aucun commentaire:

Enregistrer un commentaire