2

I have a non AMD JavaScript file with a class Snake like so:

// ./www/js/Snake.js
function Snake(initPos) {
  this.pos = initPos;
}

// I want to unit test this method.
Snake.proptotype.move = function(dir) {
  // Do things.
};

I wrote an Intern module to test it:

// ./test/Snake.js
define([
  'intern!object',
  'intern/chai!assert',
  "../www/js/Snake"
], function (registerSuite, assert, Snake) {
  registerSuite({

  name: 'Snake',

  move: function () {
    // Intern complains that this object is not a function.
    var snake = new Snake([
      {x: 0, y: 0}, 
      {x: 1, y: 0},
      {x: 2, y: 0}
    ]);

    snake.move("right");

    ...

How to make Intern recognize the Snake class?

SBel
  • 3,315
  • 6
  • 29
  • 47

1 Answers1

3

I found what the problem is. In the intern configuration file I need to set the loader packages option as follows:

// ./tests/intern.js
define({
  ...
  loader: {
    packages: [ { name: 'app', location: 'www/js' } ]
  },
  ...

Then in my test module I need to import the Snake.js file:

// ./tests/Snake.js
define([
  'intern!object',
  'intern/chai!assert',
  "intern/order!app/Snake.js"
], function (registerSuite, assert) {
  registerSuite({
  name: 'Snake',

  move: function () {
    var snake = new Snake([
      {x: 0, y: 0}, 
      {x: 1, y: 0},
      {x: 2, y: 0}
    ]);
    ...

Now it doesn't complain that Snake is undefined.

SBel
  • 3,315
  • 6
  • 29
  • 47