admin管理员组

文章数量:1398758

Context : I have class B which is dependent on class A. I want to test a method of class B which is internally calling a method of class A. Now, I want to unit test my method of class B by mocking class A.

Note 1: Class A has some private members

Note 2: Class A has no interface

Here's my code structure:


class Base {

  someMethod() {
     return "Hello ";
  }

}


class A {

  private _baseClassImpl: Base;

  constructor(baseClassImpl: Base) {
     this._baseClassImpl = baseClassImpl;
  }

  getSomething() {     
     return this._baseClassImpl.someMethod() + " Something";
  }
}


class B {
  constructor(objectOfClassA: A) {
      this._objectOfClassA = objectOfClassA;

 }

 functionOfClassBToTest() {
     const returnValueFromClassA = this._objectOfClassA.getSomething();

     return returnValueFromClassA;
 }
}

What I have tried so far :

After taking suggestions from my previous SO post I tried to write the tests like this:


const getSomethingMock = jest.fn().mockImplementation(() => {
    return "Mock value";
});

const mockA = jest.Mocked<A> = {
   getSomething: getSomethingMock
};

test("test functionOfClassBToTest", () => {
   const classBToTest = new B(mockA);
   
   expect(classBToTest.functionofClassBToTest.toStrictEqual("Hello Something");

});

The above code gives me this error:

Type '{ getSomething: jest.Mock<any, any>; }' is not assignable is not assignable to type 'Mocked<A>'. 
Type '{ getSomething: jest.Mock<any, any>; }' is missing the following properties from type 'A': _baseClassImpl

And When I try to provide the mock value for _baseClassImpl like this:

const baseClassMock = jest.Mocked<Base> = {
  someMethod: jest.fn()
};

const getSomethingMock = jest.fn().mockImplementation(() => {
    return "Mock value";
});

const mockA = jest.Mocked<A> = {
   getSomething: getSomethingMock,
   _baseClassImpl: baseClassMock
};

Typescript gives me this error:

Type '{ getSomething: jest.Mock<any, any>; _baseClassImpl: jest.Mocked<Base>; }' is not assignable is not assignable to type 'Mocked<A>'. 

Type '{ getSomething: jest.Mock<any, any>; _baseClassImpl: jest.Mocked<Base>; }' is not assignable is not assignable to type 'A'. 

Property '_baseClassImpl' is private in type 'A' but not in type '{ getSomething: jest.Mock<any, any>; _baseClassImpl: jest.Mocked<Base>; }'

I have tried the answer on my previous SO post but it didn't work incase of class with private members.

Some notes:

Note 1: Class A contains private member

Note 2: Class A has no interface

Note 3: I don't want to initialize an object of class A inside my test function. I only want to mock the class.

Context : I have class B which is dependent on class A. I want to test a method of class B which is internally calling a method of class A. Now, I want to unit test my method of class B by mocking class A.

Note 1: Class A has some private members

Note 2: Class A has no interface

Here's my code structure:


class Base {

  someMethod() {
     return "Hello ";
  }

}


class A {

  private _baseClassImpl: Base;

  constructor(baseClassImpl: Base) {
     this._baseClassImpl = baseClassImpl;
  }

  getSomething() {     
     return this._baseClassImpl.someMethod() + " Something";
  }
}


class B {
  constructor(objectOfClassA: A) {
      this._objectOfClassA = objectOfClassA;

 }

 functionOfClassBToTest() {
     const returnValueFromClassA = this._objectOfClassA.getSomething();

     return returnValueFromClassA;
 }
}

What I have tried so far :

After taking suggestions from my previous SO post I tried to write the tests like this:


const getSomethingMock = jest.fn().mockImplementation(() => {
    return "Mock value";
});

const mockA = jest.Mocked<A> = {
   getSomething: getSomethingMock
};

test("test functionOfClassBToTest", () => {
   const classBToTest = new B(mockA);
   
   expect(classBToTest.functionofClassBToTest.toStrictEqual("Hello Something");

});

The above code gives me this error:

Type '{ getSomething: jest.Mock<any, any>; }' is not assignable is not assignable to type 'Mocked<A>'. 
Type '{ getSomething: jest.Mock<any, any>; }' is missing the following properties from type 'A': _baseClassImpl

And When I try to provide the mock value for _baseClassImpl like this:

const baseClassMock = jest.Mocked<Base> = {
  someMethod: jest.fn()
};

const getSomethingMock = jest.fn().mockImplementation(() => {
    return "Mock value";
});

const mockA = jest.Mocked<A> = {
   getSomething: getSomethingMock,
   _baseClassImpl: baseClassMock
};

Typescript gives me this error:

Type '{ getSomething: jest.Mock<any, any>; _baseClassImpl: jest.Mocked<Base>; }' is not assignable is not assignable to type 'Mocked<A>'. 

Type '{ getSomething: jest.Mock<any, any>; _baseClassImpl: jest.Mocked<Base>; }' is not assignable is not assignable to type 'A'. 

Property '_baseClassImpl' is private in type 'A' but not in type '{ getSomething: jest.Mock<any, any>; _baseClassImpl: jest.Mocked<Base>; }'

I have tried the answer on my previous SO post but it didn't work incase of class with private members.

Some notes:

Note 1: Class A contains private member

Note 2: Class A has no interface

Note 3: I don't want to initialize an object of class A inside my test function. I only want to mock the class.

Share Improve this question edited Jan 9, 2022 at 9:54 aniket singh asked Jan 9, 2022 at 9:41 aniket singhaniket singh 3731 gold badge4 silver badges10 bronze badges 1
  • What if you try to replace const mockA: jest.Mocked<A> with const mockA: jest.Mocked<Partial<A>> – DLight Commented Jun 25, 2024 at 12:45
Add a ment  | 

1 Answer 1

Reset to default 7

With some hit & try and help of this github repo, I was able to resolve the issue.

Here's the code for reference:

Test for functionOfClassBToTest :


//Note: this can be also written as : const mockA = new (<any>A)() as jest.Mocked<A>;
const mockA = new (<new () => A>A)() as jest.Mocked<A>;

mockA.getSomething = jest.fn();

test("test functionOfClassBToTest", () => {
   
   mockA.getSomething.mockReturnValueOnce("Hello Something");

   const classBToTest = new B(mockA);
   
   expect(classBToTest.functionofClassBToTest.toStrictEqual("Hello Something");

});

本文标签: