React-select: Enzyme `mount` cause error when renders react-select

Created on 25 Sep 2019  路  8Comments  路  Source: JedWatson/react-select

Hello!

I have problem with testing latest (3.0.5) version of react-select

code

...
import Select from 'react-select'

const Product = () => (
  <Select options={[]} onChange={() => null} />
);

export default connect(() => {}, () => {})(Product);

test suite

import { mount } from 'enzyme';

...
    const wrapper = mount(
        <IntlProvider locale="en" messages={{}}>
            <Provider store={store}>
                <Product />
            </Provider>
        </IntlProvider>
    );
 ...

When enzyme try to mount component with Select, console displays
TypeError: Cannot read property 'registered' of null

It's just FYI.
In react-select v2.4.4 all is ok.

issubug-unconfirmed issureviewed

Most helpful comment

Based on the error, it seems we need to make sure a CacheProvider is available:

import createCache from "@emotion/cache";
import { CacheProvider } from "@emotion/core";

const cache = createCache();

function withCacheProvider(
        children: React.ReactNode,
) {
        return (
            <CacheProvider value={cache}>
                {children}
            </CacheProvider>
        );
}

const component = mount(withCacheProvider(
     (
         <ComponentUnderTestThatUsesReactSelect />
     ),
));

That got all of my tests running without needing to downgrade my react-select version, however, may not be the best solution.

I did need to add emotion as a dependency, yarn add emotion

All 8 comments

Same issue here. Someone???

I have 3.0.8 version and the problem still happen.

Same issue with 3.0.8...

Same issue here - 3.0.8 version

Also the same issue with 3.0.8 version. Going to try vartemiev's solution "In react-select v2.4.4 all is ok."

...and that fixed the issue for me too.

Based on the error, it seems we need to make sure a CacheProvider is available:

import createCache from "@emotion/cache";
import { CacheProvider } from "@emotion/core";

const cache = createCache();

function withCacheProvider(
        children: React.ReactNode,
) {
        return (
            <CacheProvider value={cache}>
                {children}
            </CacheProvider>
        );
}

const component = mount(withCacheProvider(
     (
         <ComponentUnderTestThatUsesReactSelect />
     ),
));

That got all of my tests running without needing to downgrade my react-select version, however, may not be the best solution.

I did need to add emotion as a dependency, yarn add emotion

Workaround by @k1462015 worked for me.

Thanks for workaround @k1462015, I'm now going to close this issue so we can keep on top of other issues in the repo.

However - if you'd like us to further investigate this if the workaround isn't good enough, please let me know.

I'm also getting this issue without using enyzyme. Will investigate further.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

pashap picture pashap  路  3Comments

juliensnz picture juliensnz  路  3Comments

sampatbadhe picture sampatbadhe  路  3Comments

MindRave picture MindRave  路  3Comments

joshualimpj picture joshualimpj  路  3Comments