The Now Platform® Washington DC release is live. Watch now!

Help
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Kalaiarasan Pus
Giga Sage

For people who did not read my last blog post, I am in the process of creating a reusable set of functions that can reduce writing boilerplate code.

Since the project is going to be a script include with growing set of functions, I decided to write a follow up blog on the testing strategy that I use for script includes. As I am getting better with programming, I have been consciously trying to make my functions as pure functions and follow single responsibility principle more religiously. This ensures my script include functions can be unit tested individually, thus making unit testing process more fun.

 

One of the way that I have been using for unit testing script includes is to leverage Automated Test Framework (ATF), more specifically 'Run Server Side Script' step.

For people who don’t know, you can use 'Run Server Side Script' step to write Jasmine test suites. Jasmine framework does not depend on any other JavaScript frameworks. It does not require DOM, which makes execution super fast. And it has a clean, obvious syntax so that you can easily write tests.

 

You can make an ATF server side function to execute as a Jasmine test by including a single line outside of the function definition.

jasmine.getEnv().execute();

 

This blog is not meant to be a tutorial on what or how to write Jasmine tests but for the uninitiated, below is a brief intro.

Jasmine test suite are simple to create and understand. Below is a sample test suite.

describe("A suite", function () {
    it("Test1", function () {
        expect(true).toBe(true);
    });
    it("Test2", function () {
        expect(true).toBeNull();
    });
});

 

describe:

  • Used to group specs or test together. This is basically a function which contains child functions (tests).
  • First parameter – Use it to give your test suite a name of your liking.
  • Since these are basically Javascript functions, variables declared in this section can be accessed in your 'it' functions (tests).

 

it:

  • Represents individual test or spec.
  • This is also a function but contains an assertion, which needs to evaluate to something.

 

expect:

  • Keyword which contains the expression or function that represents what is the expected output of the test.

matchers:

  • There are bunch of matchers available.
  • This is used to compare the actual test result vs expected test result.
  • This produces the end result of the test, that is whether it is passed or failed.
  • Some of the matchers available are toEqual(), toBeNull(), toBeUndefined(), etc.
  • You can add negative assertion by chaining the call to expect with a ‘not’ before calling the matcher. Ex: expect(false).not.toBe(true);


You can read more about Jasmine in the documentation HERE.

Enough back story but how all of this is done in ATF?

Lets take a look at a simple script include - DoStuff. This has a function defaultParam, which checks if arguments are passed to it. If yes, it returns it back and if not, it will default the param to ‘test’. So now to test this function, we would need two tests, one to test function with argument and one without.

 

var DoStuff = Class.create();
DoStuff.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
    defaultParam: function() {
        var param = (arguments.length > 0 && arguments[0] !== undefined) ? arguments[0] : 'test';
        return param;
    },
    type: "DoStuff"
});

 

Create a ATF test and add 'Run Server Side Script' step. Writing our Jasmine tests for the script might look like.

 

(function (outputs, steps, stepResult, assertEqual) {
    describe('Unit test for DoStuff', function () {

        var script;

        //Set up things which are common for all tests
        beforeAll(function () {
            script = new DoStuff();
        });

        it('defaultParam returns test', function () {
            expect(script.defaultParam()).toEqual('test');
        });

        it('defaultParam returns param', function () {
            expect(script.defaultParam('param')).toEqual('param');
        });

    });

})(outputs, steps, stepResult, assertEqual);

jasmine.getEnv().execute();


The first 'it' block (test) tests the function if no parameter is passed. "We expect the return value to be Equal to 'test'" is the literal translation of this test 🙂 Similarly the second, tests for the function if argument is passed.

 

Now open your ATF test and run it to see the results. If you look over the test steps results, you might find something like this

----- Tests Complete -----
Spec :: Unit test for DoStuff defaultParam returns param :: passed
Spec :: Unit test for DoStuff defaultParam returns test :: passed
Test results :: (2/2) :: passed

 

As you can see, both of our tests have passed and we are good to go 🙂

 

Remember, using ATF as shown here will make your development process that much more fun and stress free. Now anytime you make an update to this script, all you have to do is run your ATF. You will be able to quickly determine whether you broke something which was unexpected or whether your test needs an update. And, if you really want to follow test driven development, writing such an ATF is all you need to get started.

 

Following the process like above, add your unit tests for individual functions as needed using Jasmine tests and chain together tests to form the test suite for the entire script. 

I am adding the updateset to the post if someone wishes to try it on their instance. I hope this helps someone.

 

If you liked the content, please share, click helpful, bookmark or leave your valuable comments.

3 Comments