Before You Begin

Unit tests examine and verify the behavior of your code. The goal of unit testing is to isolate the smallest piece of code in a system, to ensure it works properly. Throughout this tutorial, we will write our tests with the help of Jest Test Framework. Jest is a unit testing framework that allows you to examine the behavior of JavaScript code. It automates the unit testing process by providing many features to ensure accuracy and efficiency.

Matchers

It is important to understand the concept of expectations when writing unit tests. When writing a unit test, you should expect a certain outcome for that unit of code. Jest provides built-in methods, known as Matchers, that are used to verify test expectations.

Let's say we want to write a test for the following JavaScript function. We can expect this function to return the product of two values. We will assign hypothetical values to x and y in the unit test file to confirm the behavior of this function.

          const product = (x, y) => {
    return x * y;
} 

        

The following test sample uses the toBe(value) and toBeGreaterThan(number) matchers. This test shows that we should expect the product of 5 and 3 to be 15. We can also expect the product of 2 and 7 to be greater than 10. The example provides more than one expectation to verify the code works as intended.

          it('Should test the product function', () => {
    expect(product(5, 3)).toBe(15);
    expect(product(2, 7)).toBeGreaterThan(10);
}); 

        

Mock Functions

Jest also provides Mock Functions to spy on the behavior of your scripts. Mock functions allow you to establish a relationship between Jest and the unit of code you are testing. For more information about mocking SuiteScript dependencies, see Mocking.

The following example uses the mockReturnValue(value) mock function to return an arbitrary value.

          const mockFunction = jest.fn(); //tells Jest to mock function
const mockFunction2 = jest.fn();

mockFunction.mockReturnValue(20); // returns 20
mockFunction2.mockReturnValue('hello'); // returns hello 

        

Unit Testing Tips and Tricks

Here are a few helpful tips to know before diving into unit testing:

  • Identify the unit of code you want to test. Typically, unit tests are constructed based on the behavior of conditional statements.

  • It is not required to test each line of code in your script. Jest allows you to keep track of the amount of code tested with code coverage. This feature provides a detailed report of which lines in your code have been accurately tested. For more information about code coverage, see Code Coverage.

  • Familiarize yourself with the concept of mocking. Mocking is an essential concept needed to write unit tests. For more information about mocking in SuiteScript, see Mocking.

  • Structure your test file with Given, When, Then formatting. It will help to reduce errors and keep your test file organized. For more information, see Given, When, Then Formatting Structure.

General Notices