Skip to content

Commit 08351f7

Browse files
committed
test to show changes with modern timer mocks
1 parent b88aa2b commit 08351f7

File tree

3 files changed

+37
-7
lines changed

3 files changed

+37
-7
lines changed

src/__tests__/timerUtils.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { setTimeout } from '../helpers/getTimerFuncs';
2+
13
const FakeTimerTypes = [
24
'default',
35
'legacy',
@@ -18,4 +20,8 @@ function setupFakeTimers(fakeTimerType) {
1820
}
1921
}
2022

21-
export { FakeTimerTypes, setupFakeTimers };
23+
async function sleep(ms) {
24+
return new Promise((resolve) => setTimeout(resolve, ms));
25+
}
26+
27+
export { FakeTimerTypes, setupFakeTimers, sleep };

src/__tests__/waitFor.test.js

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
// @flow
22
import * as React from 'react';
3-
import { View, Text, TouchableOpacity } from 'react-native';
4-
import { render, fireEvent, waitFor } from '..';
5-
import { FakeTimerTypes, setupFakeTimers } from './timerUtils';
3+
import { Text, TouchableOpacity, View } from 'react-native';
4+
import { fireEvent, render, waitFor } from '..';
5+
import { FakeTimerTypes, setupFakeTimers, sleep } from './timerUtils';
66

77
class Banana extends React.Component<any> {
88
changeFresh = () => {
@@ -137,3 +137,30 @@ test.each(FakeTimerTypes)(
137137
expect(mockFn).toHaveBeenCalledTimes(3);
138138
}
139139
);
140+
141+
// this test leads to a console error: Warning: You called act(async () => ...) without await, but does not fail the test
142+
// it is included to show that the previous approach of faking modern timers still works
143+
// the gotcha is that the try catch will fail to catch the final error, which is why we need to stop throwing
144+
test('non-awaited approach is not affected by fake modern timers', async () => {
145+
jest.useFakeTimers('modern');
146+
147+
let calls = 0;
148+
const mockFn = jest.fn(() => {
149+
calls += 1;
150+
if (calls < 3) {
151+
throw Error('test');
152+
}
153+
});
154+
155+
try {
156+
waitFor(() => mockFn(), { timeout: 400, interval: 200 });
157+
} catch (e) {
158+
// suppress
159+
}
160+
jest.advanceTimersByTime(400);
161+
expect(mockFn).toHaveBeenCalledTimes(0);
162+
163+
jest.useRealTimers();
164+
await sleep(500);
165+
expect(mockFn).toHaveBeenCalledTimes(3);
166+
});

src/helpers/getTimerFuncs.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ function runWithRealTimers(callback) {
2121
}
2222

2323
function getJestFakeTimersType() {
24-
// istanbul ignore if
2524
if (
2625
typeof jest === 'undefined' ||
2726
typeof globalObj.setTimeout === 'undefined'
@@ -52,13 +51,11 @@ function getJestFakeTimersType() {
5251
}
5352

5453
// we only run our tests in node, and setImmediate is supported in node.
55-
// istanbul ignore next
5654
function setImmediatePolyfill(fn) {
5755
return globalObj.setTimeout(fn, 0);
5856
}
5957

6058
function getTimeFunctions() {
61-
// istanbul ignore next
6259
return {
6360
clearTimeoutFn: globalObj.clearTimeout,
6461
setImmediateFn: globalObj.setImmediate || setImmediatePolyfill,

0 commit comments

Comments
 (0)