lucide-icons / lucide

Beautiful & consistent icon toolkit made by the community. Open-source project and a fork of Feather Icons.
https://lucide.dev
ISC License
10.8k stars 493 forks source link

JEST: => export { dynamicIconImports as default }; SyntaxError: Unexpected token 'export' #2366

Open melrose13-69 opened 2 months ago

melrose13-69 commented 2 months ago

Package

Version

^0.427.0

Can you reproduce this in the latest version?

Browser

Operating system

Description

Jest Config:

const displayName = 'displayName'

export default {
 displayName,
  roots: ['<rootDir>'],
  preset: 'ts-jest',
  modulePaths: [baseUrl],
  moduleNameMapper: {
    ...(paths &&
      pathsToModuleNameMapper(paths, {
        prefix: '<rootDir>'
      })),
    '^.+\\.(css)$': 'jest-css-modules-transform',
    '\\.(jpg|jpeg|png|gif|webp|svg)$': 'jest-transform-file'
  },
  reporters: [
    'default',
    [
      'jest-junit',
      {
        outputDirectory: '../../coverage',
        outputName: `junit-${displayName}.xml`
      }
    ]
  ],
  transform: {
    '\\.[jt]sx?$': [
      'ts-jest',
      { plugins: ['@emotion/babel-plugin'], tsconfig: 'tsconfig.spec.json' }
    ]
  },
  moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx'],
  coverageDirectory: `../../coverage/${displayName}`,
  collectCoverage: true,
  coverageReporters: ['text', 'json'],
  testEnvironment: 'jsdom',
  passWithNoTests: true
}

Component

'use client'
import type { IconProps } from '@/components/media/Icon/types'

import { createStyledIcon } from '@/components/media/Icon/styled'
import dynamicIconImports from 'lucide-react/dynamicIconImports'
import dynamic from 'next/dynamic'

export const Icon = ({
  icon,
  className,
  spin = false,
  transition = false,
  size = 16,
  rotation = 0
}: IconProps) => {
  if (!icon) return <></>

  const IconComponent = dynamic(dynamicIconImports[icon])

  const LucideIcon = createStyledIcon(IconComponent)

  return (
    <LucideIcon
      size={size}
      className={className}
      data-testid={`icon-${icon}`}
      $rotation={rotation}
      $spin={spin}
      $transition={transition}
    />
  )
}

Terminal output

Test suite failed to run

Jest encountered an unexpected token

Jest failed to parse a file. This happens e.g. when your code or its dependencies use non-standard JavaScript syntax, or when Jest is not configured to support such syntax.

Out of the box Jest supports Babel, which will be used to transform your files into valid JS based on your Babel configuration.

By default "node_modules" folder is ignored by transformers.

Here's what you can do:
 • If you are trying to use ECMAScript Modules, see https://jestjs.io/docs/ecmascript-modules for how to enable it.
 • If you are trying to use TypeScript, see https://jestjs.io/docs/getting-started#using-typescript
 • To have some of your "node_modules" files transformed, you can specify a custom "transformIgnorePatterns" in your config.
 • If you need a custom transformation specify a "transform" option in your config.
 • If you simply want to mock your non-JS modules (e.g. binary assets) you can stub them out with the "moduleNameMapper" config option.

You'll find more details and examples of these config options in the docs:
https://jestjs.io/docs/configuration
For information about custom transformations, see:
https://jestjs.io/docs/code-transformation

Details:

/Users/poltoratchi/work/hdsk/frontend/node_modules/.pnpm/lucide-react@0.427.0_react@18.2.0/node_modules/lucide-react/dynamicIconImports.js:1531
export { dynamicIconImports as default };
^^^^^^

SyntaxError: Unexpected token 'export'

  3 |
  4 | import { createStyledIcon } from '@/components/media/Icon/styled'
> 5 | import dynamicIconImports from 'lucide-react/dynamicIconImports'
    | ^
  6 | import dynamic from 'next/dynamic'
  7 |
  8 | export const Icon = ({

Steps to reproduce

  1. Created basic Icon component from lucide-react
  2. Use them in my project
  3. Run jest tests

Checklist

MarcosSantosDev commented 2 weeks ago

This solution works for me:

{
  ...,
  // A map from regular expressions to paths to transformers
  transform: {
    '^.+\\.(ts|tsx)?$': 'ts-jest',
    '^.+\\.(js|jsx)$': 'babel-jest',
  },
  // An array of regexp pattern strings that are matched against all source file paths, matched files will skip transformation
  transformIgnorePatterns: ['node_modules/(?!(lucide-react)/)'],
}
ericfennis commented 2 weeks ago

Possibly fixed by #2437