jestjs / jest

Delightful JavaScript Testing.
https://jestjs.io
MIT License
44.23k stars 6.46k forks source link

Error: Uncaught [TypeError: Cannot read properties of undefined (reading 'chain')][Bug]: #14992

Closed xieyezi closed 5 months ago

xieyezi commented 7 months ago

Version

29.7.0

Steps to reproduce

https://github.com/xieyezi/jest-example

  1. yarn install
  2. yarn test

Expected behavior

I don't know why Jest can't interpret the chain function of lodash. Is there something wrong with my configuration?

this is my jest.config.js:

/** @type {import('jest').Config} */
const config = {
  preset: 'ts-jest',
  verbose: true,
  testEnvironment: 'jsdom',
  modulePaths: ['<rootDir>'],
  roots: ['<rootDir>/src', '<rootDir>/tests'],
  testPathIgnorePatterns: ['/node_modules/'],
  moduleDirectories: ['node_modules', '<rootDir>/src'],
  transformIgnorePatterns: ['node_modules/(?!(lodash|query-string)/)'],
  setupFilesAfterEnv: ['@testing-library/jest-dom'],
  moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx'],
  moduleNameMapper: {
    '^@src/(.*)$': '<rootDir>/src/$1',
    '^@apis$': '<rootDir>/src/core/api/index',
    '\\.(css|less)$': 'identity-obj-proxy',
    '\\.(jpg|jpeg|png|gif)$': 'identity-obj-proxy',
    '^.+\\.svg$': 'jest-transformer-svg',
  },
  transform: {
    '^.+\\.[tj]sx?$': [
      'ts-jest',
      {
        isolatedModules: true,
        useESM: true,
      },
    ],
  },
};

module.exports = config;

this is my tsconfig.json:

{
  "compilerOptions": {
    "rootDir": "./",
    "baseUrl": "./",
    "sourceRoot": "/",
    "sourceMap": true,
    "strict": true,
    "noEmit": true,
    "alwaysStrict": true,
    "experimentalDecorators": true,
    "noImplicitReturns": false,
    "noImplicitThis": true,
    "strictNullChecks": false,
    "noUnusedLocals": false,
    "outDir": "build/dist",
    "inlineSources": true,
    "target": "ES2020",
    "useDefineForClassFields": true,
    "skipLibCheck": true,
    "esModuleInterop": false,
    "allowSyntheticDefaultImports": true,
    "module": "ESNext",
    "moduleResolution": "Node",
    "resolveJsonModule": true,
    "isolatedModules": true,
    "noImplicitAny": false,
    "strictPropertyInitialization": false,
    "jsx": "react-jsx",
    "skipDefaultLibCheck": true,
        "lib": [
      "ES2020",
      "DOM",
      "DOM.Iterable",
      "ESNext"
    ],
    "types": [
      "node",
      "jest",
      "@testing-library/jest-dom"
    ],
    "paths": {
      "@src/*": ["src/*"],
      "@apis": ["src/core/api/index"]
    }
  },
  "include": [
    "src/**/*.ts",
    "src/**/*.tsx",
    "tests/**/*.ts",
    "types.d.ts",
  ]
}

this is my babel.config.js:

/* eslint-disable prettier/prettier */
module.exports = {
  presets: [
    ['@babel/preset-env', { targets: { node: 'current' } }],
    '@babel/preset-react',
    '@babel/preset-typescript'
  ],
};

Actual behavior

run yarn test:

 FAIL  src/components/blueprint/subscribe/checkout/ModuleCheckoutPage/ModuleUpsellCards/index.test.tsx
  ModuleUpsellCards
    ✕ should render the component (53 ms)

  ● ModuleUpsellCards › should render the component

    TypeError: Cannot read properties of undefined (reading 'chain')

      22 |
      23 |   const upsellCards: ModuleUpsellCardUI[] = useMemo(() => {
    > 24 |     return _.chain(selectedModuleTypes)
         |              ^
      25 |       .map((moduleType) => UPSELL_MODULE_CARDS[moduleType] ?? [])
      26 |       .flatten()
      27 |       .uniqBy('moduleType')

Additional context

No response

Environment

System:
    OS: macOS 14.0
    CPU: (8) arm64 Apple M2
  Binaries:
    Node: 18.20.0 - ~/Library/Caches/fnm_multishells/56068_1711593776448/bin/node
    Yarn: 1.22.21 - ~/Library/Caches/fnm_multishells/47920_1711526061141/bin/yarn
    npm: 10.5.0 - ~/Library/Caches/fnm_multishells/56068_1711593776448/bin/npm
    pnpm: 8.11.0 - ~/Library/Caches/fnm_multishells/47920_1711526061141/bin/pnpm
  npmPackages:
    jest: ^29.7.0 => 29.7.0
github-actions[bot] commented 6 months ago

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 30 days.

github-actions[bot] commented 5 months ago

This issue was closed because it has been stalled for 30 days with no activity. Please open a new issue if the issue is still relevant, linking to this one.

github-actions[bot] commented 5 months ago

This issue was closed because it has been stalled for 30 days with no activity. Please open a new issue if the issue is still relevant, linking to this one.

github-actions[bot] commented 4 months ago

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.