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?