scottrippey / next-router-mock

Mock implementation of the Next.js Router
MIT License
401 stars 38 forks source link

Next 13.4.20-canary.20 issue #104

Closed rgembalik closed 1 year ago

rgembalik commented 1 year ago

Any idea how to make the next-router-module work with the next@13.4.20-canary.20 or later?

I'm getting this error:

Cannot find module 'next/dist/shared/lib/router-context' from 'node_modules/next-router-mock/dist/MemoryRouterProvider/next-11.js'

    Require stack:
      node_modules/next-router-mock/dist/MemoryRouterProvider/next-11.js
      node_modules/next-router-mock/dist/MemoryRouterProvider/next-13.js
      node_modules/next-router-mock/dist/MemoryRouterProvider/index.js
      src/lib/testHelpers.tsx
ra-lfaro commented 1 year ago

Yea, same issue with the latest 13.5.1

ra-lfaro commented 1 year ago

the router-context file it previously imported from the next/dist has been renamed to router-context.shared-runtime

ziishaned commented 1 year ago

After updating the import statement from import { RouterContext } from 'next/dist/shared/lib/router-context'; to import { RouterContext } from 'next/dist/shared/lib/router-context.shared-runtime'; fixed the issue for me.

Tankenstein commented 1 year ago

If you want to fix your tests while this gets fixed, I used this workaround in my global jest setup:

jest.mock(
  'next/dist/shared/lib/router-context',
  () => jest.requireActual('next/dist/shared/lib/router-context.shared-runtime'),
  { virtual: true },
);

Thanks for this great library and for working on support for the new next router. I'm amazed that by stacking workarounds it's still able to work.

alexteg commented 1 year ago

I had the same issue when upgrading to Next.js 13.5.1. Another workaround is to add a module mapping to the jest.config:

    moduleNameMapper: {
        'next/dist/shared/lib/router-context':
            'next/dist/shared/lib/router-context.shared-runtime',
    },

As @ra-lfaro mentioned, the source of the issue is the renaming of the file next/dist/shared/lib/router-context in this commit in Next.js: https://github.com/vercel/next.js/commit/5217e7eb0698947a95a2d6410f4a36c482be406a#diff-f7b79c420a8c291fefaa78163620fa1abaad67a98138fbea666b9d25d95a2331

WPaczula commented 1 year ago

I was trying to do the same using vitest, but there is neither moduleNameMapper nor {virtual: true} for the vi.mock. I checked the alias in vitest.config but with no luck. Any help here would be super appreciated 🙏

jantimon commented 1 year ago

@scottrippey - now that next 13.5 was released - would you be open for a PR which changes the import here:

https://github.com/scottrippey/next-router-mock/blob/5662b44b13ca594aba2382c3a419c79359d21fff/src/MemoryRouterProvider/next-11.tsx#L3

and here:

https://github.com/scottrippey/next-router-mock/blob/5662b44b13ca594aba2382c3a419c79359d21fff/src/MemoryRouterProvider/MemoryRouterProvider.tsx#L7-L8

See #105

scottrippey commented 1 year ago

I will get that PR merged soon!

FWIW, this has been a problem before, and I've tried working with Vercel to improve the testability. I've tried using my internal contacts with their org, and still made no progress. Please upvote this discussion if you'd like to help bring attention to this annoying issue! https://github.com/vercel/next.js/discussions/38248