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

使用Jest+React Redux遇模块找不到错误,求协助排查测试套件失败问题

Fixing "Cannot find module 'store/actions/user' from 'login-form.js'" in Jest + React Redux Tests

Hey Carlos, let's work through this module not found error you're facing when running your Jest tests with React Redux. This is a super common issue, usually tied to how Jest resolves module paths compared to your development environment. Here are the most reliable fixes to try:

1. Double-Check Your Import Path (Relative vs. Absolute)

First, confirm the import statement in login-form.js is pointing to the correct location. If you're using an absolute path like import { userActions } from 'store/actions/user', your dev server (like Webpack) might be configured to resolve this, but Jest doesn't recognize it by default.

If you haven't set up path aliases, switch to a relative path instead. For example, if login-form.js is in src/components/auth/, the correct relative import would be:

import { userActions } from '../../store/actions/user';

2. Configure Jest's moduleNameMapper for Path Aliases

If you intentionally use absolute path aliases (e.g., mapping store/ to src/store/ in Webpack or Babel), you need to tell Jest about this alias in your jest.config.js file. Add the following to your configuration:

module.exports = {
  // ... other Jest settings (testEnvironment, transform, etc.)
  moduleNameMapper: {
    // Map any import starting with 'store/' to the actual path in your project
    '^store/(.*)$': '<rootDir>/src/store/$1'
  }
};
  • <rootDir> is a built-in Jest variable that points to your project's root folder (where package.json lives). Adjust the path after <rootDir> to match your project's folder structure if needed.

3. Verify File Existence & Case Sensitivity

It might sound obvious, but double-check:

  • Does the file src/store/actions/user.js (or src/store/actions/user/index.js if you're using index exports) actually exist?
  • Are you using the correct case in your import? macOS/Linux and Jest's module resolver are case-sensitive, so User.js vs user.js will cause an error even if Windows doesn't care.

4. Sync Babel Path Aliases with Jest

If you're using babel-plugin-module-resolver to handle path aliases in your Babel config, make sure Jest is aware of it. Update your babel.config.js (or .babelrc) to include the alias:

module.exports = {
  presets: ['@babel/preset-react', '@babel/preset-env'],
  plugins: [
    ['module-resolver', {
      alias: {
        store: './src/store'
      }
    }]
  ]
};

Also, ensure Jest is using Babel to transform your JS/JSX files by adding this to jest.config.js:

transform: {
  '^.+\\.(js|jsx)$': 'babel-jest'
}

5. Clear Jest's Cache

Sometimes cached module resolutions can cause weird, persistent errors. Run this command to clear Jest's cache, then re-run your tests:

jest --clearCache

If none of these fixes work, feel free to share the exact import line from login-form.js and your project's folder structure—I can help narrow it down further!

内容的提问来源于stack exchange,提问作者Carlos Vieira

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.19 09:26:48