0

I use the browserify standalone option in the following gulp tasks to generate an UMD module:

gulp.task("bundle-source", function () {
  var b = browserify({
    standalone : 'inversify',
    entries: __dirname + "/build/source/inversify.js",
    debug: true
  });

The standalone option wraps the library code with the following code to ensure that it can be loaded as a Node module, an AMD module or a Global:

 !function(n) {
    if ("object" == typeof exports && "undefined" != typeof module) module.exports = n();
    else if ("function" == typeof define && define.amd) define([], n);
    else {
        var e;
        e = "undefined" != typeof window ? window : "undefined" != typeof global ? global : "undefined" != typeof self ? self : this, e.inversify = n()
    }
}(function() {
    return function n(e, t, i) {
        function r(u, p) {
            if (!t[u]) {
                if (!e[u]) {
                    var s = "function" == typeof require && require;
                    if (!p && s) return s(u, !0);
                    if (o) return o(u, !0);
                    var c = new Error("Cannot find module '" + u + "'");
                    throw c.code = "MODULE_NOT_FOUND", c
                }
                var f = t[u] = {
                    exports: {}
                };
                e[u][0].call(f.exports, function(n) {
                    var t = e[u][1][n];
                    return r(t ? t : n)
                }, f, f.exports, n, e, t, i)
            }
            return t[u].exports
        }
        for (var o = "function" == typeof require && require, u = 0; u < i.length; u++) r(i[u]);
        return r
    }({

    // The rest of the library code ...

I'm using istanbul to try to achieve 100% test coverage. My problems is that some parts of the UMD code snippet are not tested. For example, I'm not using AMD so lines like the ones below are never executed:

\\ ...
else if ("function" == typeof define && define.amd) define([], n);
\\ ...

or

\\ ...
if (!e[u]) {
  var s = "function" == typeof require && require;
  if (!p && s) return s(u, !0);
  if (o) return o(u, !0);
  var c = new Error("Cannot find module '" + u + "'");
  throw c.code = "MODULE_NOT_FOUND", c
}
\\ ...

This is preventing me from achieving 100% test coverage. Do you know how can I get around this problem?

Thanks!

JMM
  • 26,019
  • 3
  • 50
  • 55
Remo H. Jansen
  • 23,172
  • 11
  • 70
  • 93
  • Is it really valuable to try to cover browserify's boilerplate? – JMM Oct 09 '15 at 15:58
  • Well it is for an open-source project and being able to display the 100% test coverage badge will encourage some people to trust the project and I believe it is something really valuable. – Remo H. Jansen Oct 09 '15 at 16:11
  • In my opinion that's kind of a silly metric, as it all comes down to how much the users trust you to implement good tests. If they're willing to trust you based on a badge, perhaps they're willing to trust you to cover the important stuff and not worry about something insignificant like a UMD wrapper. – JMM Oct 09 '15 at 17:18

1 Answers1

2

I suggest you look into implementing Istanbul's ignoring features. That page specifically discusses ignoring UMD wrappers, although in the case of browserify you may have to do extra work to accomplish that, like make the bundle without standalone and implement the UMD wrapper yourself (using ForbesLindesay/umd or otherwise).

JMM
  • 26,019
  • 3
  • 50
  • 55