Skip to content

Workaround for nodeunit #338

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed

Workaround for nodeunit #338

wants to merge 1 commit into from

Conversation

bud-mo
Copy link
Contributor

@bud-mo bud-mo commented Mar 27, 2015

Using nodeunit on a project which use browserify, return an error like

/home/bud/my_secret_project/node_modules/raven-js/dist/raven.js:1871
 })((function() { if (window) { return window; } else { return this;} })());
                      ^
 ReferenceError: window is not defined

Obviously in a node context the window is not defined, passing this arguments is fine to run the tests.

I have tested (with internal tests) this patch and the result was:

./node_modules/.bin/grunt test dist
Running "jshint:all" (jshint) task
>> 9 files lint free.

Running "mocha:all" (mocha) task
Testing: test/index.html


  ․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․Error: Raven has not been configured.
․․․․․․․․․․․․․․
  ․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․

  124 passing (8s)

>> 124 passed! (8.31s)

Running "clean:0" (clean) task
Cleaning "build"...OK

Running "gitinfo" task

Running "version" task

Running "concat:core" (concat) task
File "build/raven.js" created.

Running "uglify:dist" (uglify) task
Source Map "build/raven.min.map" created.
File "build/raven.min.js" created.

Running "fixSourceMaps:all" (fixSourceMaps) task
File "build/raven.min.map" fixed.

Running "copy:dist" (copy) task
Created 1 directories, copied 3 files

Done, without errors.

Using nodeunit on a project which use browserify, return an error like
'''
/home/bud/my_secret_project/node_modules/raven-js/dist/raven.js:1871
 })((function() { if (window) { return window; } else { return this;} })());
                      ^
 ReferenceError: window is not defined
'''

Obviously in a node context the window is not defined, passing `this` arguments is fine to run the tests.

I have tested (with internal tests) this patch and the result was:
'''
./node_modules/.bin/grunt test dist
Running "jshint:all" (jshint) task
>> 9 files lint free.

Running "mocha:all" (mocha) task
Testing: test/index.html

  
  ․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․Error: Raven has not been configured.
․․․․․․․․․․․․․․
  ․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․

  124 passing (8s)

>> 124 passed! (8.31s)

Running "clean:0" (clean) task
Cleaning "build"...OK

Running "gitinfo" task

Running "version" task

Running "concat:core" (concat) task
File "build/raven.js" created.

Running "uglify:dist" (uglify) task
Source Map "build/raven.min.map" created.
File "build/raven.min.js" created.

Running "fixSourceMaps:all" (fixSourceMaps) task
File "build/raven.min.map" fixed.

Running "copy:dist" (copy) task
Created 1 directories, copied 3 files
'''
@mattrobenolt
Copy link
Contributor

I'm not gonna lie, I'm hesitant to pull this change solely because I think all of this _footer.js stuff is wrong. It has become unwieldy and unmaintainable.

This is the proposed new version:

// Expose Raven to the world
if (typeof define === 'function' && define.amd) {
    // AMD
    window.Raven = Raven;
    define('raven', [], function() {
      return Raven;
    });
} else if (typeof module === 'object') {
    // browserify
    module.exports = Raven;
} else if (typeof exports === 'object') {
    // CommonJS
    exports = Raven;
} else {
    // Everything else
    window.Raven = Raven;
}

})(
  (function() {
    if (typeof window !== 'undefined') {
      return window;
    } else {
      return this;
    }
  })()
);

Is this really how every other project works? I'm finding this hard to believe. Why is this not a solved problem that can just be copy/pasted as a whole from some other more, well established project?

@mattrobenolt
Copy link
Contributor

So jQuery has four pieces that are used for it's build from what I can tell at a quick glance:

https://github.com/jquery/jquery/blob/master/src/intro.js
https://github.com/jquery/jquery/blob/master/src/exports/amd.js
https://github.com/jquery/jquery/blob/master/src/exports/global.js
https://github.com/jquery/jquery/blob/master/src/outro.js

This compiles together to generate:

(function( global, factory ) {

    if ( typeof module === "object" && typeof module.exports === "object" ) {
        // For CommonJS and CommonJS-like environments where a proper `window`
        // is present, execute the factory and get jQuery.
        // For environments that do not have a `window` with a `document`
        // (such as Node.js), expose a factory as module.exports.
        // This accentuates the need for the creation of a real `window`.
        // e.g. var jQuery = require("jquery")(window);
        // See ticket #14549 for more info.
        module.exports = global.document ?
            factory( global, true ) :
            function( w ) {
                if ( !w.document ) {
                    throw new Error( "jQuery requires a window with a document" );
                }
                return factory( w );
            };
    } else {
        factory( global );
    }

// Pass this if window is not defined yet
}(typeof window !== "undefined" ? window : this, function( window, noGlobal ) {


/*
 * [ Insert all of jQuery here ]
 */

// Register as a named AMD module, since jQuery can be concatenated with other
// files that may use define, but not via a proper concatenation script that
// understands anonymous AMD modules. A named AMD is safest and most robust
// way to register. Lowercase jquery is used because AMD module names are
// derived from file names, and jQuery is normally delivered in a lowercase
// file name. Do this after creating the global so that if an AMD module wants
// to call noConflict to hide this version of jQuery, it will work.

// Note that for maximum portability, libraries that are not jQuery should
// declare themselves as anonymous modules, and avoid setting a global if an
// AMD loader is present. jQuery is a special case. For more information, see
// https://github.com/jrburke/requirejs/wiki/Updating-existing-libraries#wiki-anon

if ( typeof define === "function" && define.amd ) {
    define( "jquery", [], function() {
        return jQuery;
    });
}




var
    // Map over jQuery in case of overwrite
    _jQuery = window.jQuery,

    // Map over the $ in case of overwrite
    _$ = window.$;

jQuery.noConflict = function( deep ) {
    if ( window.$ === jQuery ) {
        window.$ = _$;
    }

    if ( deep && window.jQuery === jQuery ) {
        window.jQuery = _jQuery;
    }

    return jQuery;
};

// Expose jQuery and $ identifiers, even in AMD
// (#7102#comment:10, https://github.com/jquery/jquery/pull/557)
// and CommonJS for browser emulators (#13566)
if ( typeof noGlobal === strundefined ) {
    window.jQuery = window.$ = jQuery;
}




return jQuery;

}));

I don't know much about this stuff, but can't we just adapt what jQuery does and be done with all of this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants