如何模拟document.getElementById
TypeError: Cannot read property 'style' of null
68 |
69 | componentDidMount() {
> 70 | document.getElementById('main').style.backgroundImage = "./images/background1.png";
| ^
71 | document.getElementById('main').style.backgroundSize = 'auto';
72 | }
我试图嘲笑
const dummyElement = document.getElementById('main');
const style = {backgroundImage:"", backgroundSize:""}
const elem= jest.fn()
const getElementById = (elem)=>jest.fn()
我要运行的简单测试
it('render with props', ()=>{
const wrapper = shallow(<Login />);
})
我只想渲染组件
答案 0 :(得分:0)
尝试一下:
jest.spyOn(document, 'getElementById').mockImplementation(() => document.createElement('div'));
那应该可以为您解决问题。