FormidableLabs / jest-next-dynamic

Resolve Next.js dynamic import components in Jest tests
MIT License
69 stars 7 forks source link

Cannot read property 'preload' of undefined #44

Open arishoham opened 1 year ago

arishoham commented 1 year ago

With nextjs v13.1.7-canary.3, the 'preload' function no longer exists on next/dynamic components with {ssr: false}, causing an error Cannot read property 'preload' of undefined

aitorllj93 commented 1 year ago

Setting ssr to true also doesn't seem to be working as expected as the preloaded component still required to wrap the tests inside act/waitFor.

Although not a really clean solution, this can work as a temporary patch (inside the createFactory function):

let c = () => null;

const promiseLoader = loader().then((comp) => {
  c = typeof comp === 'function' ? comp : comp.default || (() => null);
});

mockInitializers.push(() => promiseLoader);

return (props) => c(props);