Ability to import external libraries into test

Use case

This topic addresses the use case for importing external utility libraries, such moment for datetime formatting, into the test execution context.

Can we import node_modules?

Unfortunately, no.
Although UI-licious executes the tests within a NodeJS environment, it is done so within a secure sandbox, so importing external libraries as node_modules using the require() function or import keyword is not supported.

Current workarounds to import external libraries

One workaround is to import the external library distributable as a .js file into your UI-licious test project, and then use TEST.run to run the file to import the library which will initialise its variables into the global test execution context.


// add moment library into the project

var tomorrow = moment().add(1,'day').format('DD-MM-YYYY')
I.fill("Check-in Date", tomorrow)

How can we make it better?

One of the issues with the above workaround is that it runs the module’s code in global text execution namespace, allow the module to ‘pollute’ the global namespace. What if you already have a variable named ‘moment’ defined in the test and you want to import the ‘moment’ library as ‘momentjs’?

One of the ways to make this better is the implementation of something like the require() function based on CommonJS modules, which runs the module’s code in an isolated module scope. We could implement TEST.require, which could be used as such

// assign the module to a variable
var momentjs = TEST.require("lib/moment.min.js")

Another issue is that we might want these modules to be imported at the start of every run. Instead of writing TEST.require at the start of every test, we can define global imports uilicious.config.json, as such:

    "require": {
        "momentjs": "lib/moment.min.js"