I’m experiencing an inconsistency between the behavior of my unit tests and the actual functionality of a JavaScript function, ConvertHandler.getNum
, within a unit conversion application.
Problem Description:
My unit test convertHandler.getNum should return an error on a double-fraction input is designed to assert that the getNum
function throws an error with the message "invalid number" when provided with an input like "3/2/3 mi", which contains a double fraction. However, while running the test suite, the error is not being thrown as expected.
convertHandler
should correctly return an error on a double-fraction (i.e. 3/2/3).
So, when I manually input "3/2/3 mi" in the browser frontend, the application does display the correct error message "invalid number". This discrepancy between the behavior of the unit test and the frontend interaction raises questions about the accuracy of my unit tests or potential issues with the environment.
Why the unit test fails to throw the expected error while the frontend interaction produces the correct error message ?
controllersconvertHandler.js:
function regexSplit(input) {
const matches = input.match(/([.d/]+)|([a-zA-Z]+)/g);
const number = matches
? matches.find((match) => /[.d/]+/.test(match))
: "1";
const string = matches
? matches.find((match) => /[a-zA-Z]+/.test(match))
: "";
return [number, string];
}
function onlyOneDiv(input) {
let fractionParts = input.split("/");
if (fractionParts.length > 2) {
return false;
}
return fractionParts;
}
function ConvertHandler() {
this.getNum = function (input) {
const number = regexSplit(input)[0] || "1"; // if nothing is provided it will default to 1.
const fractionArray = onlyOneDiv(number);
if (isNaN(parseFloat(fractionArray[0])) || isNaN(parseFloat(fractionArray[1] || "1"))) {
return "invalid number";
}
return parseFloat(fractionArray[0]) / parseFloat(fractionArray[1] || "1");
};
}
module.exports = ConvertHandler;
tests1_unit-tests.js:
const chai = require("chai");
let assert = chai.assert;
const ConvertHandler = require("../controllers/convertHandler.js");
let convertHandler = new ConvertHandler();
suite("Unit Tests", function () {
test("convertHandler.getNum should return an error on a double-fraction input", function () {
assert.throw(() => convertHandler.getNum("3/2/3 mi"), "invalid number");
});
});
Running test returns error in terminal:
convertHandler.getNum should return an error on a double-fraction input: AssertionError: expected [Function] to throw an error
In browser, code displays the error "invalid number" when i enter the value: "3/2/3 mi" :
Update: If i use assert.strictEqual
or assert.equal
instead of asset.throw
, the unit test throws error:
AssertionError: expected [Function] to equal ‘invalid number’
2
Answers
Solution is to change
assert.throw
toassert.equal
orassert.strictEqual
and remove the typo() =>
The issue is that
assert.throw()
expects a function to throw an error. Your function throws no errors, ever. It simply returns values, one of them being "invalid number" in case the input is a double fraction. To solve the problem you can do one of two things: make your function throw an actual error, or change the test to verify that the function returns the string "invalid number".