I tried all the possibilities to mock location.pathname This is my code

if(typeof location !== undefined) {

This is one of the possibility I used to cover the location as undefined

it('should gets the parentPath without location', async () => {
        const locationSpy = jest.spyOn(location);
        locationSpy.mockImplementation(() => undefined);
        await asyncMount(
            <Provider UsersStore={UsersStore} AppStore={AppStore}>
                <Users />


It's giving me this error

Error: Cannot spy the undefined property because it is not a function; undefined given instead
at ModuleMockerClass.spyOn (/Users/arpit/Documents/prakhar/new/developer-portal/node_modules/jest-mock/build/index.js:789:15)
at Object.<anonymous> (/Users/arpit/Documents/prakhar/new/developer-portal/src/components/users/Users.spec.jsx:190:38)
at Object.asyncJestTest (/Users/arpit/Documents/prakhar/new/developer-portal/node_modules/jest-jasmine2/build/jasmineAsyncInstall.js:100:37)
at /Users/arpit/Documents/prakhar/new/developer-portal/node_modules/jest-jasmine2/build/queueRunner.js:47:12
at new Promise (<anonymous>)
at mapper (/Users/arpit/Documents/prakhar/new/developer-portal/node_modules/jest-jasmine2/build/queueRunner.js:30:19)
at /Users/arpit/Documents/prakhar/new/developer-portal/node_modules/jest-jasmine2/build/queueRunner.js:77:41
at processTicksAndRejections (internal/process/task_queues.js:97:5)

Just focusing on location or window elements for this question though.. Please suggest any solution specific to this only not for any MemoryRouter Or any other react-router component

  • Ya that i know that it's not making any sense to use in that way... just using location.pathname.. React gives you that functionality to use these directly Jun 25, 2020 at 17:08
  • Cannot use MemoryRouter though as per my usecase. Jun 25, 2020 at 17:13
  • I have to use window or using location for now.. Jun 25, 2020 at 17:16
  • The question was asked specifically for location.pathname... Jun 25, 2020 at 17:45
  • The question is crystal and clear @jonrshape voting it down doesn't make any sense Jun 25, 2020 at 17:47


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.