How to unit test component modules with dependencies in terminal? #41

enyo opened this Issue Sep 16, 2012 · 7 comments


None yet

5 participants

enyo commented Sep 16, 2012

I'm kinda stuck there... Unit testing a component with mocha is pretty easy. But as soon as there are dependencies involved it gets kinda messy. I can't just install the other components with npm, and I can't include the build.js in mocha.

So, am I forced to write the tests in the browser and include the build?

EDIT: And I'm aware that unit testing should happen without the other modules, but I can't stub them because my component already fails when trying to require another one.

tj commented Sep 16, 2012

yeah that part is not super easy right now, but since they're designed for the browser they should be tested in browser(s) or phantomjs etc. I do use mocha and node to test a few of the more simple ones but that breaks down with deps like you found. I dont think this is really a component specific issue, it's more or less the same issue any non-node environment faces

@tj tj closed this Sep 16, 2012
tj commented Sep 16, 2012

in the future I'd love to normalize all component testing with a test-swarm type of tool, but that's definitely not on the immediate list of things to do

reinpk commented Mar 18, 2013

this is still a pretty big pain for me. our current workaround is to change the standard build from this:

@component build --dev

to use the standalone flag:

@component build --standalone build --out ./build --name build

and then require build.js as so that i can do mocha testing in the terminal. but this breaks down when you want to include one of your dependency components (or any other component) for use in your testing suite.

how can we unbreak require-ing components in our testing suites? two ideas so far:

  • map components/ to node_modules/ somehow so that require() works
  • component build your mocha test so that require() works

am i missing something obvious or is this a big problem for testing with component (when your component isn't a tiny piece of code, but has dependencies).

jhnns commented Mar 18, 2013

I'm currently using webpack to cope with different module styles like CommonJS and AMD. In combination with webpack-dev-server and the mocha-loader of webpack it's pretty easy to write JavaScript modules and get instantly feedback from the browser. You can check these tests for a live example.

Unfortunately webpack doesn't support components yet, but maybe in the next few weeks.

jhnns commented Mar 18, 2013

Now he has written a plugin for components ^^.

Sorry, I just realized that it doesn't really answer your question. But it makes testing in the browser almost like testing in the terminal.

reinpk commented Mar 18, 2013

yeah, it looks like testing in the browser w/mocha is the way to go. if your tests fail at least chrome dev tools can help you debug.... would be nice to have testing in the terminal though. thanks!


I have a workflow that lets me run tests in phantomjs via grunt. I use grunt to watch my index.js and build it automatically. I then use mocha in browser-mode, including the build.js file, and exposing the component via the require('name/repo). I can then run my tests via grunt mocha. It works quite well. You can see it w/ a (more extensive) component I've been working on recently:

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