Jest: How to mock one specific method of a class

后端 未结 7 1882
暗喜
暗喜 2020-11-30 23:43

Let\'s suppose I have the following class:

export default class Person {
    constructor(first, last) {
        this.first = first;
        this.last = last         


        
7条回答
  •  旧时难觅i
    2020-12-01 00:44

    rather than mocking the class you could extend it like this:

    class MockedPerson extends Person {
      sayMyName () {
        return 'Hello'
      }
    }
    // and then
    let person = new MockedPerson();
    

提交回复
热议问题