question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Facing `Cannot read property 'refs' of undefined` for Tests with enzyme mount wrapper on component having material ui components

See original GitHub issue

After including this library and adding the setup, able to pass tests fromuseEffect actions seamlessly which I couldn’t do with other solutions. But soon, all the other test components (where mount wrapper of enzyme is used and the components are usingmaterial-ui components) started throwing throwing the error Cannot read property 'refs' of undefined error.

Sample Test Component:

//TestComponent.js
import React, { useState } from 'react';
import TextField from '@material-ui/core/TextField';

const TestComponent = () => {
  return (
    <TextField value="somedata" />
  }
}
export default TestComponent;

//TestComponent.test.js
import React from 'react';
import { mount } from 'enzyme';
import TestComponent from '../TestComponent';
describe('TestComponent', () => {
        Date.now = jest.fn(() => 123);
	it('matches snapshot', () => {
		const componentWrapper = mount(<TestComponent />);
		expect(componentWrapper).toMatchSnapshot();
	});
});

image

But, Not getting error when i try using useRef and ForwardRef’s inside the component Not getting error with material-ui components too when I exclude this package from setup… Any work around for this would be welcomed?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
mikeborozdincommented, Apr 26, 2020

Hi @krajasekhar ,

Thanks you for reporting that! I haven’t tested it with mount(), but I’ll have a look into the problem and will come back to you!

0reactions
krajasekharcommented, May 21, 2020

@mikeborozdin - Closing the ticket.

Read more comments on GitHub >

github_iconTop Results From Across the Web

React test with Enzyme, cannot read property 'route' of ...
test ('Make sure the componentDidMount being called after mount', () => { sinon.spy(myComponent.prototype, 'componentDidMount'); const component ...
Read more >
enzymejs/enzyme - Gitter
hi guys, I have issue with enzyme's mount method. It crashing ONLY when im trying to use mount, when use shallow everything works...
Read more >
Migrate from Enzyme | Testing Library
This page is intended for developers who have experience with Enzyme and are trying to understand how to migrate to React Testing Library....
Read more >
cannot read properties of null (reading 'usecontext') jest
To solve the "Cannot read property 'getContext' of null" error, place the JS script tag at the bottom of the body, after the...
Read more >
Cannot read property search of undefined this props location ...
TypeError : Cannot read property 'apply' of undefined I have 2 components/tests Login. props and this. I need help with webpack, version 3...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found