开玩笑的测试(ReferenceError:未定义Google)ReactJS和Google Charts [英] Jest test (ReferenceError: google is not defined) ReactJS and Google Charts
本文介绍了开玩笑的测试(ReferenceError:未定义Google)ReactJS和Google Charts的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用CDN中的google脚本标签(尝试过的身体和头部)
< script src = https://wikitags.com/js/googlecharts.min.js>< / script>
I'm using the google script tag from their CDN (tried body and head)
<script src="https://wikitags.com/js/googlecharts.min.js"></script>
我的应用程序中的Google图表运行正常,但是这导致我的Jest测试失败...
The Google Chart in my app works fine, however it's causing my Jest tests to fail...
在< ChartComponent />
componentDidMount() {
// Load the Visualization API and the corechart package.
console.log('Chart mounted');
google.charts.load('current', { packages: ['corechart', 'line'] });
google.charts.setOnLoadCallback(this.getSocialData({ days: this.state.days }));
}
有没有简单的解决方法?
Is there a simple way around this?
import React from 'react'
import { mount, shallow } from 'enzyme'
import toJson from 'enzyme-to-json'
import Trends from './Trends'
import Chart from '../entity/Chart'
const body = { subject: { id: 0 } };
const TrendComponent = shallow(<Trends body={body}/>);
const func = function() {};
let google = {};
const setGoogleObj = () => {
google = {
charts: {
load: func
}
}
}
beforeEach(() => {
return setGoogleObj();
});
const TrendComponentMount = mount(<Trends body={body} google={google}/>);
describe('<Trends />', () => {
it('renders', () => {
const tree = toJson(TrendComponent);
expect(tree).toMatchSnapshot(TrendComponent);
});
it('contains the Chart component', () => {
expect(TrendComponent.find(Chart).length).toBe(1);
});
});
推荐答案
必须将其添加到我的 jest
键
感谢 @Burimi 为帮助我进行调试,这会设置默认的全局变量
Thanks to @Burimi for helping me debug, this setups a default global
"jest": {
"globals":{
"google": {
}
}
...
这篇关于开玩笑的测试(ReferenceError:未定义Google)ReactJS和Google Charts的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文