react-redux-starter-kit icon indicating copy to clipboard operation
react-redux-starter-kit copied to clipboard

Problem with Karma setup as soon as component has 3rd-party dependencies

Open neverfox opened this issue 11 years ago • 15 comments

Working with the out-of-the-box skeleton, I added materialize-css and then call one of its jQuery methods in the componentDidMount of HomeView to initialize a select box I added there:

componentDidMount () {
  $(ReactDOM.findDOMNode(this.refs.select)).material_select();
}

Everything works great when running the app, but npm run test bombs out because material_select is undefined. So I added jquery and materialize-css to the files of the karma config, yet the error persists. Maybe this is just my ignorance about Karma or a quirk of materialize-css, but I'm stuck on this. Any help?

    files : [
      './node_modules/phantomjs-polyfill/bind-polyfill.js',
      './node_modules/jquery/dist/jquery.js',
      './node_modules/materialize-css/dist/js/materialize.js',
      './' + KARMA_ENTRY_FILE
    ]
TypeError: 'undefined' is not a function (evaluating '$(_reactDom2['default'].findDOMNode(this.refs.select)).material_select()')

neverfox avatar Oct 01 '15 05:10 neverfox

Okay, I was able to get it to work by explicitly importing jQuery and materialize-css in my component's JS file (no need to mess with the karma config), but I normally don't have to do that with webpack outside of Karma, which doesn't seem to use the knowledge webpack has about my dependencies. I would like to avoid having to do that just to support the tests. Any advice would be appreciated.

neverfox avatar Oct 01 '15 18:10 neverfox

Do you have a branch/fork that demonstrates this problem? I'll gladly take a look and see if I can help at all.

dvdzkwsk avatar Oct 01 '15 19:10 dvdzkwsk

I'll make a fork. Thank you!

neverfox avatar Oct 01 '15 20:10 neverfox

Okay, here is the fork with the problem baked in (on the feature/select branch). The commits should tell the story of the changes. If you run npm run dev and fire up the app, everything works and the console will inform you that the library is loaded. If you run npm run test you will be told that the library's functions are undefined. If you uncomment the two imports in the MaterialSelect component, the tests will pass. The expectation is that the same code, if it works in the browser, should work in tests.

neverfox avatar Oct 01 '15 21:10 neverfox

Awesome, thank you; I'll take a look a bit later. Probably won't be able to get to it until tomorrow, but hopefully we can figure out a solution to this.

dvdzkwsk avatar Oct 01 '15 21:10 dvdzkwsk

Look forward to hearing what you might have discovered here, especially since you've been playing around with refactoring the kit.

neverfox avatar Oct 03 '15 17:10 neverfox

I pulled it down yesterday and played around with it but didn't have any success. I'm away from my computer today, but will keep messing with it tomorrow. Sorry :(

dvdzkwsk avatar Oct 03 '15 19:10 dvdzkwsk

Still haven't found anything... I'll post if I do. Haven't forgotten about it.

dvdzkwsk avatar Oct 05 '15 20:10 dvdzkwsk

@neverfox did you ever figure this out?

dvdzkwsk avatar Nov 09 '15 13:11 dvdzkwsk

No, but I've come to the conclusion that perhaps the expectation was unreasonable. I think it's probably improper to lean on webpack's ability to resolve some global identifiers and that I should always just be explicit with my dependencies. I can live with that.

neverfox avatar Nov 10 '15 01:11 neverfox

Ok, well I'll close this for now then.

dvdzkwsk avatar Nov 10 '15 01:11 dvdzkwsk

I think I'm getting a similar error to this. Whenever I'm trying to test a component that uses something with react-bootstrap (at least I think thats whats causing the issue) I get this:

screen shot 2016-01-04 at 1 13 48 pm

@neverfox did you ever figure out how to fix this or is this a different issue completely?

I tried your solution by adding the react-bootstrap node module to the files section of karma.conf like this:

  files: [
    './node_modules/phantomjs-polyfill/bind-polyfill.js',
    './node_modules/react-bootstrap/dist/react-bootstrap.js',
    {
      pattern: `./${config.dir_test}/**/*.js`,
      watched: false,
      served: true,
      included: true
    }
  ],

But then the error I get is: screen shot 2016-01-04 at 1 21 27 pm

SpencerCDixon avatar Jan 04 '16 18:01 SpencerCDixon

We had a similar problem. It may be related to the Webpack DeDupePlugin. Removing it helped us out

bradbirnbaum avatar Jan 04 '16 18:01 bradbirnbaum

Wow that totally fixed it. @davezuko we should record this fix in appropriate place. The error message was so mysterious/useless I had no idea where to even begin hahaha. Thank you @bradbirnbaum much appreciated.

Are there serious negative side affects for getting rid of the dedupe plugin? Will it just load multiple versions of the library dependencies? Or is it almost a pre-optmization that I probably don't need to worry about if this is a small SPA?

SpencerCDixon avatar Jan 04 '16 18:01 SpencerCDixon

@SpencerCDixon glad that helped. We were seeing similar errors in our tests. The first run would work but subsequent runs would fail. It only started as out project got larger.

bradbirnbaum avatar Jan 04 '16 18:01 bradbirnbaum