Skip to content

[Bug]: Jest transform regex for matching specific modules is not working #15728

@shidunce

Description

@shidunce

Version

26.6.3

Steps to reproduce

Guys, hello.

I am working on importing @carbon/ai-chat package, so it will be available for testing with Jest in my React app.
I have following Jest configuration:

moduleNameMapper: {
                ...moduleNameMapper,
                '^@carbon/ai-chat$':
                    '<rootDir>/node_modules/@carbon/ai-chat/dist/es/aiChatEntry.js',
                '^d3$': '<rootDir>node_modules/d3/dist/d3.js',
            },
            transformIgnorePatterns: [
                '<rootDir>/node_modules/(?!(d3|@?lit.*|@carbon/.+)/)',
            ],
            transform: {
                '?':
                    [
                        'babel-jest',
                        {
                            presets: [
                                ['@babel/preset-env', { modules: 'commonjs' }],
                                '@babel/preset-react',
                            ],
                        },
                    ],
                ...existingProjectTransform,
            },

I excluded some packages from node_modules and now I want them to be transformed by babel-jest with custom setup, which is different from what I have in existingProjectTransform (transformation by 'ts-jest').

The problem is that I can not provide regex like

 '^.*node_modules/(d3|@?lit.*|@carbon/.+).*\\.[tj]sx?$' 

I validated this regex at https://regex101.com/ tool, which you offer in your documentation.
I hoped to have it working, so only what is matched by this pattern will be transformed by by 'babel-jest'.

Expected behavior

Modules get transpiled and unit tests can be run.

Actual behavior

After having such regex in place I am trying to run tests and immediately (without waiting a sec) I see following error comes up:

Validation Error:

  Module babel-jest in the transform option was not found.
         <rootDir> is: /home/deniss/git/Turbonomic/ux-app

  Configuration Documentation:
  https://jestjs.io/docs/configuration.html

which is not even related to regex itself!

Tried it on latest version of Jest, got same error.

Additional context

What kid of regex is working for me?

For example

^((?!(myApp/notNeededDir/|myApp/notNeededDir1/|myApp/dev/|myApp/shared/|myApp/(?!(node_modules/(d3|@?lit.*|@carbon/.+))))).)*\\.[tj]sx?$

In other words instead of having regex that says what to take, I have one that says what not to take.

Is there any trick to make it work on existing 26.6.3 version?

Environment

Windows
"node": "22.11.0",
"yarn": ">=1.22.0",

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions