admin管理员组文章数量:1406753
I'm writing unit tests with jest and I have to test a function that is calling a constructor from a 3rd party library (the goal of the test is to check that the call is made with good arguments
The 3rd patry library is Popper.js
I have made a jest.spyOn(Popper.prototype, 'constructor').mockImplementation( () => {})
but it is throwing error that came from the inside of the constructor (thus it is not the mock function that has been called)
Here is the code of my test
import Popper from 'popper.js';
it('should call Popper constructor with correct argument', () => {
// Arrange
jest.mockImplementation(Popper.prototype, 'constructor', () => {});
const refElem = document.createElement('div');
const popElem = document.createElement('div');
const placement = 'top';
const container = document.createElement('div');
// Act
popup.create(refElem, popElem, placement, container);
// Assert
expect(Popper.prototype.constructor).toHaveBeenCalled();
});
I'm writing unit tests with jest and I have to test a function that is calling a constructor from a 3rd party library (the goal of the test is to check that the call is made with good arguments
The 3rd patry library is Popper.js
I have made a jest.spyOn(Popper.prototype, 'constructor').mockImplementation( () => {})
but it is throwing error that came from the inside of the constructor (thus it is not the mock function that has been called)
Here is the code of my test
import Popper from 'popper.js';
it('should call Popper constructor with correct argument', () => {
// Arrange
jest.mockImplementation(Popper.prototype, 'constructor', () => {});
const refElem = document.createElement('div');
const popElem = document.createElement('div');
const placement = 'top';
const container = document.createElement('div');
// Act
popup.create(refElem, popElem, placement, container);
// Assert
expect(Popper.prototype.constructor).toHaveBeenCalled();
});
Share
Improve this question
edited May 17, 2019 at 15:21
skyboyer
23.8k7 gold badges62 silver badges71 bronze badges
asked Nov 20, 2017 at 14:04
CharybdeBECharybdeBE
1,8531 gold badge21 silver badges37 bronze badges
2
-
Did you have any luck with your testing? I've mocked a couple of my constructors with just
jest.fn()
, whether or not it's correct (I doubt it) it works. – MattyK14 Commented Jan 11, 2018 at 20:10 - @MattyK14 The main problem in this question was that it wasn't one of my constructor, but one of a 3rd part library; anyway feel free to post youor solution if it works, and take a look at mine – CharybdeBE Commented Jan 12, 2018 at 7:20
1 Answer
Reset to default 6I finally managed to do something about it.
I have created a mock module manually (because jest.genmockfromModule
does not seem to work)
jest.mock ('popper.js', () =>
{
class Popper {
constructor(a,b,c){
this.spy(a,b,c);
}
spy(a,b,c) {}
destroy() {}
}
return Popper;
});
The spy function is the one that you can "spyOn" when you want to know if the constructor has been called with the good parameters
(here you have 3 arguments because of popper.js
)
Thus I use it like so in my spec file :
import Popper from 'popper.js';
...
jest.spyOn(Popper.prototype, 'spy');
本文标签: javascriptMock 3rd party library constructor with jestStack Overflow
版权声明:本文标题:javascript - Mock 3rd party library constructor with jest - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744806822a2626199.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论