Jest tests fail when using a custom image loader in Next.js 16.2.
The issue involves Jest tests failing when using a custom image loader in Next.js 16.2, despite the loader being correctly configured in `next.config.js`. The problem appears to be specific to the testing environment and requires understanding both Jest and Next.js's image handling mechanisms. The issue started in version 16.2 and was not present in 16.1.
https://github.com/guisehn/next-16.2-jest-bug-mre
Configure your next.config.js with a custom loader by using loader and loaderFile (docs)
Use npm run test to run a Jest test that renders a next/image
It will crash with:
Image with src "/images/test.jpg" is missing "loader" prop.
Read more: https://nextjs.org/docs/messages/next-image-missing-loader
Currently: crashing with error Image with src "/images/test.jpg" is missing "loader" prop.
Expected: not crash, because loaderFile was supplied in next.config.js.
This error should only happen when using loader = custom but not having a loaderFile. In that case, passing a loader prop to next/image is required.
Operating System:
Platform: darwin
Arch: arm64
Version: Darwin Kernel Version 23.3.0: Wed Dec 20 21:31:00 PST 2023; root:xnu-10002.81.5~7/RELEASE_ARM64_T6020
Available memory (MB): 32768
Available CPU cores: 12
Binaries:
Node: 22.1.0
npm: 10.7.0
Yarn: 1.22.22
pnpm: 10.16.1
Relevant Packages:
next: 16.2.1 // Latest available version is detected (16.2.1).
eslint-config-next: N/A
react: 18.3.1
react-dom: 18.3.1
typescript: 5.3.3
Next.js Config:
output: N/A
Testing
next dev (local)
Issue started happening on Next.js 16.2. Tested and it works fine on 16.1.
Claim this issue to let others know you're working on it. You'll earn 20 points when you complete it!