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
?
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);
});
});