2

I'm trying to setup intern for my project, a Dojo/JS project, and the server is not Node... I get a loader issue, which seems to be due to dojo.has using Dojo loader... The require wrapper suggested in here did not work for me.

I get the error below:

> node node_modules/intern/client.js config=tests/intern
Defaulting to "console" reporter
dojo/text plugin failed to load because loader does not support getText
TypeError: undefined is not a function
at Object.load (lib/dojo/dojo/text.js:199:6)

Below are my intern configuration and the test file:

/tests/intern.js: (config file)

loader: {
    packages: [ { name: 'visitorsPortal', location: 'portals/visitor' },
        { name: 'dojo', location: 'lib/dojo/dojo'},
        { name: 'dijit', location: 'lib/dojo/dijit'},               
        { name: 'portalLib', location: 'portals/lib'} ]
},
suites: [ 'tests/uitests' ],

tests/uitests:

define([
    'intern!tdd',
    'intern/chai!assert',
    'portals/visitor/views/MyModule'
 ], function (test, assert, MyModule) {

     // empty for now...

 });
Community
  • 1
  • 1
stafamus
  • 509
  • 2
  • 9

2 Answers2

3

This has nothing to do with dojo/has and everything to do with the dojo/text plugin requiring functionality that only exists within the Dojo 1 loader when being used server-side.

If you are attempting to test software that relies on any non-standard AMD loader functionality, you will need to use the non-standard loader, or override those modules with alternative copies that are compatible with other loaders.

In this specific case, your easiest path forward is to use the geezer edition of Intern, since it includes the old Dojo loader which contains these non-standard extensions. The best path forward is to remap the dojo/text module to another compatible module that does not need anything special in the loader in order to retrieve the data:

// in intern.js
// ...
loader: {
  map: {
    '*': {
      'dojo/text': 'my/text'
    }
  }
},
// ...
C Snover
  • 17,908
  • 5
  • 29
  • 39
1

I struggled with the same problem yesterday, but thanks to C Snover's answer here and the question you're linking to, I did make some progress.

I added the map directive to the intern.js loader config (as C Snover suggests).

    // in intern.js
    // ...
    loader: {
      map: {
        '*': {
          'dojo/text': 'my/text'
        }
      }
    },
    // ...

For the my/text module, I just copied dojo/text and added an else if clause to the part that resolves the getText function:

if(has("host-browser")){
    getText= function(url, sync, load){
        request(url, {sync:!!sync}).then(load);
    };
} else if(has("host-node")){      
    // This was my addition...
    getText = function(url, sync, load) {
        require(["dojo/node!fs"], function(fs) {
            fs.readFile(url, 'utf-8', function(err, data) {
                if(err) console.error("Failed to read file", url);
                load(data);
            });
        });
    };
} else {
      // Path for node.js and rhino, to load from local file system.
      // TODO: use node.js native methods rather than depending on a require.getText() method to exist.
      if(require.getText){

          getText= require.getText;

      }else{
          console.error("dojo/text plugin failed to load because loader does not support getText");
      }
  }

However, even though the tests were running in intern via node, the host-node value wasn't being set. That was fixed by setting dojo-has-api in my intern.js config:

define(["intern/node_modules/dojo/has"], function(has) {

    has.add("dojo-has-api", true);

    return { /* my intern config as normal */ };
});

I'll admit I don't understand 100% what I've done here, and with the copy/pasting it's not exactly pretty, but it serves as a temporary fix for my problem at least.

Note: This did introduce another set of issues though: Since Dojo now knows that it's running in node, dojo/request no longer tries to use XHR. I was using sinon.js to mock my xhr requests, so this had to be changed.

Community
  • 1
  • 1
Frode
  • 5,600
  • 1
  • 25
  • 25