← Back to Upcase

Having trouble testing Javascript code


(Colby Williams) #1

I am just getting into using Jasmine, but have spent over 2 hours without the ability to even get one test to run.

I am getting the error TypeError: object is not a function for line 7.
The weird thing is that I basically copied the format from other projects that worked fine, and I can use calc = new Calc(); within calc.js without a problem. Nothing I do here works though.

Any ideas?

Here is the ‘spec/calc_spec.js’

var Calc = require('../Calc.js');

describe("Calc", function() {
  var calc;

  beforeEach(function() {
    calc = new Calc();
  });

  it("returns the max number in an array", function() {
    var arr = [1,2,3,4,2,7,5];
    expect(calc.my_max(arr)).toEqual(7);
  });
});

And my ‘Calc.js’

var Calc = function() {};

Calc.prototype.my_max = function(nums) {
  var max = 0;
  for( var i=0; i<nums.length; i++ ) {
    if( nums[i] > max ) max = nums[i];
  }
  return max;
};

(Jared Smith) #2

Hey Colby, is the Calc.js in the directory above the spec directory?

Also have you tried moving the definition of this function to the top of the spec file and see if it will run with the full definition at the top of the file?


(Colby Williams) #3

Yea the problem is with the require for sure. The file is being found, but for some reason it’s just loading an empty object.


(Colby Williams) #4

Man…the whole time, I just had to write in module.exports = Calc; at the bottom of my Calc.js.

Just didn’t know how all of that works. I’m still a noob to Javascript / Jasmine =/