Take the 2-minute tour ×

I need to add functionality to a Javascript class that I want to test using Jasmine. The class definition goes something like this:

GAME.Player.CustomPlayerSetup = function() {

If I remove GAME.Player I can write normal tests and have them pass. But when try to leave Game.Player in the class definition for CustomPlayerSetup, I get a Reference Error Game is not defined. How do define this test?

To answer Charles' suggestion, even if I declare the GAME and Player vars in my Jasmine test file before the the require line it still gives me, as in

GAME = {};
GAME.Player = {};

CustomPlayerSetup = require("../CustomPlayerSetup").CustomPlayerSetup;

describe("Custom Player Setup", function() {

the same error which points to the production code file, and not the test file. I'm using jasmine-node to run the tests.

share|improve this question

2 Answers 2

Most likely in your spec-runner you have whatever file instantiates Game and/or player below the file running the spec (or you didn't include it at all).

If that's not the case try posting your spec-runner as well as one of your tests that fails.

share|improve this answer

Looks to me, you need to instantiate the object GAME.Player before run/include the js file on your jasmine tests. something like:

<script type="text/javascript">
    var GAME = {Player:{}};
</script src="your_src.js"/>
share|improve this answer

Your Answer


