How to run intern to test a dojo application runni

2020-04-12 08:24发布

问题:

I'm trying to use intern to test a dojo application running under node.js

My intern.js configuration file is something like:

define({
    loader: {           
            packages: [ 
                { name: 'elenajs', location: 'lib' }, 
                { name: 'tests', location: 'tests' } 
            ],
            map: { 
                'elenajs': { dojo: 'node_modules/dojo' },
                'tests': { dojo: 'node_modules/dojo' }
            }
    },
    suites: [ 'tests/all' ]});

When I try to run a test with node node_modules/intern/client.js config=tests/intern, I get this error: Error: node plugin failed to load because environment is not Node.js.

Normally I would have configured dojo with something like

dojoConfig = {
    ...
    hasCache: {
        "host-node": 1, // Ensure we "force" the loader into Node.js mode
        "dom": 0 // Ensure that none of the code assumes we have a DOM
    },
    ...
};

How can I solve this with intern?

回答1:

The issue that you are experiencing is caused by the fact that there is code in Dojo that relies on certain has-rules being set from the Dojo loader, but this doesn’t happen because the Dojo loader isn’t in use. There are a couple of potential workarounds:

  1. Since Intern’s loader sets some of the same rules, you can load intern/node_modules/dojo/has and run has.add('dojo-has-api', true) before anything else tries to load your dojo/has module. This should cause Dojo to use the has.js implementation from Intern’s loader (and adopt all the rules it already has set, which currently includes host-node). The best place to do this is going to be the Intern configuration file, which would end up being something like this:

    define([ 'intern/dojo/has' ], function (has) {
        has.add('dojo-has-api', true);
    
        return {
            // your existing Intern configuration object…
        };
    });
    
  2. Before loading any modules that call has('host-node'), load dojo/has and intern/node_modules/dojo/has and call dojoHas.add('host-node', internHas('host-node')) (or I guess you can just hard code it :)). This would require a loader plugin to be used in place of your suites array:

    // tests/preload.js
    define({
        load: function (id, require, callback) {
            require([ 'dojo/has' ], function (has) {
                has.add('host-node', true);
                require(id.split(/\s*,\s*/), callback);
            }
        }
    });
    

    And then your suites would change to suites: [ 'tests/preload!tests/all,tests/foo,tests/bar' ].

Any other has-rules that Dojo code relies on that are set by the Dojo loader will need to be set yourself. Any other rules that Dojo adds from other parts of itself will work correctly.



标签: intern