简体   繁体   中英

How to test a Javascript object property when function with mocha, chai

Trying to test a Javascript object

Game.js

var GameManager= {
    gameType: 'room',
    roomDimension: [10,10],
    playerDirection: ["W"]
    possibleDirections: ["N","E","S","W"],
    init: function(){
        if(this.gameType == 'room'){
            regexpNumber = /^-?[0-9]$|-?([1][0-9])$/;
        }
    return true;
    },
    turnRight : function(){
    var movePosition = this.possibleDirections.indexOf(this.playerDirection);
    if(movePosition == this.possibleDirections.length -1 ){
        return this.possibleDirections[0];
    }
    return this.possibleDirections[movePosition + 1];
  },
    commandString: function(string){
    var command = /^[PQR]*$/;

    if(command.test(string){
        return true;
    }
    this.errorMessageNumber = 0;
    return false;
}
}

Here is my test script test.js

var expect = require("chai").expect;
var GameManager = require("../GameManager.js");

describe("Test game type", function() {
   beforeEach(function () {
      GameManager.gameType = 'room';
      GameManager.roomDimension= [10, 10];
      GameManager.possibleDirections: ["N","E","S","W"];
  });

  it('should commandString be as parameters', function() {
    expect(GameManager.commandString("AABB")).to.not.be.false;
  });

  it('should init toBeTruthy', function() {
    expect(GameManager.init()).ok;
  });
});

Issue: In both cases the test fail with TypeError error is shown as below for one of the test:

  1) Test game type should init toBeTruthy:
  TypeError: GameManager.init is not a function at Context.<anonymous>

Since init is not considered as function here, how can this be tested?

Why are you trying to test a simple object? I think, although it can be done, the UT are more intended to be behavioral tests.

That said, I think you're using the assert wrong:

it('should init toBeTruthy', function() {
   expect(GameManager.init()).to.be.ok;
});

Or to be more precise with the test:

it('should init toBeTruthy', function() {
   expect(GameManager.init()).to.be.true;
});

Also, you may refer to this post, it might be helpful

Why is my mocha/chai Error throwing test failing?

You said in a comment:

I am not exporting GameManager since it is a simple Javascript object. Not using as Typescript export class.

You still need to export it. Whether it is a simple JavaScript object or not is utterly irrelevant. You have to write your code in a way that works with Node.js' module system.

Using the code you show in the question, and fixing the syntax errors in it, I can get your second test (the one for init ) to work if I add this to your GameManager.js file:

module.exports = GameManager;

With the code you show in the question, the exported value from GameManager.js is {} . So when you do var GameManager = require("../GameManager.js"); the GameManager variable gets assigned the value {} . Consequently GameManager.init has the value undefined , and thus it is not a function. Adding the line I show above, will make it so that the GameManager variable in your test file gets the value of GameManager from your GameManager.js file, and the test will pass.

(You did not provide code for GameManager.commandString so the first test will still fail. Just add the code for it to get it to work.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM