How to prevent jest to execute a function

Hi,

I had a file say locale.js in which I have some statements and I am requiring that file in my test cases in other files.

What happened is that I have added a function in locale.js which I am executing when that file is loaded and it is working fine but the problem is that since this file is also required by jest in test case files it is also executing that file and getting an error in test files.

Any suggestion is appreciated.

Thanks
Gaurav Balyan

Hi Gaurav,

If I understand your question correctly, easiest way would be to put your function into separate file, e.g. utils.js, import it into locale.js and call. Then you can test your function separately in utils.spec.js and have separate tests for locale.js, possibly mocking your function in locale.spec.js so it won’t matter if it executes on load.

Hope it helps :slight_smile:
z