相关文章推荐
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

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!

My project has babel set up and working fine, is there something special I would need to do for Jest? I thought it used babel-jest automatically? – Heather Roberts May 3, 2018 at 23:33

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

Do you know if a .babelrc or babel.config.js file is absolutely necessary? I'm trying to get mine working with just a transformIgnorePatterns declaration in my package.json – Yorkshireman Oct 23, 2019 at 16:46 Looking at the linked github issue, it seems like it won't work in the package.json either, but I haven't tried it. – NJCodeMonkey Oct 24, 2019 at 22:02 This response is underrated!! I had been bumping my head for several hours until I saw this! Heck this should be in the docs! – Samuel Imolorhe Jan 9, 2020 at 14:26

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

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

    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']
        // ...
        transformIgnorePatterns: [
            '<rootDir>/node_modules/(?!mathletics-portal-communication-service/)'
            

    Thanks for contributing an answer to Stack Overflow!

    • Please be sure to answer the question. Provide details and share your research!

    But avoid

    • Asking for help, clarification, or responding to other answers.
    • Making statements based on opinion; back them up with references or personal experience.

    To learn more, see our tips on writing great answers.