diff --git a/src/helpers/__tests__/timers.test.ts b/src/helpers/__tests__/timers.test.ts new file mode 100644 index 000000000..13c099abe --- /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_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 579cf3755..06646be7f 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_SKIP_AUTO_DETECT_FAKE_TIMERS ) { return null; } diff --git a/website/docs/API.md b/website/docs/API.md index 6d8adb34f..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,7 +733,6 @@ type Config = { function configure(options: Partial) {} ``` - #### `asyncUtilTimeout` option Default timeout, in ms, for async helper functions (`waitFor`, `waitForElementToBeRemoved`) and `findBy*` queries. Defaults to 1000 ms. @@ -742,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