Testing a function without a return statement in mocha

Hey folks,

I am working on a project and I am testing its functions using mocha. Can you help me understand how do I test the second function, the one that does not return anything?

Here are the functions I am in need to test so far:

// I have already written the code for this one, see below
function compareGuesses(humanGuess, computerGuess, target) {
    let humanDifference = Math.abs(target - humanGuess);
    let computerDifference = Math.abs(target - computerGuess);

    return (humanDifference <= computerDifference) ? true : false;
}

// I need to test if the two variables, humanScore and computerScore change
function updateScore(winner) {
    (winner === 'human') ? humanScore++ : computerScore++;
}

And this is the testing for the first function. Not sure how to write the test for the second one.

const {compareGuesses, updateScore} = require('../script');
const assert = require('assert');

describe('compareGuesses()', () => {
    it('returns true or false', () => {
        let target = 10;
        let humanGuess = 5;
        let computerGuess = 3;

        let result = compareGuesses(humanGuess, computerGuess, target);

        assert.ok(result, 'true');
    })
});

Thanks in advance =)

Hey @nik84,
I’m actually not sure that this will work, but it’s an idea that might be worth trying!

There is an assert.notEqual() function which you can read more about here. With that in mind, what you could do is save the human/computer score before calling the function and then after calling the function as variables, and then use assert.notEqual() to check if there has been a change? You might need to make two it() blocks (one to check each score), but that’s a reasonable thing (as you’d actually be checking two separate things).

Alternatively, you could use assert.equal() and create a variable called something like ‘expected’ to check that the score has changed in the way you expect.

1 Like

Thanks a lot mate =)

1 Like