in a client script we have this try/catch we are t...
# suitescript
s
in a client script we have this try/catch we are trying to write tests but are having very little luck
Copy code
catch (e) {
      if (typeof log !== "undefined") {
        
        log.error({
          title: `rate checkup Failed `,
          details: e.message || JSON.stringify(e) || "No Meaningful message",
        });
      } else {
        console.log(
          `Rate checkup failed: ${
            e.message || JSON.stringify(e) || "No meaningful message"
          }`
        );
      }
    }
  }
b
probably need to share more on the test side and what issues you are facing
s
Copy code
describe('shipmentrates function', () => {
  it('should log an error if <http://https.post|https.post> throws an error', () => {
    // Mock the error object
    const error = new Error('Network error');

    // Mock the <http://https.post|https.post> function to throw an error
    jest.spyOn(https, 'post').mockImplementation(() => { throw error; });

    // Spy on log.error
    jest.spyOn(log, 'error');

    // Call the function
    const result = shipment.shipmentrates(10, { shipaddress1: 'Address', city: 'City', state: 'State', zip: '12345' }, 'SKU');

    // Check if log.error is called with the correct parameters
    expect(log.error).toHaveBeenCalledWith({
      title: 'rate checkup Failed',
      details: error.message || JSON.stringify(error) || 'No Meaningful message',
    });

    // Restore the original implementation of <http://https.post|https.post>
    https.post.mockRestore();
  });
});
the issue is that LOG is undefined
e
Your test suite needs to mock the Log module and then export the mock to Jest's globals. Some examples using Jest's global
global
object
Typically ends up looking something like
Copy code
const log = require('N/log')
jest.mock(log, ...)
global.log = log
In the past I usually set up the Jest config file to automatically load my mocks of the implied modules (
log
,
util
, etc), but sadly I don't own that code so I can't share it offhand