I seem to be unable to overwrite the transformIgnorePatterns property through the jest config options in package.json when using react-app-rewired.
For a bit of context; I'm following the guide on Minimizing Bundle Size from Material-UI. The issue I'm trying to solve is when running the react-app-rewired test command I get the following error:
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".
Here's what you can do:
• If you are trying to use ECMAScript Modules, see https://jestjs.io/docs/en/ecmascript-modules for how to enable it.
• 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/en/configuration.html
My understanding is Jest needs to run Babel to compile the Material-UI components. However by default any packages in the node_modules folder are ignored from this. The solution seems to be to add any necessary packages to the transformIgnorePatterns config option - and I can verify this since everything works fine if I use the Jest CLI argument.
react-app-rewired test --transformIgnorePatterns \"node_modules/(?!(@material-ui|@babel|jest-runtime))\"
Now I tried to move this into the jest config section in package.json - but this does not behave as I would expect. (I've deliberately added in the invalid regex just so a SyntaxError was thrown to demonstrate.)
When running the default react-scripts test command I get:
SyntaxError: Invalid regular expression: /?! deliberately invalid regex|\\node_modules\\(?!(@material-ui|@babel|jest-runtime)\\)/:
Nothing to repeat
at new RegExp (<anonymous>)
Looks good - The regex just contains the two values I defined. However, when running with react-app-rewired test I get:
SyntaxError: Invalid regular expression: /?! deliberately invalid regex|\\node_modules\\(?!(@material-ui|@babel|jest-runtime)\\)|[\\\\]node_modules[\\\\].+\.(js|jsx|mjs|cjs|ts|tsx)$|^.+\.module\.(css|sass|scss)$/:
Nothing to repeat
at new RegExp (<anonymous>)
Notice react-app-rewired includes the default patterns defined in base create react app. This means I'm unable to remove these regex values so all modules within node_modules are always ignored by babel.
I've noticed the behaviour in rewireJestConfig.js differs when it comes to overwriting arrays (it uses concat) compared to Create React App, which replaces them entirely.
Hi @timarney
I seem to be unable to overwrite the
transformIgnorePatterns
property through thejest
config options inpackage.json
when usingreact-app-rewired
.For a bit of context; I'm following the guide on Minimizing Bundle Size from Material-UI. The issue I'm trying to solve is when running the
react-app-rewired test
command I get the following error:My understanding is Jest needs to run Babel to compile the Material-UI components. However by default any packages in the
node_modules
folder are ignored from this. The solution seems to be to add any necessary packages to thetransformIgnorePatterns
config option - and I can verify this since everything works fine if I use the Jest CLI argument.Now I tried to move this into the
jest
config section inpackage.json
- but this does not behave as I would expect. (I've deliberately added in the invalid regex just so a SyntaxError was thrown to demonstrate.)When running the default
react-scripts test
command I get:Looks good - The regex just contains the two values I defined. However, when running with
react-app-rewired test
I get:Notice
react-app-rewired
includes the default patterns defined in base create react app. This means I'm unable to remove these regex values so all modules withinnode_modules
are always ignored by babel.I've noticed the behaviour in
rewireJestConfig.js
differs when it comes to overwriting arrays (it uses concat) compared to Create React App, which replaces them entirely.https://github.com/timarney/react-app-rewired/blob/b7b4bea299c9fe67f77b736d10d1f9482dea6459/scripts/utils/rewireJestConfig.js#L21-L23
Should this behave the same as CRA? Or is there something else I'm missing?
Thanks!