Search code examples
javascriptamdintern

Avoid loading DOM relient modules while testing


I'm using the intern for testing javascript in a project. The javascript we are trying to test does not require the DOM, and should be able to be run within a node process without trouble. However, the modules that I'm loading for the test also load jquery (as an example), which hits the dom on load.

//file.js
require([..., 'jquery'], function(..., $) {
    // non-jquery code to test
})

//intern test
require(['intern!object', 'file'], function(registerSuite, file) {
    registerSuite({ 
        ...

Loading jquery in the module causes intern to break immediately when run in a node process, even though I want to test something completely different. Is there a correct way to get around this? I tried just pointing the jquery module to an empty file, but this doesn't work for all cases.

Thank you.


Solution

  • If any code has a hard dependency on the complete, pre-built copy of jQuery, then it must run in a DOM environment, as jQuery is a DOM library. If the code itself doesn’t require a DOM, then it shouldn’t have jQuery as a dependency, at which point it will load fine in Node.js.

    If you need a non-DOM utility library, try Dojo or Lo-Dash or something else that isn’t a DOM library like jQuery.

    If you have a module that may use the DOM, Dojo comes with the dojo/has module that can be used as an AMD loader plugin to conditionally load DOM dependencies only when the module is loaded in a browser.

    Finally, note that any AMD module (like file.js in your example) should contain a define call, not a require call.