41

I have spent a long time looking at other questions about this and looking at other projects on Github but none of the answers seem to work for me.

I am loading a third party library in my project, and when running Jest tests I get the error

export default portalCommunication;
^^^^^^

SyntaxError: Unexpected token export

> 1 | import portalCommunication from 'mathletics-portal-communication-service';

I have tried updating my Jest config in many ways to get it to transpile this library but I always get the same error.

This is my current jest.config.js file:

module.exports = {
    moduleNameMapper: {
        '\\.(css|scss)$': 'identity-obj-proxy',
        '\\.svg$': '<rootDir>/test/mocks/svg-mock.js'
    },
    setupFiles: ['./test/test-setup.js'],
    transformIgnorePatterns: [
        '<rootDir>/node_modules/(?!mathletics-portal-communication-service)'
    ]
};

I have also tried adding the transform property to run babel-jest against this mathletics-portal-communication-service directory.

Please help!

Heather Roberts
  • 1,978
  • 1
  • 24
  • 33

5 Answers5

55

The transformIgnorePatterns didn't work for me until I changed my .babelrc to babel.config.js, like this:

module.exports = {
    "presets": [
        "@babel/preset-env"
    ]
};

as seen on this comment: https://github.com/facebook/jest/issues/6229#issuecomment-403539460

Penny Liu
  • 15,447
  • 5
  • 79
  • 98
NJCodeMonkey
  • 811
  • 7
  • 8
9

Another team at my org added some common module. This was not transpiled and so the issue.

I followed this: https://babeljs.io/docs/en/configuration#whats-your-use-case

  1. Converted my .babelrc to babel.config.json
  2. in jest config added this: transformIgnorePatterns: ['/node_modules/(?!(@companyName)/).*/'],

This solved my problem.

Rohan Bagchi
  • 649
  • 10
  • 17
6

As a workaround for now I have changed my config to use the moduleNameMapper option to load a mock class for that library instead. I would have preferred to use transformIgnorePatterns instead so would still appreciate any ideas.

New config:

module.exports = {
    moduleNameMapper: {
        '\\.(css|scss)$': 'identity-obj-proxy',
        '\\.svg$': '<rootDir>/test/mocks/svg-mock.js',
        'mathletics-portal-communication-service': '<rootDir>/test/mocks/mathletics-portal-communication-service-mock.js'
    },
    setupFiles: ['./test/test-setup.js']
};
Heather Roberts
  • 1,978
  • 1
  • 24
  • 33
5

Adding trailing slash fix this for me:

{
    // ...
    transformIgnorePatterns: [
        '<rootDir>/node_modules/(?!mathletics-portal-communication-service/)'
    ]
};
Raz Luvaton
  • 3,166
  • 4
  • 21
  • 36
-4

It might be your use of rootDir. Try:

    "transformIgnorePatterns": [
      "node_modules/(?!(mathletics-portal-communication-service))"
    ]
Yorkshireman
  • 2,194
  • 1
  • 21
  • 36