Good day.

There is a browser game on js, all the code in one file, the file is connected to html. There was a question about how to make unit tests for the game. I looked at mocha and jasmine examples, but the simplest functions that return a specific value are tested there and in tests this return value is compared with the expected one. In my case, the functions in the code do not return anything, one sets the pictures, the other changes the coordinates of the player, the other draws, etc. An example of such a function:

function checkReadyState() { if (game.gameOverAudio.readyState === 4 && game.backgroundAudio.readyState === 4) { window.clearInterval(game.checkAudio); document.getElementById('loading').style.display = "none"; game.start(); }} 

This function returns nothing, and how can you tell if it passed the test or not? Or for each function add some special return value, which will indicate that the function has completed successfully?

How to test such code? And in what file should it be done, if all the code is in one file? I'm new to testing, so I rely on your advice. Thank you in advance

    2 answers 2

    There was a question about how to make unit tests for the game.

    In your case, no way.

    By definition, a modular (aka unit) test checks a small isolated piece of code - a module . Judging by the example of the code from the question, there is no talk of any modularity of the application. Totally.

    If you really, really want to do exactly the unit tests , start by isolating the modules and isolating the dependencies of the modules from each other. The question of how to isolate modules and break dependencies requires a separate discussion and should be based on the example of a specific code.

    If the task is to test the application, but there is no time to prepare the code for unit testing, then you can start with functional testing . When using this approach, you check the correctness of the work is not a separate isolated module, but a larger piece of the application. In your case, it can be the entire application. (In the latter case, I would talk about end-to-end testing, but this is a question of terminology.)

    At the same time, you need to understand: functional testing, not a replacement for modular, but an addition. Unit tests should cover most of the code base, and functional tests should check the interaction between modules.

    If we talk about tools for functional testing, then there may be a lot of options. I would start with CasperJS (or something like that).

    Here is an example of a functional test using CasperJS

     casper.test.begin('Game start', 5, function suite(test) { casper.start('http://www.example.com/', function() { test.assertTitle('Game title'); test.assertExists('form#user-info'); this.fill('form#user-info', { username: 'Foo' }, true); }); casper.then(function() { test.assertTitle('Game started for Foo!'); }); casper.run(function() { test.done(); }); }); 

      Firstly, it is strange that in the function called checkReadyState() game starts. Wouldn't it be more logical to return true/false and then start the game based on this? Then you just need to write a test for the function to check the true/false values. If you are writing in a functional style , write clean functions.