Step Two: Setup Your Unit Test File

The following steps demonstrate how to setup your unit test file. These steps assume that your SuiteCloud Project folder is configured for unit testing. For additional help with project setup, see Step One: Setup Your IDE for Unit Testing.

Importing Project Dependencies

The beginning of your test file should import the script file being tested and module level dependencies used in your SuiteScript file. You can assign a name, of any value, to the script file imported. Use this assigned name when referencing your script throughout the test file. It is best practice to name imported dependencies consistent with what it will represent in the respective SuiteScript file.

The following example demonstrates how to import the necessary files from its location in the SuiteCloud project folder.

            // import the SuiteScript file 
import script from "../src/FileCabinet/SuiteScripts/ue_exampleScript";

// import modules used
import runtime from 'N/runtime';
import Script from 'N/runtime/script';
import User from 'N/runtime/user';
import Session from 'N/runtime/session'; 

          

Using Jest Globals

Jest provides a list of global methods and objects that can be used to run tests in a variety of ways. To initiate a test statement, you must use the global describe() method that will group together a set of related tests. The describe() method allows you to create a comment about what the group of tests consist of. Each test inside of the block, must be separated with a test()/it() block. The following example demonstrates how to create a set of related tests.

            describe('Group of related tests', () => {
   it('Should test the first function', () => {
      // test1 code goes here
   });
   it('Should test the second function', () => {
     // test2 code goes here
   });
}); 

          

In addition, you can delegate how your test blocks execute. You have the option to run tests according to certain conditions and times with afterAll(), afterEach(), beforeAll(), beforeEach(). These methods allow you to run a function in the script file before or after each test in a file runs.

The following example will use the beforeEach() global to clear all mocks before each test.

            // import the SuiteScript file 
import script from "../src/FileCabinet/SuiteScripts/ue_exampleScript";

// import modules used
import runtime from 'N/runtime';
import Script from 'N/runtime/script';
import User from 'N/runtime/user';
import Session from 'N/runtime/session';
 
beforeEach(() => {
   jest.clearAllMocks();
}); 

          

Given, When, Then Formatting Structure

You have the option to structure your tests with the Given, When, Then format. This format helps to keep your code organized and reduce errors. This structure is an optional comment to be added in your test code.

  • Given – This section contains variable definitions, script type definitions, and implementations of your mocked stubbed objects. It provides background information of your script to setup test cases. You can specify different scenarios to manipulate the output of a test case.

  • When – This section specifies when test execution will occur.

  • Then – This section contains expectations of what will happen on your defined scenarios in the given section.

            // import the SuiteScript file 
import script from "../src/FileCabinet/SuiteScripts/ue_exampleScript";

// import modules used
import runtime from 'N/runtime';
import Script from 'N/runtime/script';
import User from 'N/runtime/user';

beforeEach(() => {
   jest.clearAllMocks();
});

describe('Group of related tests', () => {
   it('Should test the first function', () => {
      // given
      context.newRecord = Record;
      runtime.getCurrentScript.mockReturnValue(Script);

      // when
      script.beforeLoad(context); // occurs in beforeLoad function of the script
    
      // then 
      expect(runtime.getCurrentScript).toHaveBeenCalled(); 
   });
}); 

          

General Notices