Skip to content
Advertisement

Tag: mocking

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

How to return based on actual arguments being sent?

Let’s say we have a function with two arguments. It’s being called many times with different arguments on every call. So, its impossible to stub it with withArgs option. I want to write a stub function which will check the actual arguments being passed (args1, args2) and return a static response with a switch case. Something on the following lines:

Getting `TypeError: jest.fn is not a function`

I’m trying to create the following unit test using Jest. But I’m getting the following error after running npm test. TypeError: jest.fn is not a function This is some section of my package.json: What could be the reason I’m getting that error? Answer The jest object is automatically in scope within every test file, so there’s no need to import

How to mock functions in the same module using Jest?

What’s the best way to correctly mock the following example? The problem is that after import time, foo keeps the reference to the original unmocked bar. module.js: module.test.js: I could change: to: but this is pretty ugly in my opinion to do everywhere. Answer fwiw, the solution I settled on was to use dependency injection, by setting a default argument.

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

Advertisement