As the interpreter becomes more and more complex, testing the program in the textarea turns into an impossible mission. To test the javascript in HTML, better to separate the javascript file from the HTML file. But the javascript file could not be tested as the Node file, you need to do a little small change to export the function you want to test.

Your javascript file:

(function (exports) {
  "use strict";

  function myFunction(a, b) {
    return a + b;
  }

  exports.myFunction = myFunction;
})(this);

Then you can import you function in your test class as blow:

"use strict";

var assert = require('assert');
var myFunction = require('script.js').myFunction; //The script.js is the file contains your function

//Then you can test as a nodejs test case
assert.equal(myFunction(1, 2), 3);

Congratulations, you now can test your front-end javascript in your unit test case. To make it more effective I recommend Mocha and Chai as your testing framework. You can find those two framework in below website.

Both of the framework are available on npm.

$ npm install mocha
$ npm install chai

Recommend adding them to package.json file using a * as the version tag. This will ensure that you always have the most recent version.

Tesing javascript with Mocha

All the Mocha test case file should located in the test folder. Then we could enhance the above test case with Mocha.

"use strict";

var assert = require('assert');
var myFunction = require('script.js').myFunction; //The script.js is the file contains your function

//We can easily add some description to it
describe('myFunction(param1, param2)', function () {
  it('should return 3 if param1 = 1, param2 = 2', function (){
    assert.equal(myFunction(1, 2), 3);
  });
});

Run the mocha command in your terminal, also you can add as a test command to your package.json file. The result will print as below:

myFunction(param1, param2)
      ✓ should return 3 if param1 = 1, param2 = 2
1 passing (1ms)

If you want to assert an exception thrown by your function, you need to improve chai to your test cases. Ok lets make the myFunction throw an exception.

function myFunction(param1, param2) {
  if (isNaN(param1) || isNaN(param2)) {
    throw 'Param error';
  }

  return param1 + param2;
}

Then we add a test case for detect the exception:

describe('myFunction(param1, param2)', function () {
    it('should return 3 if param1 = 1, param2 = 2', function () {
      assert.equal(myFunction(1, 2), 3);
    });
    it('should throw an exception when param and param are not number', function (){
      //here we use chai to assert the exception
      expect(function () {
        assert.equal(myFunction('jack', 'rose'), 3);
      }).to.Throw('Param error');
    });
  });

Run again the mocha command in your terminal, as you see, the exception has been tested.

myFunction(param1, param2)
      ✓ should return 3 if param1 = 1, param2 = 2
      ✓ should throw an exception when param and param are not number

2 passing (1ms)

To find more useful test function you can refer to the official document of Mocha and Chai, this should helpful to test your frond-end javascript:)