Jest gives an error: "SyntaxError: Unexpected token export" for node_modules\@microsoft\mgt-react

Viewed 1348

I am using jest to test my react App. While running the test for one component which uses node_modules@microsoft\mgt-react which is a typescript module, I am getting the below error.

 export * from './Mgt';
    ^^^^^^

    SyntaxError: Unexpected token 'export'

I tried adding transformIgnorePatterns, but it is not working.

my babel.config.js looks like this.

 module.exports = {
   presets: ["@babel/preset-env", "@babel/preset-react"],
   plugins: ["@babel/plugin-transform-react-jsx"],
 };
1 Answers

I just had the same problem. I solved it by stubbing the component I was using.

For example, I'm using the PeoplePicker in one of my files:

import { PeoplePicker as MgtPeoplePicker } from '@microsoft/mgt-react';

const PeoplePicker = () => {
  return (
    // something
  );
};

I created a file where I mock its implementation:

// __mocks__/mgt-react.js
export default class PeoplePicker {
    // purposely empty
}

Then, in my package.json I added the following:

{
  "jest": {
    "moduleNameMapper": {
      "@microsoft/mgt-react": "<rootDir>/__mocks__/mgt-react.js",
    }
  }
}

I tried adding it on my jest.config.js but it didn't work.

Related