The problem
Consider the scenario where we’re importing a specific function, targetFunction, from a module called @example/library:
import { targetFunction } from '@example/library'You might wonder how to correctly use jest.spyOn to monitor this function, especially what argument to pass first. Jest’s spyOn expects an object or module as the first parameter, and the name of the method to spy on as the second.
The trick to effectively monitoring a named export is to import the entire module’s contents as an object, then pass this object into jest.spyOn.
The Solution
import * as exampleLibrary from '@example/library'
jest.spyOn(exampleLibrary, 'targetFunction').mockReturnValue({ key: 42 })
Be aware that attempting this might sometimes result in a TypeError: Cannot redefine property: targetFunction due to Jest’s handling of property definition. If you encounter this, it means spyOn cannot be used directly to mock this particular function. You’ll need to employ an alternate strategy for mocking in such scenarios
Also published on Medium: Javascript in Plain English
Keep reading

What fired my useEffect?
Developing in React brings its set of challenges, especially when dealing with side effects in functional components. React’s useEffect hook is a powerful tool but it can sometimes be a mystery figuring out which dependency change triggered a particular effect

When to use useCallback vs useMemo
In the expansive ecosystem of React, hooks play a pivotal role in crafting functional components and managing their state and side effects. Among these hooks, useCallback and useMemo stand out for their ability to optimize performance, albeit serving distinct purposes.
