Search code examples
javascriptintern

Load Test csript inside test in intern.js


I'm trying to load test script according to custom argument passed through start command to start intern test. To do this I'm trying to require the specific test script inside a test but i am getting Attempt to require unloaded module error. This is my code set up. Can someone help on on this or sugest some alternative work around to make this work.

define(function (require) {
var intern = require('intern');
var AdvalentAutomationTestSuite = require('intern!object');
AdvalentAutomationTestSuite({
    name: 'Advalent Automation Test',

    'AdvalentTestSets': function () {
        return this.remote
            .then(function () {
                var product = intern.args.product;
                var script = 'Automation/TestScripts/FRG/' + product + '-Config';
                require(script)
            })
        },
    });
 });

Update:

Including intern.js file:

    define(function (require) {
var intern = require('intern');
console.log(intern)
return {
    proxyPort: 9000,

    proxyUrl: 'http://localhost:9000/',
    defaultTimeout: 120000,
    capabilities: {
        'selenium_version': '2.48.2',
    },

    environments: [
        {browserName: 'chrome', version: '48', platform: ['WINDOWS'], chromeOptions: {args: ['start-maximized']}},
    ],

    maxConcurrency: 3,

    tunnel: 'NullTunnel',
    reporters: [
        {id: 'JUnit', filename: 'test-reports/report.xml'},
        {id: 'Runner'},
    ],

    Loaders: {
        'host-node': 'dojo/dojo',
        'host-browser': 'node_modules/dojo/dojo.js'
    },
    loaderOptions: {
        packages: [{name: 'intern-tutorial', location: '.'}]
    },

    functionalSuites: [

        'Automation/TestScripts/FRG/FRG-Config',
    ],
    defaultTimeout: 70000,
    excludeInstrumentation: /^(?:tests|node_modules)\//
}

});


Solution

  • You should be fine with the default loader, although as @Troopers points out, it's loaders, not Loaders. The problem is that you're doing a dynamic require with a computed name:

    var script = 'Automation/TestScripts/FRG/' + product + '-Config';
    require(script)
    

    AMD loaders don't completely support the require(script) syntax since they don't load modules synchronously. When a module is written in CJS-compatibility mode, the loader fakes it by scanning the module code for require calls and then preloading and caching the modules before executing the module code. When the require(script) call is eventually executed, the preloaded module is returned.

    When you use a computed module name, the loader can't preload the module being required, so the synchronous require call will fail. To load a module with a computed name you'll need to use the require([ dependency ]) syntax, like:

    var script = 'Automation/TestScripts/FRG/' + product + '-Config';
    return new Promise(function (resolve) {
        require([ script ], resolve);
    });
    

    At a higher level, though, it seems odd to be doing this in a test in the first place. It seems like something that should be handled at the module or config levels. For example, assuming 'Automation/TestScripts/FRG/' + product + '-Config' is a functional test suite, the config could simply add that suite to the functionalSuites list if the required command line argument were provided.