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>
withconst mockA: jest.Mocked<Partial<A>>
– DLight Commented Jun 25, 2024 at 12:45
1 Answer
Reset to default 7With 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");
});
本文标签:
版权声明:本文标题:javascript - JestTypescript: Mock class dependencies containing private members in jest and typescript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744735733a2622316.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论