jestjsrequirejssuitescriptsuitescript2.0

Require JS with jest to mock function in same module


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?


Solution

  • 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.