Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Testing with Mocha, ReferenceError: alert is not defined

I am trying to do a test where this calculator app will return a false and show an alert message when invalid entries are given. However, I keep getting this error message:

  1 failing

  1) checkNum function
       Test 3: should return false for a string with invalid entries:
     ReferenceError: alert is not defined
      at checkNum (test\test.js:78:5)
      at Context.<anonymous> (test\test.js:58:24)
      at process.processImmediate (node:internal/timers:478:21)

The following is my code. How do I resolve this issue? Thank you.

var assert = require('assert');

describe('checkNum function',()=>{
    it('Test 3: should return false for a string with invalid entries', () => {
        const result = checkNum('abc$def');
        assert.equal(result, false);
    });
});

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
}


  [1]: https://i.stack.imgur.com/MKiDi.png

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

This error occurred because Node.js was unable to execute alert,
the alert() is a method on the window object, which is only available in the browser

use console.log

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading