andersekdahl / karma-common-js

Test CommonJS modules without using Browserify
MIT License
2 stars 4 forks source link

karma-common-js

Test CommonJS modules without using Browserify

What's the point of creating yet another karma preprocessor for CommonJS modules? Because it scratched an itch, and because I think there's a need for a preprocessor which doesn't use Browserify, but exposes the feature set of Browserify like requiring Node built-in modules and respecting the browser field of package.json in modules.

How does it work?

Instead of using Browserify to build a bundle (which takes way too long) this preprocessor finds all require statements in your code and rewrites them to absolute paths. If you require something from node_modules, that path gets rewritten to the correct path as well (and it respects the browser field in that packages package.json).

It takes all code inside your js files and wrap it in functions which gets the Common JS variables passed in like module, global, etc. So when the code runs, the require function looks up that created function for the correct path, and invokes it if it hasn't been invoked before.

Why should I use this instead of the others?

I'm not saying that this preprocessor is better than the others, but has a few features that others are missing. Such as:

Mocks

In your tests you often want to override which modules gets passed in to the module you want to test. Let's say I have a module store and a module greeter like this:

// store.js
module.exports = {
  currentUser: function () {
    // Time consuming stuff to get the current user here
  }
};
// greeter.js
var store = require('./store');

module.exports = function () {
  return 'Hi ' + store.currentUser().name '!';
};

To test greeter.js, you can mock store.js like this:

// greeter-test.js
var greeter = require('./greeter', {
  './store': {
    currentUser: function () {
      return {name: 'Anders'}
    }
  }
});

expect(greeter()).to.equal('Hi Anders!');

How to use

In your karma config, add common_js to the frameworks array, and add this to your preprocessors:

preprocessors: {
  '**/!(*-test).js': ['coverage', 'common_js'], // Remove coverage if you don't use karma-coverage
  '**/*-test.js': ['common_js'],
  '**/*.html': ['common_js'], // If you want transforms for HTML files
}

This section is for configuring the preprocessor:

common_js: {
  transforms: {
    'html-browserify': true // If you want it
    'babelify': {
      exclude: '**/node_modules/**' // Don't apply the babelify transform for files in node_modules
    }
  },
  // Array of globs to auto require when the tests run. You can use
  // this to control the entry point for your tests.
  autoRequire: [
    '**/*-test.js'
  ]
},