使用Jest测试Next.js dynamic组件时报“Not supported”错误求助
Got it, let’s break down why you might still be hitting this error even after setting up jest-next-dynamic and running preloadAll(). Here are the most common fixes to try:
1. Verify Version Compatibility Between jest-next-dynamic and Next.js
First off, version mismatches are a frequent culprit. jest-next-dynamic needs to align with your Next.js version—especially if you’re using Next.js 13+ (either Pages or App Router).
- If you’re on Next.js 13+, make sure you’re using the latest version of
jest-next-dynamic(check npm for updates). Older versions might not support the newer dynamic import handling in Next.js. - If you’re on an older Next.js version (like 12 or below), double-check that your
jest-next-dynamicversion is compatible with it (some recent releases dropped support for older Next.js versions).
2. Fix Your Jest Configuration
Your jest setup might not be properly transforming Next.js modules, including next/dynamic. Here’s what to check:
- Ensure you’re using the
next/jestpreset in yourjest.config.js—this handles most Next.js-specific transformations out of the box:module.exports = { preset: 'next/jest', testEnvironment: 'jsdom', // Other config options... }; - If you have a custom transform rule, make sure it includes
jsx/tsxfiles and usesbabel-jestwith the Next.js preset. Avoid excludingnext/dynamicfrom transformation.
3. Check Your Dynamic Import Syntax and Component Dependencies
Sometimes the issue isn’t with jest-next-dynamic itself, but with how you’re importing the component or the component’s own dependencies:
- Make sure your dynamic import uses the arrow function syntax correctly. Instead of:
Use:const Rating = dynamic(import('components/Rating'));
(The arrow function is required forconst Rating = dynamic(() => import('components/Rating'));jest-next-dynamicto properly preload the component.) - If your
Ratingcomponent relies on browser-specific APIs (likewindowordocument), these might throw errors during preloading. Add mocks for these APIs in your test setup (e.g., injest.setup.js):global.window = {}; global.document = { createElement: () => ({}) };
4. Confirm preloadAll() Runs Before All Tests
It might sound obvious, but double-check that your beforeAll block is actually executing before your test cases.
- Ensure the
beforeAllis declared outside anydescribeblocks (unless you intentionally want it scoped to a test suite). - Add a console log inside the
beforeAllto confirm it runs:
If you don’t see these logs, your test runner might be skipping the block (unlikely, but worth verifying).beforeAll(async () => { console.log('Preloading dynamic components...'); await preloadAll(); console.log('Preloading complete'); });
5. App Router-Specific Adjustments
If you’re using Next.js 13’s App Router (the app/ directory), you might need extra setup:
- Make sure your
jest.config.jsincludes the App Router in its module name mapper if needed:module.exports = { preset: 'next/jest', moduleNameMapper: { '^@/components/(.*)$': '<rootDir>/app/components/$1', }, }; - Some App Router-specific components (like
use clientcomponents) might require additional mocking or configuration. Ensurejest-next-dynamicis set up to handle client-side components.
内容的提问来源于stack exchange,提问作者Luis Felipe Perez

