From 685143e8deba7fd6a39617dc930dc483868643d6 Mon Sep 17 00:00:00 2001 From: Andrew Smith Date: Sat, 28 Aug 2021 21:35:51 -0700 Subject: [PATCH 1/5] First draft of docs improvements. --- docs/Configuration.md | 8 +++++++- docs/TutorialReactNative.md | 13 +++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/docs/Configuration.md b/docs/Configuration.md index 58c1d6133852..80f34f26e027 100644 --- a/docs/Configuration.md +++ b/docs/Configuration.md @@ -1367,7 +1367,13 @@ 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. + +Patterns that overlap with each other may result in a file not being transformed unexpectedly. For example: + +`["/node_modules/(?!foo|bar)", ".*/bar/"]` + +The first matches any `node_modules` folders except for `node_modules/foo` and `node_modules/bar`, but the second one matches any with `bar` in the path. In this case, `node_modules/bar` 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. diff --git a/docs/TutorialReactNative.md b/docs/TutorialReactNative.md index bba83bf1c291..f6abc3a6295b 100644 --- a/docs/TutorialReactNative.md +++ b/docs/TutorialReactNative.md @@ -148,6 +148,19 @@ By default the jest-react-native preset only processes the project's own source } ``` +Because `transformIgnorePatterns` will not transform a file if it matches **any** pattern provided, it is necessary to only use the above exclusion pattern only once. Otherwise, a folder that doesn't match in one pattern will match in the other. In the example below, neither module will be transformed: + +```json +{ + "transformIgnorePatterns": [ + "node_modules/(?!(foo)/)", + "node_modules/(?!(bar)/)" + ] +} +``` + + + ### 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. From 35e6dc94065a7a1a0a577573892e0686b66b2d72 Mon Sep 17 00:00:00 2001 From: Andrew Smith Date: Sun, 29 Aug 2021 20:29:29 -0700 Subject: [PATCH 2/5] PR feedback. --- docs/Configuration.md | 21 +++++++++++++++++---- docs/TutorialReactNative.md | 15 +++++---------- 2 files changed, 22 insertions(+), 14 deletions(-) diff --git a/docs/Configuration.md b/docs/Configuration.md index 80f34f26e027..56aade567cc2 100644 --- a/docs/Configuration.md +++ b/docs/Configuration.md @@ -1369,15 +1369,28 @@ 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. -Patterns that overlap with each other may result in a file not being transformed unexpectedly. For example: +Providing regexp patterns that overlap with each other may result in files not being transformed that you expected to be transformed. For example: -`["/node_modules/(?!foo|bar)", ".*/bar/"]` +```json +{ + "transformIgnorePatterns": ["/node_modules/(?!foo|bar/)", "/bar/"] +} +``` -The first matches any `node_modules` folders except for `node_modules/foo` and `node_modules/bar`, but the second one matches any with `bar` in the path. In this case, `node_modules/bar` will not be transformed. +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. 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: `["/bower_components/", "/node_modules/"]`. +Example: + +```json +{ + "transformIgnorePatterns": [ + "/bower_components/", + "/node_modules/" + ] +} +``` 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). diff --git a/docs/TutorialReactNative.md b/docs/TutorialReactNative.md index f6abc3a6295b..ab16ef68c7b1 100644 --- a/docs/TutorialReactNative.md +++ b/docs/TutorialReactNative.md @@ -136,31 +136,26 @@ 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 separating them with the `|` operator: ```json { "transformIgnorePatterns": [ - "node_modules/(?!(react-native|my-project|react-native-button)/)" + "node_modules/(?!react-native|my-project|react-native-button/)" ] } ``` -Because `transformIgnorePatterns` will not transform a file if it matches **any** pattern provided, it is necessary to only use the above exclusion pattern only once. Otherwise, a folder that doesn't match in one pattern will match in the other. In the example below, neither module will be transformed: +`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)/)" - ] + "transformIgnorePatterns": ["node_modules/(?!foo/)", "node_modules/(?!bar/)"] } ``` - - ### 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. From 5b13a379941f534afd8b8c8ec24c14db3ba55faa Mon Sep 17 00:00:00 2001 From: Andrew Smith Date: Thu, 2 Sep 2021 08:46:05 -0700 Subject: [PATCH 3/5] More PR feedback. --- docs/Configuration.md | 6 +++--- docs/TutorialReactNative.md | 8 +++++--- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/docs/Configuration.md b/docs/Configuration.md index 56aade567cc2..9d6d8ebdd1cf 100644 --- a/docs/Configuration.md +++ b/docs/Configuration.md @@ -1373,12 +1373,14 @@ Providing regexp patterns that overlap with each other may result in files not b ```json { - "transformIgnorePatterns": ["/node_modules/(?!foo|bar/)", "/bar/"] + "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: @@ -1392,8 +1394,6 @@ Example: } ``` -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). - ### `unmockedModulePathPatterns` \[array<string>] Default: `[]` diff --git a/docs/TutorialReactNative.md b/docs/TutorialReactNative.md index ab16ef68c7b1..760aaed02b4b 100644 --- a/docs/TutorialReactNative.md +++ b/docs/TutorialReactNative.md @@ -138,21 +138,23 @@ The preset sets up the environment and is very opinionated and based on what we 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 separating them with the `|` operator: +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 { "transformIgnorePatterns": [ - "node_modules/(?!react-native|my-project|react-native-button/)" + "node_modules/(?!(react-native|my-project|react-native-button)/)" ] } ``` +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/)"] + "transformIgnorePatterns": ["node_modules/(?!foo/)", "node_modules/(?!bar/)"] // not what you want } ``` From fc3efc74312e53a5c3db5c167c589c866a378eed Mon Sep 17 00:00:00 2001 From: Andrew Smith Date: Thu, 2 Sep 2021 08:51:28 -0700 Subject: [PATCH 4/5] Copy edits to other versions. --- .../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 ++++++++-- 8 files changed, 137 insertions(+), 21 deletions(-) diff --git a/website/versioned_docs/version-25.x/Configuration.md b/website/versioned_docs/version-25.x/Configuration.md index dc100cfef6e7..1977013fc8f9 100644 --- a/website/versioned_docs/version-25.x/Configuration.md +++ b/website/versioned_docs/version-25.x/Configuration.md @@ -1208,16 +1208,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 5c806ff26bb9..a54914a45b45 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 4091301b32f3..e916d032325d 100644 --- a/website/versioned_docs/version-26.x/Configuration.md +++ b/website/versioned_docs/version-26.x/Configuration.md @@ -1302,14 +1302,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 5c806ff26bb9..a54914a45b45 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 9c2ab1e8d4d8..e8ba478bc65d 100644 --- a/website/versioned_docs/version-27.0/Configuration.md +++ b/website/versioned_docs/version-27.0/Configuration.md @@ -1332,14 +1332,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 bba83bf1c291..760aaed02b4b 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 58c1d6133852..9d6d8ebdd1cf 100644 --- a/website/versioned_docs/version-27.1/Configuration.md +++ b/website/versioned_docs/version-27.1/Configuration.md @@ -1367,14 +1367,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 bba83bf1c291..760aaed02b4b 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. From 918edd819ff529fa1c754cdfb489665b4b7f62aa Mon Sep 17 00:00:00 2001 From: Andrew Smith Date: Thu, 23 Sep 2021 13:36:50 -0700 Subject: [PATCH 5/5] Carry over changes to 27.2. --- .../version-27.2/Configuration.md | 25 ++++++++++++++++--- .../version-27.2/TutorialReactNative.md | 14 +++++++++-- 2 files changed, 34 insertions(+), 5 deletions(-) diff --git a/website/versioned_docs/version-27.2/Configuration.md b/website/versioned_docs/version-27.2/Configuration.md index 384e60723092..e6a28430f8b8 100644 --- a/website/versioned_docs/version-27.2/Configuration.md +++ b/website/versioned_docs/version-27.2/Configuration.md @@ -1371,14 +1371,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.