karma-runner / karma-commonjs

A Karma plugin. Test CommonJS modules.
MIT License
73 stars 30 forks source link

Modules not found during testing #11

Closed spikebrehm closed 10 years ago

spikebrehm commented 10 years ago

Hello,

I just switched a project over to using karma-commonjs, but i'm seeing an issue where a required module isn't found. Perhaps you may know how to fix it?

https://github.com/airbnb/rendr/compare/karma-commonjs

The error I'm getting is:

Uncaught Error: Could not find module '/Users/spike/code/rendr/examples/00_simple/app/collections/repos.js' from '/Users/spike/code/rendr/examples/00_simple/test/app/collections/repos.test.js'
        at /Users/spike/code/rendr/examples/00_simple/node_modules/karma-commonjs/client/commonjs_bridge.js:18

Which is strange because is the file is certainly there:

$ ls /Users/spike/code/rendr/examples/00_simple/app/collections/repos.js
-rw-r--r--  1 spike  staff   291B Oct 25 13:23 /Users/spike/code/rendr/examples/00_simple/app/collections/repos.js

You can see the test file, repos.test.js, here:

https://github.com/airbnb/rendr/blob/42e160e212ceee333ae93b2742ef1cdfb61ac485/examples/00_simple/test/app/collections/repos.test.js

Any thoughts?

pkozlowski-opensource commented 10 years ago

@spikebrehm I'm introducing karma-commonjs into one of my project and could try to help you if you could prepare a separate repository with a minimal reproduce scenario. It is kind of hard to go over all the files in your repo right now.

spikebrehm commented 10 years ago

Ok, thanks. Let me see how easy it is to come up with a reproducible scenario.

jamesshore commented 10 years ago

On a related note, it looks like I introduced a bug way back when in commonjs_bridge.js on line 9. The error message used to be more descriptive. Let's change it to say:

Could not find module '../../../app/collections/repos' ('/Users/spike/code/rendr/examples/00_simple/app/collections/repos.js') from '/Users/spike/code/rendr/examples/00_simple/test/app/collections/repos.test.js'

In other words, rather than overwriting dependency on line 9, let's save the user's original dependency so we can report it.

spikebrehm commented 10 years ago

Oh, this was me misunderstanding the Karma config file. The files array has to include both application source files and test files; the config I pasted was just loading the test files.

Now my next question: what about NPM dependencies? What do we have to do so that our application files can require NPM modules?

Am I better off running Browserify to create both 'app' and 'test' bundles as a separate step, rather than trying to use karma-commonjs?

jamesshore commented 10 years ago

@pkozlowski-opensource has been working on supporting npm dependencies, but it isn't ready yet. See #10.

spikebrehm commented 10 years ago

Thanks. I'm going to close for now, presuming I'll be using karma-browserify.