question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Problems overriding jest.transformIgnorePatterns

See original GitHub issue

Hi there!

First of all, thanks for this awesome project!

We use packages that are written in ES6. Jest by default doesn’t transform anything inside node_modules unless transformIgnorePatterns is set. If jest is used without create-react-app the following configuration fixes the issue:

"jest": {
  "transformIgnorePatterns": ["/node_modules/(?!(@my-company)/).*/"]
}

NOTE: @my-company is used because all the packages are scoped.

When we use that configuration with create-react-app and react-app-rewired the final value for that option is:

["/node_modules/(?!(@my-company)/).*/", "[/\\\\]node_modules[/\\\\].+\\.(js|jsx|mjs)$"]

Jest would run both regular expressions and if any of them is matched the file won’t be transformed. The second regex matches all .js inside node_modules forcing jest to do not transform the files inside node_modules/@my-company.


I can think of a couple of solutions:

  1. Override transformIgnorePatters instead of append. My impression is that if anyone needs to modify this option, they will always want to avoid the default pattern.
  2. Use a special property to enable overriding. For example:
"jest": {
  "override": {
    "transformIgnorePatterns": ["..."]
  }
}

Please, let me know your thoughts. If you’re happy with any of the solutions I’ll submit a PR.

Thanks

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

13reactions
dawnmistcommented, May 9, 2018

@timarney Both this and #240 have the same root cause - although #240 is also affected by not passing even the permitted overrides into create-react-app.

React-app-rewired merges any array or object values defined in package.json with the original values from create-react-app (react-scripts package) instead of overwriting them. I don’t know the history of why it was done this way. I think this either needs to be documented in the Readme or the decision to be re-looked at, as it wasn’t behaviour I’d expected initially either.

What you can do to make an overwrite setting instead of a setting to be merged is to apply the setting inside the jest function in config-overrides, which is run after the merge has occurred. This lets you make a full replacement instead of a merge replacement.

module.exports = {
  jest: (config) => {
    config.transformIgnorePatterns = ["/node_modules/(?!(@my-company)/).*/"]
    return config;
  }
}
4reactions
Luismahoucommented, May 9, 2018

@dawnmist Thanks, that works like a charm!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Nextjs & Jest transform/transformIgnorePatterns not working ...
Here is a solution in case someone runs into this same issue but is using NextJs 12.2 , next/jest and Jest 28 ....
Read more >
Configuring Jest
Test files are normally ignored from collecting code coverage. With this option, you can overwrite this behavior and include otherwise ignored ...
Read more >
Troubleshooting | jest-preset-angular - GitHub Pages
Please be advised that every entry in default configuration may be overridden to best suite your app's needs. Can't resolve all parameters for...
Read more >
@marko/jest - npm
You can override the default Marko compiler options by adding a Jest "globals" config with a marko property. An additional taglib property ...
Read more >
SyntaxError: Unexpected token when using react-script test
json Jest configuration are not currently supported by Create React App: • transformIgnorePatterns If you wish to override other Jest options, ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found