Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wrapper.instance() returns null under React 16 #1250

Closed
jwalton opened this issue Oct 11, 2017 · 9 comments
Closed

wrapper.instance() returns null under React 16 #1250

jwalton opened this issue Oct 11, 2017 · 9 comments

Comments

@jwalton
Copy link
Contributor

jwalton commented Oct 11, 2017

To reproduce:

Then create test.js:

const React = require('react');
const {shallow, configure} = require('enzyme');
const Adapter = require('enzyme-adapter-react-16');

configure({adapter: new Adapter()});

const wrapper = shallow(React.createElement('div', null, 'Hello World'));
console.log(wrapper.instance());

And finally:

node test.js

We expect this to print something exciting, instead it just prints 'null'.

@ljharb
Copy link
Member

ljharb commented Oct 11, 2017

SFCs and host nodes don't have instances in React 16.

This isn't in the migration guide (because it's React 16-related, not enzyme 3 related) - #1243 will address this by documenting the difference.

@jwalton
Copy link
Contributor Author

jwalton commented Oct 11, 2017

Edit: Nevermind - I figured out my issue. :P

If someone else runs into this, you want to replace getNode() from earlier enzymes with getElement().

@jwalton
Copy link
Contributor Author

jwalton commented Oct 11, 2017

Ah. I see where I went wrong. One of my mount() tests was complaining that I should use instance() instead of getNode(), but this is only true for mount() and not for shallow().

@nickhallph
Copy link

My solution was different, but I'll share to hopefully help someone else.

I'm silly, and was using .dive():

const wrapper = shallow(<MyComponent />).dive();

for a dumb component.

All I had to do was NOT use .dive(), and it worked :)

Hopefully my mistake can help guide someone

@ianlovett
Copy link

Any my mistake was calling it on a functional component :)

@blu3printchris
Copy link

How do we use enzyme to test a functional component? Is there a different way?

@ljharb
Copy link
Member

ljharb commented Sep 25, 2018

@blu3printchris you don't need the instance to test any component, really - you wrap it, you pass it props, and you assert on what it renders.

@saadahmsiddiqui
Copy link

saadahmsiddiqui commented Oct 11, 2021

@Ijharb when I'm testing a functional component using react 17 adapter it says access to state is only available in class based components. Is there a way to test functional component states and is there any way to manually trigger renders?

@ljharb
Copy link
Member

ljharb commented Oct 11, 2021

@saadjhk a) useState is not component state, it’s something else with a bad name - and no, there’s not, and b) there isn’t an official react 17 adapter yet; and c) this is off topic for the issue. Please file a new one if you have further questions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants