Overview

Writing a good testable code is key to stable and maintainable code. NetSuite does NOT offer any unit testing framework out of the box.

In Javascript world, I liked Mocha amongst the unit testing framework because of its simplicity and is easily pluggable with other libraries built for assertions, mocking, code coverage etc. than being a complete solution.

Why MochaJS for NetSuite

Amongst other unit javascript test frameworks I chose MochaJS because:

  1. Its simple
  2. Pluggable reporting capabilities
  3. It does not come up as one complete framework for everything - Mocks, Assertions, etc.

So, I thought of modifying this library for NetSuite

What Kind of Scripts you can unit test

The framework currently, supports running tests in the context of scheduled script and suitelet.

To use this framework you got to write more of your code base as libraries so, that they run in the context of any script like user event, suitelet, scheduled script etc.

How does MochaJS work in NetSuite?

MochaJS code has support for asynchronous code which isn’t supported in Javascript. I mostly ended up removing all asynchronous code and integrating suitelet UI for reporting and Emails for reporting. And, its use of Observer pattern made it easier to modify large section of code. If you are interested more look at suite.test.mocha.js in github code repo.

Looking for an example Test Suite?

Check the gihub repo

describe('credstore suite', function(){
          it('should be array', function(){
       //make sure there is no credentials for testplatform and test@test.com
                  var x = [2];
                  expect(x).to.exist;
                  expect(x).to.have.length(1);
     });

});

Getting Started

The github repo describes it. Feel free to leave a comment if there is a problem. I would be happy to resolve it.

Author:   Prasun Sultania

Tags:     NetSuite      UnitTesting   

Oct 16, 2015 06:56 pm

Comments