From c7e1724b4099534c2c0ab3cd7b7349c0307eded0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Pierzcha=C5=82a?= Date: Tue, 2 Mar 2021 14:17:45 +0100 Subject: [PATCH 1/2] feat: expose custom Jest preset restoring global Promise --- README.md | 18 ++++++++++++++++++ jest/preset.js => jest-preset/index.js | 0 {jest => jest-preset}/restore-promise.js | 0 {jest => jest-preset}/save-promise.js | 0 package.json | 3 ++- 5 files changed, 20 insertions(+), 1 deletion(-) rename jest/preset.js => jest-preset/index.js (100%) rename {jest => jest-preset}/restore-promise.js (100%) rename {jest => jest-preset}/save-promise.js (100%) diff --git a/README.md b/README.md index aaef02bf8..2385ce1bb 100644 --- a/README.md +++ b/README.md @@ -73,6 +73,24 @@ Then automatically add it to your jest tests by using `setupFilesAfterEnv` optio } ``` +### Custom Jest Preset + +> **important** if you use "modern" Fake Timers + +We generally advice to use the "react-native" preset when testing with this library. However, if you use ["modern" Fake Timers](https://jestjs.io/blog/2020/05/05/jest-26#new-fake-timers) (default since Jest 27), you'll need to apply our custom Jest preset or awaiting promises, like `waitFor`, will timeout. + +This is a [known issue](https://github.com/facebook/react-native/issues/29303). It happens because React Native's Jest preset overrides native Promise. Our preset restores it to defaults, which is not a problem in most apps out there. + +Here's how you apply a custom preset in your Jest config: + +```json +{ + "preset": "@testing-library/react-native" +} +``` + +If this doesn't work for you, please fall back to using "legacy" fake timers. + ### Flow Note for [Flow](https://flow.org) users – you'll also need to install typings for `react-test-renderer`: diff --git a/jest/preset.js b/jest-preset/index.js similarity index 100% rename from jest/preset.js rename to jest-preset/index.js diff --git a/jest/restore-promise.js b/jest-preset/restore-promise.js similarity index 100% rename from jest/restore-promise.js rename to jest-preset/restore-promise.js diff --git a/jest/save-promise.js b/jest-preset/save-promise.js similarity index 100% rename from jest/save-promise.js rename to jest-preset/save-promise.js diff --git a/package.json b/package.json index 3b9d2dae7..277a6ad4c 100644 --- a/package.json +++ b/package.json @@ -20,6 +20,7 @@ ], "files": [ "build/", + "jest-preset/", "typings/index.d.ts", "pure.js", "dont-cleanup-after-each.js" @@ -70,7 +71,7 @@ "build": "rm -rf build; babel src --out-dir build --ignore 'src/__tests__/*'" }, "jest": { - "preset": "../jest/preset.js", + "preset": "../jest-preset", "moduleFileExtensions": [ "js", "json" From a583e6970af9c1f45ba118cf66e2ba6de0f1e010 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Pierzcha=C5=82a?= Date: Tue, 2 Mar 2021 14:22:31 +0100 Subject: [PATCH 2/2] typo --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 2385ce1bb..893836e1d 100644 --- a/README.md +++ b/README.md @@ -77,7 +77,7 @@ Then automatically add it to your jest tests by using `setupFilesAfterEnv` optio > **important** if you use "modern" Fake Timers -We generally advice to use the "react-native" preset when testing with this library. However, if you use ["modern" Fake Timers](https://jestjs.io/blog/2020/05/05/jest-26#new-fake-timers) (default since Jest 27), you'll need to apply our custom Jest preset or awaiting promises, like `waitFor`, will timeout. +We generally advise to use the "react-native" preset when testing with this library. However, if you use ["modern" Fake Timers](https://jestjs.io/blog/2020/05/05/jest-26#new-fake-timers) (default since Jest 27), you'll need to apply our custom Jest preset or awaiting promises, like `waitFor`, will timeout. This is a [known issue](https://github.com/facebook/react-native/issues/29303). It happens because React Native's Jest preset overrides native Promise. Our preset restores it to defaults, which is not a problem in most apps out there.