Skip to content

Commit

Permalink
Clarify behavior of transformIgnorePatterns when including multiple…
Browse files Browse the repository at this point in the history
… patterns. (#11796)
  • Loading branch information
AndrewSouthpaw authored Sep 29, 2021
1 parent 94734e1 commit fb0e09c
Show file tree
Hide file tree
Showing 12 changed files with 205 additions and 31 deletions.
25 changes: 22 additions & 3 deletions docs/Configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -1373,14 +1373,33 @@ If the tests are written using [native ESM](ECMAScriptModules.md) the transforme

Default: `["/node_modules/", "\\.pnp\\.[^\\\/]+$"]`

An array of regexp pattern strings that are matched against all source file paths before transformation. If the file path matches any of the patterns, it will not be transformed.
An array of regexp pattern strings that are matched against all source file paths before transformation. If the file path matches **any** of the patterns, it will not be transformed.

These pattern strings match against the full path. Use the `<rootDir>` string token to include the path to your project's root directory to prevent it from accidentally ignoring all of your files in different environments that may have different root directories.
Providing regexp patterns that overlap with each other may result in files not being transformed that you expected to be transformed. For example:

```json
{
"transformIgnorePatterns": ["/node_modules/(?!(foo|bar)/)", "/bar/"]
}
```

Example: `["<rootDir>/bower_components/", "<rootDir>/node_modules/"]`.
The first pattern will match (and therefore not transform) files inside `/node_modules` except for those in `/node_modules/foo/` and `/node_modules/bar/`. The second pattern will match (and therefore not transform) files inside any path with `/bar/` in it. With the two together, files in `/node_modules/bar/` will not be transformed because it does match the second pattern, even though it was excluded by the first.

Sometimes it happens (especially in React Native or TypeScript projects) that 3rd party modules are published as untranspiled. Since all files inside `node_modules` are not transformed by default, Jest will not understand the code in these modules, resulting in syntax errors. To overcome this, you may use `transformIgnorePatterns` to allow transpiling such modules. You'll find a good example of this use case in [React Native Guide](/docs/tutorial-react-native#transformignorepatterns-customization).

These pattern strings match against the full path. Use the `<rootDir>` string token to include the path to your project's root directory to prevent it from accidentally ignoring all of your files in different environments that may have different root directories.

Example:

```json
{
"transformIgnorePatterns": [
"<rootDir>/bower_components/",
"<rootDir>/node_modules/"
]
}
```

### `unmockedModulePathPatterns` \[array&lt;string&gt;]

Default: `[]`
Expand Down
14 changes: 12 additions & 2 deletions docs/TutorialReactNative.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,9 @@ The preset sets up the environment and is very opinionated and based on what we

### transformIgnorePatterns customization

The [`transformIgnorePatterns`](configuration#transformignorepatterns-arraystring) option can be used to specify which files shall be transformed by Babel. Many react-native npm modules unfortunately don't pre-compile their source code before publishing.
The [`transformIgnorePatterns`](configuration#transformignorepatterns-arraystring) option can be used to specify which files shall be transformed by Babel. Many `react-native` npm modules unfortunately don't pre-compile their source code before publishing.

By default the jest-react-native preset only processes the project's own source files and react-native. If you have npm dependencies that have to be transformed you can customize this configuration option by including modules other than react-native:
By default the `jest-react-native` preset only processes the project's own source files and `react-native`. If you have npm dependencies that have to be transformed you can customize this configuration option by including modules other than `react-native` by grouping them and separating them with the `|` operator:

```json
{
Expand All @@ -148,6 +148,16 @@ By default the jest-react-native preset only processes the project's own source
}
```

You can test which paths would match (and thus be excluded from transformation) with a tool [like this](https://regex101.com/r/JsLIDM/1).

`transformIgnorePatterns` will exclude a file from transformation if the path matches against **any** pattern provided. Splitting into multiple patterns could therefore have unintended results if you are not careful. In the example below, the exclusion (also known as a negative lookahead assertion) for `foo` and `bar` cancel each other out:

```json
{
"transformIgnorePatterns": ["node_modules/(?!foo/)", "node_modules/(?!bar/)"] // not what you want
}
```

### setupFiles

If you'd like to provide additional configuration for every test file, the [`setupFiles` configuration option](configuration#setupfiles-array) can be used to specify setup scripts.
Expand Down
27 changes: 23 additions & 4 deletions website/versioned_docs/version-25.x/Configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -1210,16 +1210,35 @@ _Note: when adding additional code transformers, this will overwrite the default

### `transformIgnorePatterns` \[array&lt;string&gt;]

Default: `["/node_modules/"]`
Default: `["/node_modules/", "\\.pnp\\.[^\\\/]+$"]`

An array of regexp pattern strings that are matched against all source file paths before transformation. If the file path matches any of the patterns, it will not be transformed.
An array of regexp pattern strings that are matched against all source file paths before transformation. If the file path matches **any** of the patterns, it will not be transformed.

These pattern strings match against the full path. Use the `<rootDir>` string token to include the path to your project's root directory to prevent it from accidentally ignoring all of your files in different environments that may have different root directories.
Providing regexp patterns that overlap with each other may result in files not being transformed that you expected to be transformed. For example:

Example: `["<rootDir>/bower_components/", "<rootDir>/node_modules/"]`.
```json
{
"transformIgnorePatterns": ["/node_modules/(?!(foo|bar)/)", "/bar/"]
}
```

The first pattern will match (and therefore not transform) files inside `/node_modules` except for those in `/node_modules/foo/` and `/node_modules/bar/`. The second pattern will match (and therefore not transform) files inside any path with `/bar/` in it. With the two together, files in `/node_modules/bar/` will not be transformed because it does match the second pattern, even though it was excluded by the first.

Sometimes it happens (especially in React Native or TypeScript projects) that 3rd party modules are published as untranspiled. Since all files inside `node_modules` are not transformed by default, Jest will not understand the code in these modules, resulting in syntax errors. To overcome this, you may use `transformIgnorePatterns` to allow transpiling such modules. You'll find a good example of this use case in [React Native Guide](/docs/tutorial-react-native#transformignorepatterns-customization).

These pattern strings match against the full path. Use the `<rootDir>` string token to include the path to your project's root directory to prevent it from accidentally ignoring all of your files in different environments that may have different root directories.

Example:

```json
{
"transformIgnorePatterns": [
"<rootDir>/bower_components/",
"<rootDir>/node_modules/"
]
}
```

### `unmockedModulePathPatterns` \[array&lt;string&gt;]

Default: `[]`
Expand Down
14 changes: 12 additions & 2 deletions website/versioned_docs/version-25.x/TutorialReactNative.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,9 @@ The preset sets up the environment and is very opinionated and based on what we

### transformIgnorePatterns customization

The [`transformIgnorePatterns`](configuration#transformignorepatterns-arraystring) option can be used to specify which files shall be transformed by Babel. Many react-native npm modules unfortunately don't pre-compile their source code before publishing.
The [`transformIgnorePatterns`](configuration#transformignorepatterns-arraystring) option can be used to specify which files shall be transformed by Babel. Many `react-native` npm modules unfortunately don't pre-compile their source code before publishing.

By default the jest-react-native preset only processes the project's own source files and react-native. If you have npm dependencies that have to be transformed you can customize this configuration option by including modules other than react-native:
By default the `jest-react-native` preset only processes the project's own source files and `react-native`. If you have npm dependencies that have to be transformed you can customize this configuration option by including modules other than `react-native` by grouping them and separating them with the `|` operator:

```json
{
Expand All @@ -148,6 +148,16 @@ By default the jest-react-native preset only processes the project's own source
}
```

You can test which paths would match (and thus be excluded from transformation) with a tool [like this](https://regex101.com/r/JsLIDM/1).

`transformIgnorePatterns` will exclude a file from transformation if the path matches against **any** pattern provided. Splitting into multiple patterns could therefore have unintended results if you are not careful. In the example below, the exclusion (also known as a negative lookahead assertion) for `foo` and `bar` cancel each other out:

```json
{
"transformIgnorePatterns": ["node_modules/(?!foo/)", "node_modules/(?!bar/)"] // not what you want
}
```

### setupFiles

If you'd like to provide additional configuration for every test file, the [`setupFiles` configuration option](configuration#setupfiles-array) can be used to specify setup scripts.
Expand Down
25 changes: 22 additions & 3 deletions website/versioned_docs/version-26.x/Configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -1304,14 +1304,33 @@ _Note: when adding additional code transformers, this will overwrite the default

Default: `["/node_modules/", "\\.pnp\\.[^\\\/]+$"]`

An array of regexp pattern strings that are matched against all source file paths before transformation. If the file path matches any of the patterns, it will not be transformed.
An array of regexp pattern strings that are matched against all source file paths before transformation. If the file path matches **any** of the patterns, it will not be transformed.

These pattern strings match against the full path. Use the `<rootDir>` string token to include the path to your project's root directory to prevent it from accidentally ignoring all of your files in different environments that may have different root directories.
Providing regexp patterns that overlap with each other may result in files not being transformed that you expected to be transformed. For example:

```json
{
"transformIgnorePatterns": ["/node_modules/(?!(foo|bar)/)", "/bar/"]
}
```

Example: `["<rootDir>/bower_components/", "<rootDir>/node_modules/"]`.
The first pattern will match (and therefore not transform) files inside `/node_modules` except for those in `/node_modules/foo/` and `/node_modules/bar/`. The second pattern will match (and therefore not transform) files inside any path with `/bar/` in it. With the two together, files in `/node_modules/bar/` will not be transformed because it does match the second pattern, even though it was excluded by the first.

Sometimes it happens (especially in React Native or TypeScript projects) that 3rd party modules are published as untranspiled. Since all files inside `node_modules` are not transformed by default, Jest will not understand the code in these modules, resulting in syntax errors. To overcome this, you may use `transformIgnorePatterns` to allow transpiling such modules. You'll find a good example of this use case in [React Native Guide](/docs/tutorial-react-native#transformignorepatterns-customization).

These pattern strings match against the full path. Use the `<rootDir>` string token to include the path to your project's root directory to prevent it from accidentally ignoring all of your files in different environments that may have different root directories.

Example:

```json
{
"transformIgnorePatterns": [
"<rootDir>/bower_components/",
"<rootDir>/node_modules/"
]
}
```

### `unmockedModulePathPatterns` \[array&lt;string&gt;]

Default: `[]`
Expand Down
14 changes: 12 additions & 2 deletions website/versioned_docs/version-26.x/TutorialReactNative.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,9 @@ The preset sets up the environment and is very opinionated and based on what we

### transformIgnorePatterns customization

The [`transformIgnorePatterns`](configuration#transformignorepatterns-arraystring) option can be used to specify which files shall be transformed by Babel. Many react-native npm modules unfortunately don't pre-compile their source code before publishing.
The [`transformIgnorePatterns`](configuration#transformignorepatterns-arraystring) option can be used to specify which files shall be transformed by Babel. Many `react-native` npm modules unfortunately don't pre-compile their source code before publishing.

By default the jest-react-native preset only processes the project's own source files and react-native. If you have npm dependencies that have to be transformed you can customize this configuration option by including modules other than react-native:
By default the `jest-react-native` preset only processes the project's own source files and `react-native`. If you have npm dependencies that have to be transformed you can customize this configuration option by including modules other than `react-native` by grouping them and separating them with the `|` operator:

```json
{
Expand All @@ -148,6 +148,16 @@ By default the jest-react-native preset only processes the project's own source
}
```

You can test which paths would match (and thus be excluded from transformation) with a tool [like this](https://regex101.com/r/JsLIDM/1).

`transformIgnorePatterns` will exclude a file from transformation if the path matches against **any** pattern provided. Splitting into multiple patterns could therefore have unintended results if you are not careful. In the example below, the exclusion (also known as a negative lookahead assertion) for `foo` and `bar` cancel each other out:

```json
{
"transformIgnorePatterns": ["node_modules/(?!foo/)", "node_modules/(?!bar/)"] // not what you want
}
```

### setupFiles

If you'd like to provide additional configuration for every test file, the [`setupFiles` configuration option](configuration#setupfiles-array) can be used to specify setup scripts.
Expand Down
25 changes: 22 additions & 3 deletions website/versioned_docs/version-27.0/Configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -1334,14 +1334,33 @@ If the tests are written using [native ESM](ECMAScriptModules.md) the transforme

Default: `["/node_modules/", "\\.pnp\\.[^\\\/]+$"]`

An array of regexp pattern strings that are matched against all source file paths before transformation. If the file path matches any of the patterns, it will not be transformed.
An array of regexp pattern strings that are matched against all source file paths before transformation. If the file path matches **any** of the patterns, it will not be transformed.

These pattern strings match against the full path. Use the `<rootDir>` string token to include the path to your project's root directory to prevent it from accidentally ignoring all of your files in different environments that may have different root directories.
Providing regexp patterns that overlap with each other may result in files not being transformed that you expected to be transformed. For example:

```json
{
"transformIgnorePatterns": ["/node_modules/(?!(foo|bar)/)", "/bar/"]
}
```

Example: `["<rootDir>/bower_components/", "<rootDir>/node_modules/"]`.
The first pattern will match (and therefore not transform) files inside `/node_modules` except for those in `/node_modules/foo/` and `/node_modules/bar/`. The second pattern will match (and therefore not transform) files inside any path with `/bar/` in it. With the two together, files in `/node_modules/bar/` will not be transformed because it does match the second pattern, even though it was excluded by the first.

Sometimes it happens (especially in React Native or TypeScript projects) that 3rd party modules are published as untranspiled. Since all files inside `node_modules` are not transformed by default, Jest will not understand the code in these modules, resulting in syntax errors. To overcome this, you may use `transformIgnorePatterns` to allow transpiling such modules. You'll find a good example of this use case in [React Native Guide](/docs/tutorial-react-native#transformignorepatterns-customization).

These pattern strings match against the full path. Use the `<rootDir>` string token to include the path to your project's root directory to prevent it from accidentally ignoring all of your files in different environments that may have different root directories.

Example:

```json
{
"transformIgnorePatterns": [
"<rootDir>/bower_components/",
"<rootDir>/node_modules/"
]
}
```

### `unmockedModulePathPatterns` \[array&lt;string&gt;]

Default: `[]`
Expand Down
14 changes: 12 additions & 2 deletions website/versioned_docs/version-27.0/TutorialReactNative.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,9 @@ The preset sets up the environment and is very opinionated and based on what we

### transformIgnorePatterns customization

The [`transformIgnorePatterns`](configuration#transformignorepatterns-arraystring) option can be used to specify which files shall be transformed by Babel. Many react-native npm modules unfortunately don't pre-compile their source code before publishing.
The [`transformIgnorePatterns`](configuration#transformignorepatterns-arraystring) option can be used to specify which files shall be transformed by Babel. Many `react-native` npm modules unfortunately don't pre-compile their source code before publishing.

By default the jest-react-native preset only processes the project's own source files and react-native. If you have npm dependencies that have to be transformed you can customize this configuration option by including modules other than react-native:
By default the `jest-react-native` preset only processes the project's own source files and `react-native`. If you have npm dependencies that have to be transformed you can customize this configuration option by including modules other than `react-native` by grouping them and separating them with the `|` operator:

```json
{
Expand All @@ -148,6 +148,16 @@ By default the jest-react-native preset only processes the project's own source
}
```

You can test which paths would match (and thus be excluded from transformation) with a tool [like this](https://regex101.com/r/JsLIDM/1).

`transformIgnorePatterns` will exclude a file from transformation if the path matches against **any** pattern provided. Splitting into multiple patterns could therefore have unintended results if you are not careful. In the example below, the exclusion (also known as a negative lookahead assertion) for `foo` and `bar` cancel each other out:

```json
{
"transformIgnorePatterns": ["node_modules/(?!foo/)", "node_modules/(?!bar/)"] // not what you want
}
```

### setupFiles

If you'd like to provide additional configuration for every test file, the [`setupFiles` configuration option](configuration#setupfiles-array) can be used to specify setup scripts.
Expand Down
Loading

0 comments on commit fb0e09c

Please sign in to comment.