3
votes

I'm using two js files to make a test with Mocha.

My test.js file looks like this:

const assert = require('assert');
const operations = require('./operations.js');

it('Calculates correct deserialization', () => {

  var leftLeft = new operations.Node('left.left', null, null);
  var left = new operations.Node('left', leftLeft, null);
  var right = new operations.Node('right', null, null);
  var root = new operations.Node('root', left, right);
  assert.equal(operations.deserialize(serialize(root)).left.left.val == 'left.left');
});

and my operations.js file looks like this:

function Node(val, left, right) {
  this.val = val;
  this.left = left;
  this.right = right;
}

although incomplete, the test fails to instantiate the Node objects, and exits with the message

"TypeError: operations.Node is not a constructor"

I've already tried with

var leftLeft = new Node('left.left', null, null);

that is, without de operations.Node(). I'm using strict mode.

1

1 Answers

1
votes

If you are going to require() the file as a module, then you must export the function:

function Node(val, left, right) {
  this.val = val;
  this.left = left;
  this.right = right;
}

module.exports = Node;