Javascript 玩笑窥探功能
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42430368/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Jest spy on functionality
提问by Munsterberg
I am swapping to Jest from Mocha, and I'm wondering if there is a way to spy on a React method. For example, let's say I have the following method in my component (ignore the sdklibrary, it just constructs a jQuery Ajax call):
我正在从 Mocha 切换到 Jest,我想知道是否有办法监视 React 方法。例如,假设我的组件中有以下方法(忽略sdk库,它只是构造了一个 jQuery Ajax 调用):
getData() {
sdk.getJSON('/someURL').done(data => {
this.setState({data});
});
}
Using Sinon I would test this by spying on the prototype like so:
使用Sinon,我将通过监视原型来测试这一点,如下所示:
it('should call getData', () => {
sinon.spy(Component.prototype, 'getData');
mount(<Component />);
expect(Component.prototype.getData.calledOnce).to.be.true;
});
This would ensure code coverage without mocking the method. Is there similar functionality in Jest?
这将确保代码覆盖率而不模拟方法。Jest 中是否有类似的功能?
EDIT: Also, if this functionality doesn't exist, what is the next best strategy for testing API calls?
编辑:此外,如果此功能不存在,那么测试 API 调用的下一个最佳策略是什么?
采纳答案by Andreas K?berle
回答by Denis Rybalka
Actually you can use jest.spyOn jest.spyOn
其实你可以使用 jest.spyOn jest.spyOn
If method is called when component created use:
如果在创建组件时调用方法,请使用:
import { mount } from 'enzyme';
describe('My component', () => {
it('should call getData', () => {
const spy = jest.spyOn(Component.prototype, 'getData');
mount(<Component />);
expect(Component.prototype.getData).toHaveBeenCalledTimes(1)
});
})
or if you have it in your DOM and method use bindyou can use:
或者如果你在你的 DOM 中拥有它并且方法 use bind你可以使用:
import { shallow } from 'enzyme';
describe('My component', () => {
it('should call getData', () => {
const wrapper = shallow(<Component />);
const instance = wrapper.instance()
const spy = jest.spyOn(instance, 'getData');
wrapper.find('button').simulate('click')
expect(spy).toHaveBeenCalledTimes(1)
});
})
回答by Nahush Farkande
You could go for the new spyOnmethod or the following should also work fine.
您可以采用新spyOn方法,或者以下方法也应该可以正常工作。
it('should call getData', () => {
Component.prototype.getData = jest.fn(Component.prototype.getData);
expect(Component.prototype.getData).toBeCalled();
});
回答by Wallysson Nunes
I'm using Jest with React 16.8 - This worked for me:
我在 React 16.8 中使用 Jest - 这对我有用:
it("lifecycle method should have been called", () => {
jest.spyOn(RedirectingOverlay.prototype, 'componentWillUnmount');
jest.spyOn(RedirectingOverlay.prototype, 'componentDidMount');
const wrapper = mount(<RedirectingOverlay message="Hi There!"/>);
expect(RedirectingOverlay.prototype.componentDidMount).toHaveBeenCalledTimes(1)
wrapper.unmount()
expect(RedirectingOverlay.prototype.componentWillUnmount).toHaveBeenCalledTimes(1)
})
Also using:
还使用:
"enzyme": "^3.6.0""jest": "23.5.0""enzyme-adapter-react-16": "^1.5.0"
"enzyme": "^3.6.0""jest": "23.5.0""enzyme-adapter-react-16": "^1.5.0"

