Jest testing failing for files under node modules .d.ts and unable to parse even after ignoring node modules

I am getting Below error on the while doing the jest testing. I have done transform ignore for node modules to which the error are being posted. Please review the configuration and help me compile my test case

Tried both moduleNameMapper and transformIgnorePatterns with no luck
Added babel configuration as well

Error:


    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:

    C:\projectName\node_modules\@ms\centr-loader\dist\loader\index.js:7
    export { initialize, preloadFeature, bootstrapFeature } from './foreignEnvLoader';
    ^^^^^^

    SyntaxError: Unexpected token 'export'

      2 | import { Stack, DetailsListLayoutMode, StackItem } from '@fluentui/react';
      3 | import { Dropdown } from '@fluentui/react/lib/Dropdown';
    > 4 | import { DataGrid, Loading } from '@fluentui/react';
        | ^

      at Runtime.createScriptFromCode (../node_modules/jest-runtime/build/index.js:1505:14)

jest.config.ts

// Jest.config.ts
import type { Config } from 'jest';

const config: Config = {
  testEnvironment: 'jsdom',
  setupFilesAfterEnv: ['<rootDir>/support/setupTests.ts'],
  moduleFileExtensions: ["js", "jsx", "tsx", "ts", "mjs", "mjx", "json", "node"],
  preset: 'ts-jest',
  moduleNameMapper: {
    '\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga|mjs|ms|mjx)$': '<rootDir>/__mocks__/fileMock.js',
    '\\.(css|less)$': 'identity-obj-proxy'
  },
  testRegex: "(/__tests__/.*|(\\.|/)(test|spec))\\.(jsx?|tsx?|mjs?|mjx?)$",
  transform: {
    '^.+\\.(ts|tsx)$': 'ts-jest',
    '^.+\\.(js|jsx|mjs|ms|mts)$': 'babel-jest',
    "^.+\\.m?jsx?$": "babel-jest"
  },
  transformIgnorePatterns: [
    '!<rootDir>/node_modules/'
  ],
};

export default config;

tsconfig.json

{
  "compilerOptions": {
    "target": "esnext",
    "useDefineForClassFields": true,
    "lib": [
      "DOM",
      "DOM.Iterable",
      "esnext"
    ],
    "allowJs": false,
    "skipLibCheck": true,
    "esModuleInterop": true,
    "module": "commonjs",
    "allowSyntheticDefaultImports": true,
    "strict": true,
    "forceConsistentCasingInFileNames": true,
    "moduleResolution": "node",
    "resolveJsonModule": true,
    "isolatedModules": false,
    "noEmit": true,
    "jsx": "react",
    "types": [
      "jest"
    ]
  },
  "include": [
    "src",
    "test"
  ],
  "references": [
    {
      "path": "./tsconfig.node.json"
    }
  ]
}

.babelrc

{
    "env": {
        "test": {
            "presets": [
                [
                    "@babel/preset-env",
                    {
                        "modules": false
                    }
                ]
            ],
            "plugins": [
                [
                    "@babel/plugin-transform-modules-commonjs",
                    {
                        "spec": true
                    }
                ]
            ]
        }
    }
}

Try to modify transformIgnorePatterns to this

const transformedModules = [
  '@fluentui/react'
].join('|');

const config: Config = {
  ...,
  transformIgnorePatterns: [`node_modules/(?!(${transformedModules})/)`],
};

Anytime this error pops up for a specific node module, add its name to the transformedModules array

Leave a Comment