nextjs - ✅(Solved) Fix 16.2 Jest tests fail when using a custom image loader [2 pull requests, 1 participants]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…
GitHub stats
vercel/next.js#91876Fetched 2026-04-08 01:25:54
View on GitHub
Comments
0
Participants
1
Timeline
7
Reactions
0
Author
Participants
Timeline (top)
cross-referenced ×2referenced ×2issue_type_added ×1labeled ×1

Error Message

Currently: crashing with error Image with src "/images/test.jpg" is missing "loader" prop. 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.

Root Cause

Expected: not crash, because loaderFile was supplied in next.config.js.

Fix Action

Fixed

PR fix notes

PR #91882: fix(jest): include loaderFile in imageConfig and moduleNameMapper for custom image loaders

Description (problem / solution / changelog)

What?

When next.config.js configures a custom image loader via images.loaderFile, Jest tests fail with:

Image with src "/images/test.jpg" is missing "loader" prop.

Why?

The imageConfig object built in packages/next/src/build/jest/jest.ts (added in #89536) includes loader but not loaderFile. This causes two problems:

  1. process.env.__NEXT_IMAGE_OPTS (injected by the SWC transformer) does not contain loaderFile, so the image config is incomplete.
  2. The moduleNameMapper does not redirect next/dist/shared/lib/image-loader to the custom file, unlike what webpack does via create-compiler-aliases.ts in production.

When config.loader === 'custom' but no custom loader module is wired up, get-img-props.ts throws the "missing loader prop" error.

How?

  1. Added loaderFile to the imageConfig object so it is included in __NEXT_IMAGE_OPTS.
  2. Added moduleNameMapper entries for next/dist/shared/lib/image-loader and next/dist/esm/shared/lib/image-loader pointing to nextConfig.images.loaderFile when set — mirroring what create-compiler-aliases.ts does for webpack.

Fixes #91876

Changed files

  • packages/next/src/build/jest/jest.ts (modified, +12/-0)

PR #91884: fix(next/jest): respect images.loaderFile config in Jest tests

Description (problem / solution / changelog)

Summary

Fixes #91876

When images.loaderFile is configured in next.config.js, webpack and Turbopack alias the default image loader (next/dist/shared/lib/image-loader) to the user's custom loader file at build time. However, the next/jest configuration did not set up a corresponding moduleNameMapper entry, so the default image loader (which carries __next_img_default = true) was always imported during Jest tests.

Combined with the image config reporting loader: 'custom', this caused next/image to throw:

Image with src "..." is missing "loader" prop.

Changes

  • packages/next/src/build/jest/jest.ts: Added a moduleNameMapper entry that aliases next/dist/shared/lib/image-loader to the user's loaderFile when configured, mirroring the webpack/turbopack alias in create-compiler-aliases.ts.
  • test/production/app-dir/image-jest-custom-loader/: Added a test that verifies next/image works correctly in Jest when a custom loaderFile is configured.

How it works

The root cause is that get-img-props.ts checks '__next_img_default' in loader (line 352) alongside config.loader === 'custom' (line 355). During builds, the loaderFile alias replaces the default loader import with the user's custom loader (which lacks the __next_img_default marker), so the check passes. In Jest, this alias was missing, so the default loader was always used while the config still said loader: 'custom', triggering the error.

Test plan

  • New test test/production/app-dir/image-jest-custom-loader/image-jest-custom-loader.test.ts validates that next/image renders successfully in a Jest environment with a custom loaderFile configured
  • Existing test test/production/app-dir/image-jest-qualities/ continues to pass (no regression for non-custom-loader setups)

Changed files

  • packages/next/src/build/jest/jest.ts (modified, +10/-0)
  • test/production/app-dir/image-jest-custom-loader/app/app/page.tsx (added, +8/-0)
  • test/production/app-dir/image-jest-custom-loader/image-jest-custom-loader.test.ts (added, +105/-0)

Code Example

Image with src "/images/test.jpg" is missing "loader" prop.
Read more: https://nextjs.org/docs/messages/next-image-missing-loader

---

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
RAW_BUFFERClick to expand / collapse

Link to the code that reproduces this issue

https://github.com/guisehn/next-16.2-jest-bug-mre

To Reproduce

  1. Configure your next.config.js with a custom loader by using loader and loaderFile (docs)

  2. Use npm run test to run a Jest test that renders a next/image

  3. 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

Current vs. Expected behavior

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.

Provide environment information

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

Which area(s) are affected? (Select all that apply)

Testing

Which stage(s) are affected? (Select all that apply)

next dev (local)

Additional context

Issue started happening on Next.js 16.2. Tested and it works fine on 16.1.

extent analysis

Fix Plan

To resolve the issue, you need to update your next.config.js to correctly configure the custom loader for next/image.

Here are the steps:

  • Update next.config.js to include the loader and loaderFile properties.
  • Ensure the loaderFile is correctly referenced and exported.

Example next.config.js:

module.exports = {
  //... other configurations ...
  images: {
    loader: 'custom',
    loaderFile: './loader.js',
  },
}

In your loader.js file, ensure you export the custom loader configuration:

export default {
  loader: 'custom',
  //... other loader configurations ...
}

Additionally, verify that your Jest test is correctly configured to use the next/image component with the custom loader.

Verification

To verify the fix, run your Jest test again using npm run test. The test should no longer crash with the Image with src "/images/test.jpg" is missing "loader" prop error.

Extra Tips

  • Ensure your custom loader file is correctly referenced in next.config.js.
  • Verify that your Jest test is using the correct next/image component configuration.
  • If issues persist, try downgrading to Next.js 16.1 to isolate the issue.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING