Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot read property 'preload' of undefined when testing with config testEnvironment: 'node' at jest #38

Open
smhorta opened this issue Nov 16, 2021 · 0 comments

Comments

@smhorta
Copy link

smhorta commented Nov 16, 2021

We are trying to set some SSR tests for our Next.js app to make sure that our server side rendering is working as expected. Some of the requirements of this is that the window object or the document object should be undefined in our environment.

To achieve this we have set our jest test environment to node at the config file jest.config.js:

module.exports = {
   testEnvironment: 'node',
}

We have also set some dynamic imports at our app with the option { ssr: false }, as we want them to be rendered on the client side:

 const DynamicCustomComponent = dynamic(
  () => import('../DynamicCustomComponent'),
  { ssr: false }
);

When having all the above specs we run into the following issue:

 TypeError: Cannot read property 'preload' of undefined

      35 | 
      36 | beforeAll(async () => {
    > 37 |   await preloadAll();
         |         ^
      38 | });
      39 | 
      40 | describe('SSR - Custom Page', () => {

      at node_modules/jest-next-dynamic/index.js:40:38
      at node_modules/jest-next-dynamic/index.js:12:52
          at Array.map (<anonymous>)
      at preloadAll (node_modules/jest-next-dynamic/index.js:12:37)

As far as I have researched, whats going on is that in a jest node environment is that we are trying to preload a SSR false dynamic component, which will be undefined as it should only be render in a client. So in conclusion LoadableComponent at line 35 at the file https://github.com/FormidableLabs/jest-next-dynamic/blob/master/index.js is undefined.

LoadableComponent.preload
          ? LoadableComponent.preload()
          : LoadableComponent.render.preload();

Is there a way around to fix this? If we set the jest env too jsdom works but we NEED to set a node jest env to simulate a Server Side Rendering test, so that solution does not work for us.

I have tried locally the following change for lines 35, 36 and 37 at file https://github.com/FormidableLabs/jest-next-dynamic/blob/master/index.js that worked:

if(LoadableComponent.preload) {
          LoadableComponent.preload();
        } else if(LoadableComponent.render)  {
          LoadableComponent.render.preload();
        }

Is this a possible fix for the library?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant