From 1bebe187ee0e218a489b00332c2d9e0881e20ce2 Mon Sep 17 00:00:00 2001 From: Nir Yosef Date: Tue, 11 Oct 2022 15:45:44 +0300 Subject: [PATCH 1/4] allow ignore jest fake timers --- src/helpers/__tests__/timers.test.ts | 8 ++++++++ src/helpers/timers.ts | 3 ++- 2 files changed, 10 insertions(+), 1 deletion(-) create mode 100644 src/helpers/__tests__/timers.test.ts diff --git a/src/helpers/__tests__/timers.test.ts b/src/helpers/__tests__/timers.test.ts new file mode 100644 index 000000000..4b0a38ec6 --- /dev/null +++ b/src/helpers/__tests__/timers.test.ts @@ -0,0 +1,8 @@ +import { jestFakeTimersAreEnabled } from '../timers'; +describe('timers', () => { + it('should not mock timers if RNTL_IGNORE_JEST_FAKE_TIMERS is set', async () => { + process.env.RNTL_IGNORE_JEST_FAKE_TIMERS = 'true'; + jest.useFakeTimers(); + expect(jestFakeTimersAreEnabled()).toEqual(false); + }); +}); diff --git a/src/helpers/timers.ts b/src/helpers/timers.ts index 579cf3755..6c3ca517f 100644 --- a/src/helpers/timers.ts +++ b/src/helpers/timers.ts @@ -25,7 +25,8 @@ function getJestFakeTimersType(): FakeTimersTypes | null { // istanbul ignore if if ( typeof jest === 'undefined' || - typeof globalObj.setTimeout === 'undefined' + typeof globalObj.setTimeout === 'undefined' || + process.env.RNTL_IGNORE_JEST_FAKE_TIMERS ) { return null; } From 6eb58c92ab89419ed4a9f66633370a96c8e37994 Mon Sep 17 00:00:00 2001 From: Nir Yosef Date: Tue, 11 Oct 2022 15:48:43 +0300 Subject: [PATCH 2/4] update flag name --- src/helpers/__tests__/timers.test.ts | 4 ++-- src/helpers/timers.ts | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/helpers/__tests__/timers.test.ts b/src/helpers/__tests__/timers.test.ts index 4b0a38ec6..4a52ee4a2 100644 --- a/src/helpers/__tests__/timers.test.ts +++ b/src/helpers/__tests__/timers.test.ts @@ -1,7 +1,7 @@ import { jestFakeTimersAreEnabled } from '../timers'; describe('timers', () => { - it('should not mock timers if RNTL_IGNORE_JEST_FAKE_TIMERS is set', async () => { - process.env.RNTL_IGNORE_JEST_FAKE_TIMERS = 'true'; + it('should not mock timers if SKIP_AUTO_FAKE_TIMERS_DETECTION is set', async () => { + process.env.SKIP_AUTO_FAKE_TIMERS_DETECTION = 'true'; jest.useFakeTimers(); expect(jestFakeTimersAreEnabled()).toEqual(false); }); diff --git a/src/helpers/timers.ts b/src/helpers/timers.ts index 6c3ca517f..b5324ce24 100644 --- a/src/helpers/timers.ts +++ b/src/helpers/timers.ts @@ -26,7 +26,7 @@ function getJestFakeTimersType(): FakeTimersTypes | null { if ( typeof jest === 'undefined' || typeof globalObj.setTimeout === 'undefined' || - process.env.RNTL_IGNORE_JEST_FAKE_TIMERS + process.env.SKIP_AUTO_FAKE_TIMERS_DETECTION ) { return null; } From 17f62161ddf4933279d66916056c6f7ce4930b25 Mon Sep 17 00:00:00 2001 From: Nir Yosef Date: Tue, 18 Oct 2022 13:58:30 +0300 Subject: [PATCH 3/4] code review fixes --- src/helpers/__tests__/timers.test.ts | 4 ++-- src/helpers/timers.ts | 2 +- website/docs/API.md | 2 ++ 3 files changed, 5 insertions(+), 3 deletions(-) diff --git a/src/helpers/__tests__/timers.test.ts b/src/helpers/__tests__/timers.test.ts index 4a52ee4a2..13c099abe 100644 --- a/src/helpers/__tests__/timers.test.ts +++ b/src/helpers/__tests__/timers.test.ts @@ -1,7 +1,7 @@ import { jestFakeTimersAreEnabled } from '../timers'; describe('timers', () => { - it('should not mock timers if SKIP_AUTO_FAKE_TIMERS_DETECTION is set', async () => { - process.env.SKIP_AUTO_FAKE_TIMERS_DETECTION = 'true'; + it('should not mock timers if RNTL_SKIP_AUTO_DETECT_FAKE_TIMERS is set', async () => { + process.env.RNTL_SKIP_AUTO_DETECT_FAKE_TIMERS = 'true'; jest.useFakeTimers(); expect(jestFakeTimersAreEnabled()).toEqual(false); }); diff --git a/src/helpers/timers.ts b/src/helpers/timers.ts index b5324ce24..06646be7f 100644 --- a/src/helpers/timers.ts +++ b/src/helpers/timers.ts @@ -26,7 +26,7 @@ function getJestFakeTimersType(): FakeTimersTypes | null { if ( typeof jest === 'undefined' || typeof globalObj.setTimeout === 'undefined' || - process.env.SKIP_AUTO_FAKE_TIMERS_DETECTION + process.env.RNTL_SKIP_AUTO_DETECT_FAKE_TIMERS ) { return null; } diff --git a/website/docs/API.md b/website/docs/API.md index 6d8adb34f..4f8cd2614 100644 --- a/website/docs/API.md +++ b/website/docs/API.md @@ -730,6 +730,8 @@ type Config = { function configure(options: Partial) {} ``` +### Environment variables +* *RNTL_SKIP_AUTO_DETECT_FAKE_TIMERS* (default: false): Useful in rare cases when you need to disable the auto-detection of fake timers (see #886). #### `asyncUtilTimeout` option From 36db7fdfbf10f8380482d8e55284c62958f635ad Mon Sep 17 00:00:00 2001 From: Maciej Jastrzebski Date: Tue, 18 Oct 2022 13:29:58 +0200 Subject: [PATCH 4/4] chore: tweak docs --- website/docs/API.md | 22 +++++++++++++++++++--- website/docs/MigrationV2.md | 2 +- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/website/docs/API.md b/website/docs/API.md index 4f8cd2614..212ca9393 100644 --- a/website/docs/API.md +++ b/website/docs/API.md @@ -49,6 +49,9 @@ title: API - [`configure`](#configure) - [`asyncUtilTimeout` option](#asyncutiltimeout-option) - [`resetToDefaults()`](#resettodefaults) + - [Environment variables](#environment-variables) + - [`RNTL_SKIP_AUTO_CLEANUP`](#rntl_skip_auto_cleanup) + - [`RNTL_SKIP_AUTO_DETECT_FAKE_TIMERS`](#rntl_skip_auto_detect_fake_timers) - [Accessibility](#accessibility) - [`isInaccessible`](#isinaccessible) @@ -730,9 +733,6 @@ type Config = { function configure(options: Partial) {} ``` -### Environment variables -* *RNTL_SKIP_AUTO_DETECT_FAKE_TIMERS* (default: false): Useful in rare cases when you need to disable the auto-detection of fake timers (see #886). - #### `asyncUtilTimeout` option Default timeout, in ms, for async helper functions (`waitFor`, `waitForElementToBeRemoved`) and `findBy*` queries. Defaults to 1000 ms. @@ -744,6 +744,22 @@ Default timeout, in ms, for async helper functions (`waitFor`, `waitForElementTo function resetToDefaults() {} ``` +### Environment variables + +#### `RNTL_SKIP_AUTO_CLEANUP` +Set to `true` to disable automatic `cleanup()` after each test. It works the same as importing `react-native-testing-library/dont-cleanup-after-each` or using `react-native-testing-library/pure`. + +```shell +$ RNTL_SKIP_AUTO_CLEANUP=true jest +``` + +#### `RNTL_SKIP_AUTO_DETECT_FAKE_TIMERS` +Set to `true` to disable auto-detection of fake timers. This might be useful in rare cases when you want to use non-Jest fake timers. See [issue #886](https://github.com/callstack/react-native-testing-library/issues/886) for more details. + +```shell +$ RNTL_SKIP_AUTO_DETECT_FAKE_TIMERS=true jest +``` + ## Accessibility ### `isInaccessible` diff --git a/website/docs/MigrationV2.md b/website/docs/MigrationV2.md index 3c59aafa3..c5a113fc8 100644 --- a/website/docs/MigrationV2.md +++ b/website/docs/MigrationV2.md @@ -26,7 +26,7 @@ This change might break your code, if you tests are not isolated, i.e. you call } ``` -- by setting `RTNL_SKIP_AUTO_CLEANUP` env variable to `true`. You can do this with `cross-evn` like this: +- by setting `RNTL_SKIP_AUTO_CLEANUP` env variable to `true`. You can do this with `cross-evn` like this: ```sh cross-env RNTL_SKIP_AUTO_CLEANUP=true jest