TypeError in unit testing using Javascript, node.js and Chai

36 Views Asked by At

I am trying to create unit tests using javascript, node js and Chai for a calculator app. My directory is set up with an src folder containing the calculator app and a test folder containing the test file with a package json file.

Unit Test file

const { expect } = import('chai');
const { cos, sin, tan, sqrt, ln, exp, compute, square, checkNum } = import('./script');

describe('Trigonometric Functions', function () {
    it('should compute the cosine of a number', function () {
        const result = cos({ display: { value: 0 } });
        expect(result).to.equal(1);
    });

    it('should compute the sine of a number', function () {
        const result = sin({ display: { value: Math.PI / 2 } });
        expect(result).to.equal(1);
    });

    it('should compute the tangent of a number', function () {
        const result = tan({ display: { value: 0 } });
        expect(result).to.equal(0);
    });
});

describe('Mathematical Functions', function () {
    it('should compute the square root of a number', function () {
        const result = sqrt({ display: { value: 16 } });
        expect(result).to.equal(4);
    });

    it('should compute the natural logarithm of a number', function () {
        const result = ln({ display: { value: Math.E } });
        expect(result).to.equal(1);
    });

    it('should compute the exponential of a number', function () {
        const result = exp({ display: { value: 2 } });
        expect(result).to.equal(Math.exp(2));
    });
});

describe('Computation and Validation', function () {
    it('should perform basic computation using eval', function () {
        const result = compute({ display: { value: '2 + 3' } });
        expect(result).to.equal(5);
    });

    it('should square a number', function () {
        const result = square({ display: { value: 4 } });
        expect(result).to.equal(16);
    });

    it('should validate numeric input', function () {
        const validInput = checkNum('12345');
        const invalidInput = checkNum('12a345');
        expect(validInput).to.be.true;
        expect(invalidInput).to.be.false;
    });
});

Calculator app file

function addChar(input, character) {
    if(input.value == null || input.value == "0")
        input.value = character
    else
        input.value += character
}

function cos(form) {
    form.display.value = Math.cos(form.display.value);
}

function sin(form) {
    form.display.value = Math.sin(form.display.value);
}

function tan(form) {
    form.display.value = Math.tan(form.display.value);
}

function sqrt(form) {
    form.display.value = Math.sqrt(form.display.value);
}

function ln(form) {
    form.display.value = Math.log(form.display.value);
}

function exp(form) {
    form.display.value = Math.exp(form.display.value);
}

function deleteChar(input) {
    input.value = input.value.substring(0, input.value.length - 1)
}
var val = 0.0;
function percent(input) {
  val = input.value;
  input.value = input.value + "%";
}

function changeSign(input) {
    if(input.value.substring(0, 1) == "-")
        input.value = input.value.substring(1, input.value.length)
    else
        input.value = "-" + input.value
}

function compute(form) {
  //if (val !== 0.0) {
   // var percent = form.display.value;  
   // percent = pcent.substring(percent.indexOf("%")+1);
   // form.display.value = parseFloat(percent)/100 * val;
    //val = 0.0;
 // } else 
    form.display.value = eval(form.display.value);
  }


function square(form) {
    form.display.value = eval(form.display.value) * eval(form.display.value)
}

function checkNum(str) {
    for (var i = 0; i < str.length; i++) {
        var ch = str.charAt(i);
        if (ch < "0" || ch > "9") {
            if (ch != "/" && ch != "*" && ch != "+" && ch != "-" && ch != "."
                && ch != "(" && ch!= ")" && ch != "%") {
                alert("invalid entry!")
                return false
                }
            }
        }
        return true
}


Package Json

{
  "name": "chai",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "mocha"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "chai": "^5.0.0",
    "Math": "^0.0.1-security",
    "mocha": "^10.2.0",
    "save-dev": "^0.0.1-security"
  }
}

When I try to run it on the command prompt, every line returns an error. It must be a simple mistake but I am unsure of what is going wrong.

Command Prompt Error Message

1

There are 1 best solutions below

0
Rahul On

Your code has multiple issues -

  1. export functions using ES6 export or module.exports syntax
  2. use require or import syntax correctly based on ES module

See below code, I have modified imports, you can use require as well. Add {"type" : "module"} to your package.json file

// script.js file

export function addChar(input, character) {
    if (input.value == null || input.value == "0")
        input.value = character
    else
        input.value += character
}

export function cos(form) {
    form.display.value = Math.cos(form.display.value);
}

export function sin(form) {
    form.display.value = Math.sin(form.display.value);
}

export function tan(form) {
    form.display.value = Math.tan(form.display.value);
}

export function sqrt(form) {
    form.display.value = Math.sqrt(form.display.value);
}

export function ln(form) {
    form.display.value = Math.log(form.display.value);
}

export function exp(form) {
    form.display.value = Math.exp(form.display.value);
}

export function deleteChar(input) {
    input.value = input.value.substring(0, input.value.length - 1)
}
var val = 0.0;
export function percent(input) {
    val = input.value;
    input.value = input.value + "%";
}

export function changeSign(input) {
    if (input.value.substring(0, 1) == "-")
        input.value = input.value.substring(1, input.value.length)
    else
        input.value = "-" + input.value
}

export function compute(form) {
    //if (val !== 0.0) {
    // var percent = form.display.value;  
    // percent = pcent.substring(percent.indexOf("%")+1);
    // form.display.value = parseFloat(percent)/100 * val;
    //val = 0.0;
    // } else 
    form.display.value = eval(form.display.value);
}


export function square(form) {
    form.display.value = eval(form.display.value) * eval(form.display.value)
}

export function checkNum(str) {
    for (var i = 0; i < str.length; i++) {
        var ch = str.charAt(i);
        if (ch < "0" || ch > "9") {
            if (ch != "/" && ch != "*" && ch != "+" && ch != "-" && ch != "."
                && ch != "(" && ch != ")" && ch != "%") {
                alert("invalid entry!")
                return false
            }
        }
    }
    return true
}

// test.js file

import { expect } from "chai"
import { cos, sin, tan, sqrt, ln, exp, compute, square, checkNum } from "./script.js";

describe('Trigonometric Functions', function () {
    it('should compute the cosine of a number', function () {
        const result = cos({ display: { value: 0 } });
        expect(result).to.equal(1);
    });

    it('should compute the sine of a number', function () {
        const result = sin({ display: { value: Math.PI / 2 } });
        expect(result).to.equal(1);
    });

    it('should compute the tangent of a number', function () {
        const result = tan({ display: { value: 0 } });
        expect(result).to.equal(0);
    });
});

describe('Mathematical Functions', function () {
    it('should compute the square root of a number', function () {
        const result = sqrt({ display: { value: 16 } });
        expect(result).to.equal(4);
    });

    it('should compute the natural logarithm of a number', function () {
        const result = ln({ display: { value: Math.E } });
        expect(result).to.equal(1);
    });

    it('should compute the exponential of a number', function () {
        const result = exp({ display: { value: 2 } });
        expect(result).to.equal(Math.exp(2));
    });
});

describe('Computation and Validation', function () {
    it('should perform basic computation using eval', function () {
        const result = compute({ display: { value: '2 + 3' } });
        expect(result).to.equal(5);
    });

    it('should square a number', function () {
        const result = square({ display: { value: 4 } });
        expect(result).to.equal(16);
    });

    it('should validate numeric input', function () {
        const validInput = checkNum('12345');
        const invalidInput = checkNum('12a345');
        expect(validInput).to.be.true;
        expect(invalidInput).to.be.false;
    });
});

Try this out!