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

"Error: Uncaught [TypeError: (0 , _reactdom.preload) is not a function]" when running jest #53272

Closed
1 task done
roryhen opened this issue Jul 27, 2023 · 7 comments · Fixed by #53443
Closed
1 task done
Labels
bug Issue was opened via the bug report template. Image (next/image) Related to Next.js Image Optimization. linear: next Confirmed issue that is tracked by the Next.js team. locked Testing Related to testing with Next.js.

Comments

@roryhen
Copy link

roryhen commented Jul 27, 2023

Verify canary release

  • I verified that the issue exists in the latest Next.js canary release

Provide environment information

Operating System:
      Platform: linux
      Arch: x64
      Version: #22 SMP Tue Jan 10 18:39:00 UTC 2023
    Binaries:
      Node: 18.14.2
      npm: 9.5.0
      Yarn: 1.22.19
      pnpm: N/A
    Relevant Packages:
      next: 13.4.13-canary.4
      eslint-config-next: 13.4.12
      react: 18.2.0
      react-dom: 18.2.0
      typescript: 5.0.4
    Next.js Config:
      output: N/A

Which area(s) of Next.js are affected? (leave empty if unsure)

Jest (next/jest)

Link to the code that reproduces this issue or a replay of the bug

https://codesandbox.io/p/sandbox/lively-monad-rr5hgl

To Reproduce

Run npm run test in a new terminal in the codesandbox repro provided

Describe the Bug

In jest tests where <Image /> from next/image is being used with priority={true} set, when I attempt to run jest it throws this error Error: Uncaught [TypeError: (0 , _reactdom.preload) is not a function].

It seems to be introduced in next@13.4.11, when I install next@13.4.10 the error goes away. It also prints The above error occurred in the <ImagePreload> component in the terminal so I tracked it down to this PR (#52425) where preload from react-dom is imported and used in that component which I suspect could be part of the issue.

Not sure of how to workaround the issue, but I'm open to suggestions.

Expected Behavior

I expect to be able to run jest tests using Image components without errors from the framework.

Which browser are you using? (if relevant)

No response

How are you deploying your application? (if relevant)

No response

NEXT-1482

@roryhen roryhen added the bug Issue was opened via the bug report template. label Jul 27, 2023
@github-actions github-actions bot added the Testing Related to testing with Next.js. label Jul 27, 2023
@balazsorban44 balazsorban44 added linear: next Confirmed issue that is tracked by the Next.js team. Image (next/image) Related to Next.js Image Optimization. labels Jul 28, 2023
@elliotgonzalez-lk
Copy link

Just updated to next@13.4.11. Same issue. Over 500 tests failing.

@ChrisCrossCrash
Copy link

I'm also having this problem when I run Cypress component testing on the following component:

const ImageBackground = (props: ImageBackgroundProps) => {
  const uploadUrl = process.env.NEXT_PUBLIC_UPLOAD_URL
  if (!uploadUrl) {
    throw new Error('NEXT_PUBLIC_UPLOAD_URL is not defined')
  }

  const imageMobileUrl = getFullUrl(props.imageMobile.url, uploadUrl)
  const imageDesktopUrl = getFullUrl(props.imageDesktop.url, uploadUrl)

  return (
    <>
      <Image
        src={imageMobileUrl}
        fill
        alt=''
        className={styles.imageBgMobile}
        data-cy='hero-bg-image-element'
        // Commenting out `priority` fixes the error:
        // (0 , _reactdom.preload) is not a function
        // However, it also disables preloading the image, which is not ideal.
        priority
      />
      <Image
        src={imageDesktopUrl}
        fill
        alt=''
        className={styles.imageBgDesktop}
        data-cy='hero-bg-image-element'
        quality={50}
        priority // Same as above comment.
      />
    </>
  )
}

I'm using Next version 13.4.12 and Cypress version 12.17.2.

@roryhen
Copy link
Author

roryhen commented Jul 28, 2023

As a temporary workaround, I ended up adding a mock in our jest.setup.js:

import React from 'react'

function MockImage(props) {
  return React.createElement('img', props)
}
jest.mock('next/image', () => MockImage)

Not perfect, but works for us for now.

@fuyu77
Copy link

fuyu77 commented Jul 29, 2023

import type * as ReactDom from 'react-dom';

jest.mock('react-dom', () => ({
  ...jest.requireActual<typeof ReactDom>('react-dom'),
  preload: jest.fn(),
}));

It also works for us by mocking react-dom.

@C4rl0s
Copy link

C4rl0s commented Aug 7, 2023

jest.mock('react-dom', () => ({
  ...jest.requireActual<typeof ReactDom>('react-dom'),
  preload: jest.fn(),
}));

This worked for me too :)

@kodiakhq kodiakhq bot closed this as completed in #53443 Aug 8, 2023
kodiakhq bot pushed a commit that referenced this issue Aug 8, 2023
…#53443)

### What?

`ReactDOM.preload` is available in `react-dom@experimental` builds. If it's not available, we should fall back to `Head`+`link`

### Why?

Since `ReactDOM.preload` is only available in `react-dom@experimental` builds, certain environments (like Jest or [Storybook](storybookjs/storybook#23661)) might have a version of `react-dom` installed that won't work with `preload()`

### How?

Closes NEXT-1482
Fixes #53272

See also: storybookjs/storybook#23661

Co-authored-by: Steven <229881+styfle@users.noreply.github.com>
@xyzarivera
Copy link

Bumped my nextjs to 13.4.13, the error still exists. Solutions above still work also.

@github-actions
Copy link
Contributor

This closed issue has been automatically locked because it had no new activity for 2 weeks. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 24, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Issue was opened via the bug report template. Image (next/image) Related to Next.js Image Optimization. linear: next Confirmed issue that is tracked by the Next.js team. locked Testing Related to testing with Next.js.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants