Archi Forum

Archi Plug-ins => jArchi => Topic started by: AlwaysLearning on January 20, 2023, 17:14:51 PM

Title: How do you unit test your scripts?
Post by: AlwaysLearning on January 20, 2023, 17:14:51 PM
How do you unit/end-to-end test you jArchi scripts?

I'm creating jArchi script which is complicated/valuable enough that I want to wrap it in tests. In these tests I'll populate and then empty a test model.

I can simply write a "Run Tests function" in a second script which includes the main script and then calls each of the test functions. 

I'm hoping for a Javascript testing framework/library recommendation if anyone use on inside jArchi. Needless to say JavaScript is not my core skillset.
Title: Re: How do you unit test your scripts?
Post by: Phil Beauvoir on January 26, 2023, 21:28:33 PM
Hi, it's an interesting question. Anyone unit testing their jArchi scripts? @Jean-Baptiste Sarrodie didn't you once create a testing suite?
Title: Re: How do you unit test your scripts?
Post by: Jean-Baptiste Sarrodie on January 27, 2023, 09:25:05 AM
Hi,

Quote from: Phil Beauvoir on January 26, 2023, 21:28:33 PM@Jean-Baptiste Sarrodie (https://forum.archimatetool.com/index.php?action=profile;u=6) didn't you once create a testing suite?

Not a real one.

Quote from: AlwaysLearning on January 20, 2023, 17:14:51 PMcan simply write a "Run Tests function" in a second script which includes the main script and then calls each of the test functions. 

That's basically how I do it. I try to separate script behavior and UI functions so that it becomes easier to test the behavior. But it also requires to define some helper function to easily switch on a debug mode which prints as much information as possible.

I'm not aware of a better testing framework for JS only application (ie. outside nodeJS), but if you find (or build) one, I could use it ;-)

Regards,

JB
Title: Re: How do you unit test your scripts?
Post by: jsimoncello on February 21, 2023, 14:32:59 PM
I have not found any test framework (at least one that does not require node js) so I am trying to developed from scratch a very minimalistic one.
First the service :
class SimpleObjectsCountService  {
    process(selection) {
        var counts = new Map()
        //get all items and count them by type
        $(selection).each(function (o) {
            var rootType = getRootTypeForType(o.type)
            if (counts.has(rootType)) {
                counts.set(rootType, counts.get(rootType) + 1)
   
            } else {
                counts.set(rootType, 1)
            }
        })
        return counts
    }
}


the ajs script that calls this service and handles output to console :

/*
 * Count everything objects in the model and output as CSV with no details
 */

load(__DIR__ + "/SimpleObjectsCountService.js");
load(__DIR__ + "/./../lib/Utils.js");
initScript();
main();
/**
 * main method
 * handles output to console
 */
function main() {
    var counts = new SimpleObjectsCountService().process("*");
    // output csv header
    logToConsoleAsCsv(";", "Type", "Qty");
    //for each type output its count
    counts.forEach(function (value, key) {
        logToConsoleAsCsv(";", key, value);
    });
}
and then the test script

load(__DIR__ + "/./../../reports/SimpleObjectsCountService.js") ;
debug=false;
/**
 *
 */
class SimpleObjectsCountTest extends ArchiUnitTest {
    processtest() {
       
        var counts= new SimpleObjectsCountService().process("*");

        assertEquals("Count concepts in model",counts.get("Concept"),5941);
        assertEquals("Count Other in model",counts.get("Other"),721);
        assertEquals("Count Relationships in model",counts.get("Relationships"),15227);
        assertEquals("Count Views in model",counts.get("Views"),736);
    }
   
  }

  new SimpleObjectsCountTest().doTest();

This test script defines a class that extends ArchiUnitTest which handles some helper methods


I am not entirely satisfied with this approach,I'd like to put both service and the script in a single file.

Finally, I have a 4th file that calls all tests one after the other

console.clear();
console.log("#######################################################")
console.log("Engine:" + $.process.engine);
console.log("Platform:" + $.process.platform);
console.log("Archi version" + $.process.release.archiVersion);;
console.log("#######################################################")

load(__DIR__ + "/./../lib/TestUtils.js");
load(__DIR__ + "/views/InitViewsTest.ajs")
load(__DIR__ + "/reports/SimpleObjectsCountTest.ajs");

Title: Re: How do you unit test your scripts?
Post by: Alexis_H on December 11, 2023, 06:51:04 AM
Hi,

I'm going on the same road : my jArchi scripts are getting more and more valuable/complex, and I need a unit-testing framework.


I'm not aware of a better testing framework for JS only application (ie. outside nodeJS), but if you find (or build) one, I could use it ;-)
I've spent a few hours trying different options : as you mentioned most of the Unit-Test fwk are aiming a Browser or Node.js execution stack .. but I ended up trying this 'fork' of Jasmine : graalvm_jasmine (https://github.com/pminearo/graalvm_jasmine) .. and "partialysucceeded" to run it within Archi : it launch and executes a few tests ('specs' in Jasmine langage) but I stumbled on a async issue making the tests results unavailable..

Will update you if I succeed to make it work.

Alexis