You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Jest测试Next.js dynamic组件时报“Not supported”错误求助

Troubleshooting "Not supported" Error with next/dynamic in Jest Tests

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-dynamic version 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/jest preset in your jest.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/tsx files and uses babel-jest with the Next.js preset. Avoid excluding next/dynamic from 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:
    const Rating = dynamic(import('components/Rating'));
    
    Use:
    const Rating = dynamic(() => import('components/Rating'));
    
    (The arrow function is required for jest-next-dynamic to properly preload the component.)
  • If your Rating component relies on browser-specific APIs (like window or document), these might throw errors during preloading. Add mocks for these APIs in your test setup (e.g., in jest.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 beforeAll is declared outside any describe blocks (unless you intentionally want it scoped to a test suite).
  • Add a console log inside the beforeAll to confirm it runs:
    beforeAll(async () => {
      console.log('Preloading dynamic components...');
      await preloadAll();
      console.log('Preloading complete');
    });
    
    If you don’t see these logs, your test runner might be skipping the block (unlikely, but worth verifying).

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.js includes 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 client components) might require additional mocking or configuration. Ensure jest-next-dynamic is set up to handle client-side components.

内容的提问来源于stack exchange,提问作者Luis Felipe Perez

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.13 08:54:51