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's best practice to name imported dependencies consistently with what they'll represent in the respective SuiteScript file.

To find the correct file path for each module, see SuiteCloud Jest Configuration.

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. Start a test statement by using the global describe() method to group 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 can use afterAll(), afterEach(), beforeAll(), beforeEach() to run tests under specific conditions and times. These methods allow you to run a function in the script file before or after each test in a file runs.

The following example uses 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 can structure your tests using the Given, When, Then format. This format keeps your code organized and helps reduce errors. You can add this structure as an optional comment 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