vendredi 25 janvier 2019

Simplest way to test JavaScript code (for web-frontend) on each Git push

I'm looking for the simplest most minimalist way to create automated tests on each Git push for a relatively simple JavaScript code (on GitHub) for an HTML/CSS/JS web application.

I assume it would need some sort of a "headless browser" testing - in which case I would only need it to be executed in Google Chrome. (This application is not planned be used in any other browser.)

I already created a JS function that does the testing. For the sake of a simple example, let's say I always expect an even number that is larger than 5. In that case the function would be:

function my_test() {
    is_valid = true;
    if (num % 2 != 0) {
        console.log("it's not an even number");
        is_valid = false;
    } else if (num <= 5) {
        console.log("it's smaller than 5");
        is_valid = false;
    }
    return is_valid ;
}

So the question is, what would be the simplest way to make this test automatically be run in the terminal whenever executing a git push in the terminal?

I know that there are Node.js testing frameworks such as Tape, but it seems complicated for what I imagine (or hope) could be done simpler; plus if I install these it adds MBs to the otherwise tiny application (around 0.3 MB) which I don't really like.

Such testing could then be automatically run with e.g. Travis, so for example I could just add a ".travis.yml" file, with

language: node_js
node_js:
- "node"

An then in the package.json file I could add e.g. the line "test": "jshint my_code.js" to test the my_code.js with the jshint package.

But is there a way to execute my own simple JS function as a test? E.g., to say "first load my page with all my related JS code, then execute my custom my_test() function (above), display (in the terminal) whatever would be output in the console (via console.log('output')), and, if in the end it does not return true, disrupt the git push".

I appreciate any suggestions, but the best would be a clear description of what steps should be taken to set all this up. E.g., "create the .travis.yml with such and such content, add the package.json file with such and such content, etc.". And/or if it's perhaps also possible to use e.g. Tape without installing it on top of the application, but just automatically calling it whenever the testing is executed, then how would that be done.

(Note that I don't insist on Travis or Tape at all, these were just examples.)

Aucun commentaire:

Enregistrer un commentaire