From fb0e09c1e60d8e2900f6d7a48a113b9d144e443b Mon Sep 17 00:00:00 2001 From: Andrew Smith Date: Wed, 29 Sep 2021 03:44:09 -0700 Subject: [PATCH] Clarify behavior of `transformIgnorePatterns` when including multiple patterns. (#11796) --- docs/Configuration.md | 25 ++++++++++++++--- docs/TutorialReactNative.md | 14 ++++++++-- .../version-25.x/Configuration.md | 27 ++++++++++++++++--- .../version-25.x/TutorialReactNative.md | 14 ++++++++-- .../version-26.x/Configuration.md | 25 ++++++++++++++--- .../version-26.x/TutorialReactNative.md | 14 ++++++++-- .../version-27.0/Configuration.md | 25 ++++++++++++++--- .../version-27.0/TutorialReactNative.md | 14 ++++++++-- .../version-27.1/Configuration.md | 25 ++++++++++++++--- .../version-27.1/TutorialReactNative.md | 14 ++++++++-- .../version-27.2/Configuration.md | 25 ++++++++++++++--- .../version-27.2/TutorialReactNative.md | 14 ++++++++-- 12 files changed, 205 insertions(+), 31 deletions(-) diff --git a/docs/Configuration.md b/docs/Configuration.md index b36987a84352..5b051f3ff450 100644 --- a/docs/Configuration.md +++ b/docs/Configuration.md @@ -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 `` 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: `["/bower_components/", "/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 `` 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": [ + "/bower_components/", + "/node_modules/" + ] +} +``` + ### `unmockedModulePathPatterns` \[array<string>] Default: `[]` diff --git a/docs/TutorialReactNative.md b/docs/TutorialReactNative.md index 3bbe76e5793f..d89564980791 100644 --- a/docs/TutorialReactNative.md +++ b/docs/TutorialReactNative.md @@ -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 { @@ -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. diff --git a/website/versioned_docs/version-25.x/Configuration.md b/website/versioned_docs/version-25.x/Configuration.md index c525be374165..55ab8e845024 100644 --- a/website/versioned_docs/version-25.x/Configuration.md +++ b/website/versioned_docs/version-25.x/Configuration.md @@ -1210,16 +1210,35 @@ _Note: when adding additional code transformers, this will overwrite the default ### `transformIgnorePatterns` \[array<string>] -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 `` 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: `["/bower_components/", "/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 `` 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": [ + "/bower_components/", + "/node_modules/" + ] +} +``` + ### `unmockedModulePathPatterns` \[array<string>] Default: `[]` diff --git a/website/versioned_docs/version-25.x/TutorialReactNative.md b/website/versioned_docs/version-25.x/TutorialReactNative.md index d6c0fbf56860..beabd0d09e2d 100644 --- a/website/versioned_docs/version-25.x/TutorialReactNative.md +++ b/website/versioned_docs/version-25.x/TutorialReactNative.md @@ -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 { @@ -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. diff --git a/website/versioned_docs/version-26.x/Configuration.md b/website/versioned_docs/version-26.x/Configuration.md index ae456a6a93db..2b9301b42f1b 100644 --- a/website/versioned_docs/version-26.x/Configuration.md +++ b/website/versioned_docs/version-26.x/Configuration.md @@ -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 `` 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: `["/bower_components/", "/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 `` 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": [ + "/bower_components/", + "/node_modules/" + ] +} +``` + ### `unmockedModulePathPatterns` \[array<string>] Default: `[]` diff --git a/website/versioned_docs/version-26.x/TutorialReactNative.md b/website/versioned_docs/version-26.x/TutorialReactNative.md index d6c0fbf56860..beabd0d09e2d 100644 --- a/website/versioned_docs/version-26.x/TutorialReactNative.md +++ b/website/versioned_docs/version-26.x/TutorialReactNative.md @@ -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 { @@ -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. diff --git a/website/versioned_docs/version-27.0/Configuration.md b/website/versioned_docs/version-27.0/Configuration.md index bab0a9e9074a..ad0251a572ca 100644 --- a/website/versioned_docs/version-27.0/Configuration.md +++ b/website/versioned_docs/version-27.0/Configuration.md @@ -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 `` 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: `["/bower_components/", "/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 `` 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": [ + "/bower_components/", + "/node_modules/" + ] +} +``` + ### `unmockedModulePathPatterns` \[array<string>] Default: `[]` diff --git a/website/versioned_docs/version-27.0/TutorialReactNative.md b/website/versioned_docs/version-27.0/TutorialReactNative.md index 3bbe76e5793f..d89564980791 100644 --- a/website/versioned_docs/version-27.0/TutorialReactNative.md +++ b/website/versioned_docs/version-27.0/TutorialReactNative.md @@ -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 { @@ -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. diff --git a/website/versioned_docs/version-27.1/Configuration.md b/website/versioned_docs/version-27.1/Configuration.md index 8e68c280a495..5edc798578e2 100644 --- a/website/versioned_docs/version-27.1/Configuration.md +++ b/website/versioned_docs/version-27.1/Configuration.md @@ -1369,14 +1369,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 `` 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: `["/bower_components/", "/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 `` 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": [ + "/bower_components/", + "/node_modules/" + ] +} +``` + ### `unmockedModulePathPatterns` \[array<string>] Default: `[]` diff --git a/website/versioned_docs/version-27.1/TutorialReactNative.md b/website/versioned_docs/version-27.1/TutorialReactNative.md index 3bbe76e5793f..d89564980791 100644 --- a/website/versioned_docs/version-27.1/TutorialReactNative.md +++ b/website/versioned_docs/version-27.1/TutorialReactNative.md @@ -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 { @@ -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. diff --git a/website/versioned_docs/version-27.2/Configuration.md b/website/versioned_docs/version-27.2/Configuration.md index b36987a84352..5b051f3ff450 100644 --- a/website/versioned_docs/version-27.2/Configuration.md +++ b/website/versioned_docs/version-27.2/Configuration.md @@ -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 `` 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: `["/bower_components/", "/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 `` 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": [ + "/bower_components/", + "/node_modules/" + ] +} +``` + ### `unmockedModulePathPatterns` \[array<string>] Default: `[]` diff --git a/website/versioned_docs/version-27.2/TutorialReactNative.md b/website/versioned_docs/version-27.2/TutorialReactNative.md index 3bbe76e5793f..d89564980791 100644 --- a/website/versioned_docs/version-27.2/TutorialReactNative.md +++ b/website/versioned_docs/version-27.2/TutorialReactNative.md @@ -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 { @@ -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.