title: How to export functions for unit tests in PortalScript (ECMAScript5)
body: <p>We have an application server supporting an embedded server side scripting language called PortalScript (SpiderMonkey V1.8.5), which is compliant to the ECMA-Script-262 Edition 5 (ECMAScript5) specification. The Portalserver supports numerous so-called scripting hooks and event hooks, which allow to embed our scripts. These attached scripts implement our business logic which I want to test e.g. with Jasmine, Jest, etc.</p>
  
  <p>The first problem is that return statements at the end of the scripts are mandatory, and these return statements are not within functions. A minimal example could look like this:</p>
  
  <pre><code>// #import "myFctLib"
  
  var currentModel = given_context.model;
  if (!currentModel) {
      return -1;
  }
  currentModel.setAttribute("$fieldId", "SUCCESS");
  return 0;
  </code></pre>
  
  <p>These scripts can be quite large. So if we extract functions (within a script) to just test them, how could we call these functions from the tests? How can we export functions compliant to PortalScript? Does someone know if it's possible to use the CommonJS module system with PortalScript?</p>
  
  <p>Currently we don't use a module system but a precompiler to replace code for statements like <code>// #import "myFctLib"</code> (see first line in the example code) in the scripts to use functions from other scripts...</p>
  
  <p>In such a context, how would you write (unit) tests? How would your approach look like?</p>