Skip to content

Ignore fake timers #1169

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Oct 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/helpers/__tests__/timers.test.ts
Original file line number Diff line number Diff line change
@@ -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);
});
});
3 changes: 2 additions & 1 deletion src/helpers/timers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
20 changes: 19 additions & 1 deletion website/docs/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -730,7 +733,6 @@ type Config = {

function configure(options: Partial<Config>) {}
```

#### `asyncUtilTimeout` option

Default timeout, in ms, for async helper functions (`waitFor`, `waitForElementToBeRemoved`) and `findBy*` queries. Defaults to 1000 ms.
Expand All @@ -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`
Expand Down
2 changes: 1 addition & 1 deletion website/docs/MigrationV2.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down