PositiveGuy
PositiveGuy

Reputation: 20172

Cannot convert undefined or null to object

Not sure where I went wrong here:

test.js

let chai = require('chai'),
    should = chai.should(),
    game = require('../src/game');

    it('should be able to start the game', () => {
        game.start();

        game.started.should.be.true;
    });

game.js

var board = require('./board'),
    hasStarted = false;

module.exports = {
    start: start,
    started: hasStarted
};

function start(){
    hasStarted = true;
};

for the test I get the assertion error:

AssertionError: expected false to be true

I thought I had set it in my start() method so why is my test still failing with false?

Upvotes: 2

Views: 1503

Answers (2)

Devendiran Kumar
Devendiran Kumar

Reputation: 223

Since the local variable you have used is a primitive type it doesn't reflect after calling start method.Primitive types in js are passed by value.

var hasStarted = {
    isStarted: false
};
var game = {
    start: start,
    started: hasStarted
};

function start() {
    hasStarted.isStarted = true;
};

module.exports = game;

This will work has you expect

Upvotes: 0

Grozz
Grozz

Reputation: 8425

You've assigned initial value of hasStarted to your module exports, it didn't change with a call to start().

Use a function to retrieve it instead of a variable, i.e.:

module.exports = {
    start: start,
    started: function() { return hasStarted; }
};

Upvotes: 4

Related Questions