Skip to content Skip to sidebar Skip to footer

Testing Custom Javascript (not Node Module) With Intern

Is it possible to create and run test suites for custom client-side JavaScript, not created as Node modules? How should the configuration be altered then? There is loader section

Solution 1:

1. Your test modules will always need to be written as AMD modules, but you can test any client-side code you want. Within your test module, just specify the non-AMD code as a dependency like any other module, then access the global variables that your script creates:

define([
  'intern!tdd',
  'intern/chai!assert',
  'intern/order!myPackage/myFoo.js'
], function (tdd, assert /* note, no assignment here */) {
  tdd.suite('foo suite', function () {
    tdd.test('something in foo', function () {
      // accessing a global variable created by `myPackage/myFoo.js`
      assert.ok(window.myFoo, 'Global myFoo object should exist');
    });
  });
});

2. You don’t need to do anything explicit in the Intern configuration in order for this to work, except to specify your test module in the suites array.

3. The recommended directory structure for a client-side application needing testing would look something like this:

/ - your entire application
    src/
        index.html - your app’s entry-point HTML
        app/ - your app package, containing application-specific JavaScript to be tested
            foo.js        - Some module `foo`
            tests/        - Intern tests for `app`
                foo.js    - Tests for `foo`
                intern.js - Intern configuration for `app`
    node_modules/
        intern/

This, of course, will vary depending upon how your application is already architected, but from a “starting from scratch” approach, this is our generally recommended directory structure.

Post a Comment for "Testing Custom Javascript (not Node Module) With Intern"