Loading your scripts

jstest does not care how you go about loading your code, so you are free to use any module loader you like. However, if you’re not using a module loader, or want to run your tests on both the client and the server, jstest can help you out.

In Getting started, you set up a page using script tags to load your code. On some platforms, these scripts will be aggressively cached, making it hard to test your code reliably. And if you want to run your tests on the server, or using other browser test runners, you’ll need to duplicate the information in the script tags using those platforms’ script loading APIs.

jstest lets you solve both these problems by using a programmatic API for loading your code. Add the following to example/runner.js:

// example/runner.js

var run = function() { JS.Test.autorun() }

var ROOT = JS.ENV.ROOT || '.'
JS.cache = false

JS.load(  ROOT + '/example/lib/set.js',
          ROOT + '/example/spec/set_spec.js',

          // add files here as the project grows

          run)

(JS.ENV gives you a reference to the global object on whatever platform you’re running jstest on; you can use this to access global variables easily.)

If you rewrite your page to use this, your code will be loaded dynamically, bypassing the browser cache:

<!-- example/browser.html -->

<!doctype html>
<html>
  <head>
    <meta charset="utf-8">
    <title>jstest</title>
  </head>
  <body>

    <script src="../build/jstest.js"></script>

    <script>ROOT = '..'</script>
    <script src="./runner.js"></script>

  </body>
</html>

You can reuse this script-loading code on the server, and with various browser test runners, minimizing the duplication you need in your test configuration.