xinbloody
xinbloody

Reputation: 131

What is the real meaning of transformIgnorePatterns in Jest configuration?

In Jest we can configure transformIgnorePatterns to ignore files to be transpiled, which defaults to "/node_modules/". However, if a dependency is not translated when it is published, such as /node_modules/atest, according to the instructions on the official website, it should be configured to transformIgnorePatterns, which feels contrary to the "ignored" meaning of this configuration.

I want to know which files are translated and which files are ignored and not translated by pressing the configuration file below.

module.exports = {
    // ...
    transformIgnorePatterns: ['/node_modules/atest']
    // ...
}

Possible answer 1: Dependencies in node_modules except atest are transpiled

Possible answer 2: Only atest in node_modules is transpiled, the rest of the dependencies are not transpiled

Upvotes: 12

Views: 19170

Answers (1)

Lin Du
Lin Du

Reputation: 102672

From the doc, the transformIgnorePatterns option has default value: ["/node_modules/", "\\.pnp\\.[^\\\/]+$"]

It means:

If the file path matches any of the patterns, it will not be transformed.

So, the packages inside the node_modules directory will NOT be transformed by default.

Now, you have a package named atest which is not a pre-compiled package, you need to transform it using babel and don't transform other packages inside node_modules. So the configuration should be:

{
  "transformIgnorePatterns": ["/node_modules/(?!(atest)/)"]
}

Test paths:

/node_modules/atest/index.js
/node_modules/react/index.js
/node_modules/lodash/index.js
/node_modules/dayjs/index.js

The /node_modules/atest will be excluded from transformIgnorePatterns configuration which means it will be transformed.

See the regexp test

Upvotes: 11

Related Questions