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!
"transformIgnorePatterns": ["node_modules"] won't work because it is pretty much the default behavior. Have you tried using the official recommendation config?
By default, if Jest sees a Babel config, it will use that to transform your files, ignoring "node_modules". Here's what you can do: • 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.
Jest encountered an unexpected token This usually means that you are trying to import a file which Jest cannot parse, e.g. it's not plain JavaScript. By default, if Jest sees a Babel config, it will use that to transform your files, ignoring "node_modules".
These options let you control Jest's behavior in your package.json file. The Jest philosophy is to work great by default, but sometimes you just need more configuration power. You can retrieve Jest's default options to expand them if needed: This option tells Jest that all imported modules in your tests should be mocked automatically.
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
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']
};
Adding trailing slash fix this for me:
{
// ...
transformIgnorePatterns: [
'<rootDir>/node_modules/(?!mathletics-portal-communication-service/)'
]
};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With