RE: How to use jest with Suitecloud SDK?

I am trying to use JEST and also create more MOCK classes under stubs for integration tests but I need more examples to understand how it works.

Has anybody used it or have more samples to share? LIke how to correctly test User Event scripts?

 

Thanks

Add Comment
1 Answers

I was looking for the same and here is an example of how I tested my Sales Order User Event script. Hopefully this helps

 

File src/FileCabinet/SuiteScripts/SalesOrder-UserEvent.js


/**

* @NApiVersion 2.1

* @NScriptType UserEventScript

*/

define([],

() => {
/**

* Defines the function definition that is executed before record is submitted.

* @param {Object} scriptContext

* @param {Record} scriptContext.newRecord - New record

* @param {Record} scriptContext.oldRecord - Old record

* @param {string} scriptContext.type - Trigger type; use values from the context.UserEventType enum

* @since 2015.2

*/

const beforeSubmit = (scriptContext) => {

if(scriptContext.type === scriptContext.UserEventType.CREATE){

scriptContext.newRecord.setValue({fieldId: 'custbody_customfield', value: Math.random()});

}

}
return {beforeSubmit}
});

File __tests__/sales-order-test.js

import record from 'N/record'; import Record from 'N/record/instance'; import SalesOrderUserEvent from 'SuiteScripts/SalesOrder-UserEvent'; jest.mock('N/record'); jest.mock('N/record/instance'); beforeEach(() => { jest.clearAllMocks(); }); describe('Sales Order Test', () => { it('should add a random number to the sales order on create', () => { // given const salesOrderId = 1352; record.load.mockReturnValue(Record); Record.save.mockReturnValue(salesOrderId); const scriptContext = { type: 'create', newRecord: Record, UserEventType: { CREATE: 'create', EDIT: 'edit', } }; // when SalesOrderUserEvent.beforeSubmit(scriptContext); // then expect(Record.setValue).toHaveBeenCalledWith({fieldId: 'custbody_customfield', value: expect.anything()}); }); it('should not add a random number to the sales order on edit', () => { // given const salesOrderId = 1352; record.load.mockReturnValue(Record); Record.save.mockReturnValue(salesOrderId); const scriptContext = { type: 'edit', newRecord: Record, UserEventType: { CREATE: 'create', EDIT: 'edit', } }; // when SalesOrderUserEvent.beforeSubmit(scriptContext); // then expect(Record.setValue).not.toHaveBeenCalled(); }); });

Rookie Answered on October 29, 2021.
Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.