24

I have a simple JavaScript file, color.js, and a matching spec file, colorSpec.js.

color.js:

function Color() 
{

}

colorSpec.js:

require('./color.js');

describe("color", function() {
  it("should work", function() {
    new Color(255, 255, 255);
  });
});

When I run jasmine-node colorSpec.js, I get the following exception:

ReferenceError: Color is not defined

How can I get Jasmine to load my color.js file before running colorSpec.js?

LandonSchropp
  • 10,084
  • 22
  • 86
  • 149
  • I just found this question, which is similar: http://stackoverflow.com/questions/4662851/how-do-you-import-non-node-js-files – LandonSchropp Feb 26 '12 at 03:49

3 Answers3

20

you could load your color.js in the colorSpec.js with a require(). I dont see how jasmine can guess all the dependencies without you telling jasmine what they are exactly in your spec file. Edit : A quick and dirty solution , but maybe there is something builtin Jasmine to do that :

fs = require('fs')
myCode = fs.readFileSync('./color.js','utf-8') // depends on the file encoding
eval(myCode)

then your class should be available with jasmine

if you call require directly on your file i think you need to create a module and export it

mpm
  • 20,148
  • 7
  • 50
  • 55
  • I tried adding `require('./color.js');` at the top of my spec file but I'm still getting `RefereceError` exceptions. – LandonSchropp Feb 24 '12 at 19:05
  • 1
    This works, but it feels like `node.js` should have something built in that does this. Still, thanks for the help. Please let me know if you run across a different way of achieving this. – LandonSchropp Feb 26 '12 at 03:51
  • Hello! I i need to evaluate Angular or some other DOM/ Browser based library, then what can i do?? Window is not available in node! :( – Navaneeth Sep 25 '14 at 08:47
  • what does require('fs') does? – Ramtin Soltani Apr 01 '16 at 21:59
19

When using Jasmine Node, you'll want to export your object/function/class, in this case Color, as a node module. I like to try and make my modules work in both node or a browser, like so:

Folder Structure:

js
  - src/
      color.js
  - spec/
      colorSpec.js

src/color.js

/**
 * class Color
 *
 * @constructor
 */
function Color(red, green, blue)
{
    var current = [red, green, blue];

    this.getCurrent = function ()
    {
        return current;
    }
}

// Export node module.
if ( typeof module !== 'undefined' && module.hasOwnProperty('exports') )
{
    module.exports = Color;
}

spec/colorSpec.js

var Color = require('../src/color.js');

describe("Test the Color object", function() {
    var color = new Color(255, 255, 255);

    it('to verify that it can return a color.', function() {
        expect(color.getCurrent()).toContain(255);
    });
});
b01
  • 4,076
  • 2
  • 30
  • 30
  • Thanks. One question: can I omit the `module` existence checking logic? When can `module` be `undefined`? – Sanghyun Lee May 19 '17 at 16:01
  • 1
    If you never plan to run this in a browser, then yes. – b01 May 20 '17 at 14:51
  • hmm, I tried this method but I get `TypeError: util.roundDecimal is not a function` The only way that works is to `exports.roundDecimal = function` but I can't because my script is read by something that doesn't what `exports` is – DevEng Jul 31 '18 at 20:41
  • Seems like the export needed to be an object `module.exports = { roundDecimal: roundDecimal }` – DevEng Jul 31 '18 at 20:49
5

This is not how require works. Your color.js needs to define/export something. I will assume you use require.js here for sanity.

color.js

define('Color', function (require) {
  var Color = function () {};
  return Color;
});

Then in your spec:

var Color = require('color.js');
ggozad
  • 13,105
  • 3
  • 40
  • 49
  • 3
    I honestly wasn't planning on using `require.js`. I'm building a color library and I didn't want to force any dependencies on the people using it. Your answer is probably the accepted way to do this, but it doesn't fit my project requirements. – LandonSchropp Feb 26 '12 at 03:46