Using Spies and Mocks on complex objects with Jest

I’m fairly new to testing and writing tests for a currently uncovered javaScript codebase using Jest. The code covers some niche use cases, as its conditionally injected and executed by the browser during page load. Anyway, I’m having issues mocking up custom objects. Here’s the function in question: As far as I understand, I need to mock both window.location.pathname to return a string, and I need to mock window.TOOL.cookie.setCookie() as a mock function. Here’s my attempt at the test: The test fails, saying that window.TOOL.cookie.setCookie was called 0 times. I’ve dug into the process, and found that window.location.pathname is executing

How to determine if JEST is running the code or not?

I am creating a JS test on my react-native project. I’m specifically using firebase for react native, in which I would like to replace firebase instance with a mockfirebase instance if JS is running the code of my class. For example I have class setup like below. I’d like to have a check if jest is the running environment then I’d replace the import line with appropriate mock class. Answer jest sets an environment variable called JEST_WORKER_ID so you check if this is set: I also see that if NODE_ENV is not set the jest CLI sets it to the

How to mock Push notification native module in React native jest tests?

When using the module react-native-push-notification, I had this error: I tried to mock the module by creating __mocks__/react-native.js and putting this code within it: Now, I have this error: How I could mock fully this module the right way? Answer I mocked the module PushNotificationIOS by creating a setup file jest/setup.js: I’ve configured jest to run this setup file by adding this line into packages.json: