-1

I am working on a NetSuite suitescript customization and would like to write the test with Jest. Since suitescript run with Require JS, I would like to know how to correctly mock the function as I need.

SourceCode

/**
 * @NApiVersion 2.1
 * @NScriptType UserEventScript
 */
define([],() => {
        const afterSubmit = (scriptContext) => {
            try {
                if(scriptContext.type === 'create'){
                 foo();
                }
            } catch (error) {
                log.error('afterSubmit', error);
            }
        }

        function foo(){
            //do sth...
        }
        return {afterSubmit,
            //Export for testing purpose
            foo}
    });

Test file

import * as ueScript from 'the file path'

describe('Check run situation',()=>{
    const MOCK_SCRIPT_CONTEXT = {};
    it('Should call foo when type is create',()=>{
        jest.spyOn(ueScript, 'foo');
        MOCK_SCRIPT_CONTEXT.type = 'create';
        ueScript.afterSubmit(MOCK_SCRIPT_CONTEXT);
        expect(ueScript.foo).toHaveBeenCalled();
    });
});

I expect when type = 'create', the foo should be called, but the test result was foo has not been called.

I know maybe I am not correctly mocking the function that is actually called, but I have no idea about how to make it.

I have researched this topic with the keyword 'jest mock function in same module' but yet to find a way that works.

any idea about this?

2 Answers 2

0

I ran into the same issue when writing tests. I could not figure out a way to test the helper execution this way. What I ended up doing was writing the tests to check if what is inside the helper function gets executed.

ex.

/**
 * @NApiVersion 2.1
 * @NScriptType UserEventScript
 */
define(['N/record'],
    /**
 * @param{record} record
 */
    (record) => {

        /**
         * Defines the function definition that is executed after 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 afterSubmit = (scriptContext) => {

            if (scriptContext.type == 'create'){
                foo();
            }

        }

        /**
         * Helper Function
         */
        const foo = () => {
            
            const recordInstance = record.create({
                type: record.Type.INVENTORY_ITEM,
                isDynamic: true
            });

            // Do more things

        }

        return {afterSubmit, foo}

    });


Test

import testModule from 'SuiteScripts/testModule.js';
import record from 'N/record';

jest.mock('N/record');

let afterSubmitContext = {
    newRecord: {},
    oldRecord: {},
    mode: ''
}

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

describe('testModule', () => {

    test('Helper Function test', () => {

        afterSubmitContext.type = 'create';

        testModule.afterSubmit(afterSubmitContext);

        expect(record.create).toHaveBeenCalledWith({
            type: record.Type.INVENTORY_ITEM,
            isDynamic: true
        });

    });

    test('Helper function incorrect parameters', () => {

        afterSubmitContext.type = 'not create';

        testModule.afterSubmit(afterSubmitContext);

        expect(record.create).not.toHaveBeenCalledWith({
            type: record.Type.INVENTORY_ITEM,
            isDynamic: true
        });

    })

});

I don't think this is the best way to write tests, but this is how I am doing it for now until I figure out this issue.

I went through and double checked with the debugger and according to what it says the helper function 'foo' was a part of the module and defined as a mockFunction. I stepped through the execution and it does get triggered, but that doesn't get registered as a call in jest for some reason. It might be an issue on Oracle's side, I'm not sure.

Sign up to request clarification or add additional context in comments.

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
0

I finally found the resolution about this by help another community.

So two things to make this work.

  1. Use 'this.function' to make sure the function called is in the current instance, which is important to this case.

  2. By 1., 'this' keyword is not able to use in the ()=>{} kind statement, need to be used in the function funcName(){} kind function statement.

For making the test case work, see below.
Test

import * as ueScript from 'the file path'

describe('Check run situation',()=>{
    const MOCK_SCRIPT_CONTEXT = {};
    it('Should call foo when type is create',()=>{
        jest.spyOn(ueScript, 'foo');
        MOCK_SCRIPT_CONTEXT.type = 'create';
        ueScript.afterSubmit(MOCK_SCRIPT_CONTEXT);
//before typo, change to check on correct function
        expect(ueScript.foo).toHaveBeenCalled();
    });
});

SourceCode



/**
 * @NApiVersion 2.1
 * @NScriptType UserEventScript
 */
define([],() => {
        //const afterSubmit = (scriptContext) => {
        function afterSubmit(scriptContext){
            try {
                if(scriptContext.type === 'create'){
                 this.foo();
                }
            } catch (error) {
                log.error('afterSubmit', error);
            }
        }

        function foo(){
            //do sth...
        }
        return {afterSubmit,
            //Export for testing purpose
            foo}
    });

By this way, the test should pass.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.