-
SummaryI ran through the Jest Testing Setup for NextJS and all looks fine. I setup a very simple test and ran into an error with ECMAScript Modules using Lucid React. The only way I found to start fixing it was to add lucid-react to I also tried to match a similar jestConfig to this discussion, but continue to receive the same error. Is there anything I can do to streamline this to work? Or is the only way to one by one add them to transpilePackages? Here is my import type { Config } from 'jest'
import nextJest from 'next/jest.js'
const createJestConfig = nextJest({
// Provide the path to your Next.js app to load next.config.js and .env files in your test environment
dir: './',
})
// Add any custom config to be passed to Jest
const config: Config = {
coverageProvider: 'v8',
testEnvironment: 'jsdom',
// Add more setup options before each test is run
setupFilesAfterEnv: ['<rootDir>/jest.setup.ts'],
moduleNameMapper: {
// ...
'^@/(.*)$': '<rootDir>/src/$1',
},
transformIgnorePatterns: [
'/node_modules/(?!lucide-react/)',
],
}
// createJestConfig is exported this way to ensure that next/jest can load the Next.js config which is async
export default createJestConfig(config) Additional informationThe error I received was: 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:
node_modules\lucide-react\dist\esm\lucide-react.js:8
import * as index from './icons/index.js';
^^^^^^
SyntaxError: Cannot use import statement outside a module |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 1 reply
-
Hi, there |
Beta Was this translation helpful? Give feedback.
-
I suggest using swc/jest; https://swc.rs/docs/usage/jest |
Beta Was this translation helpful? Give feedback.
-
Which version of lucide-react are you using? I was on |
Beta Was this translation helpful? Give feedback.
Which version of lucide-react are you using? I was on
^0.475.0
and bumping toˆ0.487.0
worked