karma-runner / karma-commonjs

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

Multiple Load Paths #31

Open leeavital opened 10 years ago

leeavital commented 10 years ago

Not sure how feasible this would be, but it would be really useful to be able to have multiple paths for modules. One use-case is if there is library code in one folder and application code in another. It's impossible to test application code that depends on library code.

pkozlowski-opensource commented 10 years ago

@leeavital I'm kind of the same boat as you and I'm still wondering if this should be added or not...

Technically it would be rather easy to add support for multiple "root folders" from which modules can be loaded. Having said this there were number of things that were stopping me from doing so:

So, I'm not saying we shouldn't add it but I would love to have some more discussion on the topic before starting any implementation efforts. Thoughts?

leeavital commented 10 years ago

Anyway, I would be happy to work on this if it's something worth doing.

jamesshore commented 10 years ago

@leeavital The main motivation for karma-commonjs was to allow testing of Browserified code without needing to run Browserify itself. (That's slow and messes up stack traces, or at least it used to). What does Browserify and other tools' support for multiple trees look like?

leeavital commented 10 years ago

I can't say for browserify, as I've never used it myself.

Karma-commonjs-preprocessor allows you to register a function that rewrites a file name. So if you had a folder named vendor and a folder named application, you could write:

karmaCommonJs: { 
  options : { 
     pathReplace: function( path ){ return path.replace( /^(vendor|application)/, '') 
     } 
  } 
}

So modules registered in /vendor/a.js and /application/b.js, can be required as:

require('a'); 
require('b');