Skip to content

Commit 8356d2c

Browse files
authored
Ignore fake timers (#1169)
1 parent 28aa750 commit 8356d2c

File tree

4 files changed

+30
-3
lines changed

4 files changed

+30
-3
lines changed

src/helpers/__tests__/timers.test.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { jestFakeTimersAreEnabled } from '../timers';
2+
describe('timers', () => {
3+
it('should not mock timers if RNTL_SKIP_AUTO_DETECT_FAKE_TIMERS is set', async () => {
4+
process.env.RNTL_SKIP_AUTO_DETECT_FAKE_TIMERS = 'true';
5+
jest.useFakeTimers();
6+
expect(jestFakeTimersAreEnabled()).toEqual(false);
7+
});
8+
});

src/helpers/timers.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ function getJestFakeTimersType(): FakeTimersTypes | null {
2525
// istanbul ignore if
2626
if (
2727
typeof jest === 'undefined' ||
28-
typeof globalObj.setTimeout === 'undefined'
28+
typeof globalObj.setTimeout === 'undefined' ||
29+
process.env.RNTL_SKIP_AUTO_DETECT_FAKE_TIMERS
2930
) {
3031
return null;
3132
}

website/docs/API.md

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ title: API
4949
- [`configure`](#configure)
5050
- [`asyncUtilTimeout` option](#asyncutiltimeout-option)
5151
- [`resetToDefaults()`](#resettodefaults)
52+
- [Environment variables](#environment-variables)
53+
- [`RNTL_SKIP_AUTO_CLEANUP`](#rntl_skip_auto_cleanup)
54+
- [`RNTL_SKIP_AUTO_DETECT_FAKE_TIMERS`](#rntl_skip_auto_detect_fake_timers)
5255
- [Accessibility](#accessibility)
5356
- [`isInaccessible`](#isinaccessible)
5457

@@ -730,7 +733,6 @@ type Config = {
730733

731734
function configure(options: Partial<Config>) {}
732735
```
733-
734736
#### `asyncUtilTimeout` option
735737

736738
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
742744
function resetToDefaults() {}
743745
```
744746

747+
### Environment variables
748+
749+
#### `RNTL_SKIP_AUTO_CLEANUP`
750+
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`.
751+
752+
```shell
753+
$ RNTL_SKIP_AUTO_CLEANUP=true jest
754+
```
755+
756+
#### `RNTL_SKIP_AUTO_DETECT_FAKE_TIMERS`
757+
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.
758+
759+
```shell
760+
$ RNTL_SKIP_AUTO_DETECT_FAKE_TIMERS=true jest
761+
```
762+
745763
## Accessibility
746764

747765
### `isInaccessible`

website/docs/MigrationV2.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ This change might break your code, if you tests are not isolated, i.e. you call
2626
}
2727
```
2828

29-
- by setting `RTNL_SKIP_AUTO_CLEANUP` env variable to `true`. You can do this with `cross-evn` like this:
29+
- by setting `RNTL_SKIP_AUTO_CLEANUP` env variable to `true`. You can do this with `cross-evn` like this:
3030

3131
```sh
3232
cross-env RNTL_SKIP_AUTO_CLEANUP=true jest

0 commit comments

Comments
 (0)