How to mock a function in JEST?

10 views
Skip to first unread message

Sudhir Goswami

unread,
Jul 30, 2019, 12:23:05 AM7/30/19
to Jest

I am trying to mock a function. below my code snippet

FileName : logging.js

 function trackEvent(event, user, object, objectId, extraProperties) {
    if (client !== null) {
        let properties = extraProperties || {};
        properties['user'] = user;
        properties['object_type'] = object;
        properties['object_id'] = objectId;
        client.trackEvent({ name: event, properties: properties });
    }
}

I created one test.js file and imported the my function like

const Logging = require('../Logging.js');

Please help me how to mock "trackEvent()" Function using JEST. As trackEvent() is not returning any value, Can it be verified by some mock function to be called once?


Thanks

Sudhir

Reply all
Reply to author
Forward
0 new messages