Skip to Content


TinyWorld – Part 6

Create Node.js unit tests

/wp-content/uploads/2016/03/image001_916513.png

Welcome back!


In Introduction to the TinyWorld application we introduced the TinyWorld muti-part tutorial, describing how to develop applications for SAP HANA and XS Advanced, using the SAP Web IDE for SAP HANA (Web IDE). This is the first of our “advanced” topic blogs.

SAP HANA XS Classic supported a Jasmine-like unit test pattern called XSUnit. In this part of the tutorial, we will see how to use XSUnit to develop unit tests for our Node.js module.

You may have noticed that when we created a new Node.js module with XSJS support in the Web IDE, the template also created a test/ folder with an initial unit-test file called sampleTest.xsjslib.

Create a new Node.js Test Run Configuration for your tinyjs module via “Run > Run Configurations… “, call it for instance test, leave the default test file pattern, and click “Save and Run”.

/wp-content/uploads/2016/03/image034_917244.png

This will run your Node.js module in XSJS test mode and find the previously edited sampleTest.xsjslib file because it matches the test file pattern. The Node.js Test Execution pane will appear on the right of the Web IDE and display a result like below. Obviously, that test is not doing much yet (toggle Show Code Coverage:

/wp-content/uploads/2016/03/image035_917245.png

Now replace the function it(“not ok”…) with something meaningful, like expecting some calculator to correctly add up one and one:

it(“add simple”, function() {

   $.import(“calc”, “calculator”);

   var calc = $.calc.calculator;

   var sum = calc.add(1, 1);

   expect(sum).toBe(2);

});

This will of course not run because the function add from calc/calculator.xsjslib is not yet defined. So let’s create that file and fill it with the following contents:

function add(a, b) {

   return a+b;

}

The next test run should look much better. As you can see by the green background in calculator.xsjslib, the newly added business logic was indeed covered by our test:

/wp-content/uploads/2016/03/image036_917282.png

You can keep adding new tests as additional it functions or new test suites by adding new describe functions in the same or other *Test.xsjslib files in the predefined test/ folder.

Summary of part 6

Here we learned how to use the “Jasmine” framework to add tests to Node.js modules.

You can continue to explore the more advanced parts of this TinyWorld tutorial, covering things like the use of version control, adding authentication and authorization control to our application, and  how to manage the life cycle of our application, from development to deployment:

Part 7:   Under the hood

Part 8:   Source control

Part 9:   Application life cycle

Part 10: Add authentication

Part 11: Add authorization

To report this post you need to login first.

Be the first to leave a comment

You must be Logged on to comment or reply to a post.

Leave a Reply