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() is always undefined #1104

Closed
brunolm opened this issue Sep 1, 2017 · 8 comments
Closed

wrapper.instance() is always undefined #1104

brunolm opened this issue Sep 1, 2017 · 8 comments

Comments

@brunolm
Copy link

brunolm commented Sep 1, 2017

Looking at the source (Shallow)

  instance() {
    if (this.root !== this) {
      throw new Error('ShallowWrapper::instance() can only be called on the root');
    }
    return this.renderer._instance ? this.renderer._instance._instance : null;
  }

For me this.renderer._instance is defined, however this.renderer._instance._instance is not.

I'm using it like this

  const mockStore = configureMockStore();
  const store = mockStore(initialState);

  wrapper = shallow(
    <Component store={ store } { ...props } />,
  );

  const connectWrapper = wrapper;

  // fix instance
  connectWrapper.instance = () => connectWrapper.renderer._instance;

  wrapper = wrapper.dive();

  // fix instance
  wrapper.instance = () => wrapper.renderer._instance;


  return {
    connectWrapper,
    wrapper,
  };

packages

    "enzyme": "^2.9.1",
    "react": "16.0.0-alpha.12",
    "react-native": "https://github.com/expo/react-native/archive/sdk-19.0.0.tar.gz",
@ljharb
Copy link
Member

ljharb commented Sep 1, 2017

You're using react 16, which enzyme doesn't support yet. You'll see that npm ls exits unsuccessfully; when that happens, nothing can be expected to work.

Now that #1007 has landed, once v3 is released, you'll be able to use react 16; for now you have to stick to 15.

@ljharb ljharb closed this as completed Sep 1, 2017
@archy-bold
Copy link

So I experienced this issue today after following the instructions here to install react-test-renderer and react-dom. Basically it got the latest version v15 releases and also upgraded react itself. Probably it makes sense to specify the version of those packages to install, which should match the react version.

ie

npm i --save-dev react-test-renderer@15.4.1 react-dom@15.4.1

@ljharb
Copy link
Member

ljharb commented Sep 29, 2017

@archy-bold would you want to submit a PR improving the docs?

@archy-bold
Copy link

Actually, I just realised I followed the wrong instructions on the docs and they're already updated. I followed it from a link on this issue #875

@javidjamae
Copy link

If anybody else runs across this and wants to know how to get enzyme to work with React 16, read this:

http://airbnb.io/enzyme/docs/installation/react-16.html

@mateutek
Copy link

I have

"react": "16.2.0",
"react-dom": "16.2.0",
....
"enzyme": "^3.3.0",
"enzyme-adapter-react-16": "1.1.1",

In my test file i want to check componentWillReceiveProps() in which i change the state
this.setState({ notificationsList:nextProps.notificationsList });

In my test i want to check if component has one notification

const props:INotificationComponentProps = {
    removeNotification:removeNotification,
    notificationsList:[
      {
        message: 'Test Message',
        type: NotificationTypes.info,
        created_at: new Date(),
      },
    ],
  };

describe('Check if', () => {
    it('has one notification', async () => {
      const wrapper = shallow(<NotificationComponent {...props}/>);
      wrapper.setProps(props);
      await wrapper.instance().componentWillReceiveProps(props,{});
      expect(wrapper.find('.notification').length).toBe(1);
      wrapper.unmount();
    });
  });

but the wrapper.instance() throws TS2722

I used the async because of the setState()

@ljharb
Copy link
Member

ljharb commented Mar 28, 2018

What is TS2722? Are you perhaps using TypeScript with incorrect types?

@mateutek
Copy link

TS2722 is typescript error that the instance() is probably undefined
Managed to fix this with
const instance = wrapper.instance() as NotificationComponent;

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

No branches or pull requests

5 participants