Javascript Test - Assertion library

> (World Wide) Web - (W3|WWW) > Javascript (Js|ECMAScript) > Javascript - Test

1 - About

assertion in Javascript

Advertising

3 - Library

They just throws an Error when the assertion is not satisfied.

4 - Devtool Console

Chrome console

function greaterThan(a,b) {
  console.assert(a > b, {"message":"a is not greater than b","a":a,"b":b});
}
greaterThan(5,6);

5 - Implementation Example

  • The function to test named plus1
function plus1(a){
    return a + 1;
}
  • The test function of the plus1 function
function assertEqual(then, expected) {
    if (result == expected) {
        console.log("The assertion was successful")
    } else {
        console.log("The assertion was not successful. then="+then+", expected="+expected)
    }
}
  • The test
var result = plus1(1);
var expectation = 2;
assertEqual(result, expectation);
  • The result

Advertising
web/javascript/test/assertion.txt · Last modified: 2019/06/19 11:23 by gerardnico