Skip to content

Commit f46ba87

Browse files
committed
refactor: pass useFakeTimer config in tests
1 parent 814ec26 commit f46ba87

File tree

4 files changed

+34
-45
lines changed

4 files changed

+34
-45
lines changed

src/__tests__/auto-cleanup.test.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,13 @@ test('component is automatically umounted after first test ends', () => {
3838
});
3939

4040
test('does not time out with legacy fake timers', () => {
41-
jest.useFakeTimers('legacy');
41+
jest.useFakeTimers({ legacyFakeTimers: true });
4242
render(<Test />);
4343
expect(isMounted).toEqual(true);
4444
});
4545

4646
test('does not time out with fake timers', () => {
47-
jest.useFakeTimers('modern');
47+
jest.useFakeTimers();
4848
render(<Test />);
4949
expect(isMounted).toEqual(true);
5050
});

src/__tests__/timers.test.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
import waitFor from '../waitFor';
2-
import { TimerMode } from './timerUtils';
32

4-
describe.each([TimerMode.Legacy, TimerMode.Modern])(
5-
'%s fake timers tests',
6-
(fakeTimerType) => {
3+
describe.each([false, true])(
4+
'fake timers tests (legacyFakeTimers = %s)',
5+
(legacyFakeTimers) => {
76
beforeEach(() => {
8-
jest.useFakeTimers(fakeTimerType);
7+
jest.useFakeTimers({ legacyFakeTimers });
98
});
109

1110
test('it successfully runs tests', () => {

src/__tests__/waitFor.test.tsx

Lines changed: 24 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -79,36 +79,26 @@ test('waits for element with custom interval', async () => {
7979
expect(mockFn).toHaveBeenCalledTimes(2);
8080
});
8181

82-
test('waits for element until it stops throwing using modern fake timers', async () => {
83-
jest.useFakeTimers('modern');
84-
const { getByText, queryByText } = render(<BananaContainer />);
85-
86-
fireEvent.press(getByText('Change freshness!'));
87-
expect(queryByText('Fresh')).toBeNull();
88-
89-
jest.advanceTimersByTime(300);
90-
const freshBananaText = await waitFor(() => getByText('Fresh'));
91-
92-
expect(freshBananaText.props.children).toBe('Fresh');
93-
});
82+
test.each([false, true])(
83+
'waits for element until it stops throwing using fake timers (legacyFakeTimers = %s)',
84+
async (legacyFakeTimers) => {
85+
jest.useFakeTimers({ legacyFakeTimers });
86+
const { getByText, queryByText } = render(<BananaContainer />);
9487

95-
test('waits for element until it stops throwing using legacy fake timers', async () => {
96-
jest.useFakeTimers('legacy');
97-
const { getByText, queryByText } = render(<BananaContainer />);
88+
fireEvent.press(getByText('Change freshness!'));
89+
expect(queryByText('Fresh')).toBeNull();
9890

99-
fireEvent.press(getByText('Change freshness!'));
100-
expect(queryByText('Fresh')).toBeNull();
101-
102-
jest.advanceTimersByTime(300);
103-
const freshBananaText = await waitFor(() => getByText('Fresh'));
91+
jest.advanceTimersByTime(300);
92+
const freshBananaText = await waitFor(() => getByText('Fresh'));
10493

105-
expect(freshBananaText.props.children).toBe('Fresh');
106-
});
94+
expect(freshBananaText.props.children).toBe('Fresh');
95+
}
96+
);
10797

108-
test.each([TimerMode.Legacy, TimerMode.Modern])(
109-
'waits for assertion until timeout is met with %s fake timers',
110-
async (fakeTimerType) => {
111-
jest.useFakeTimers(fakeTimerType);
98+
test.each([false, true])(
99+
'waits for assertion until timeout is met with fake timers (legacyFakeTimers = %s)',
100+
async (legacyFakeTimers) => {
101+
jest.useFakeTimers({ legacyFakeTimers });
112102

113103
const mockFn = jest.fn(() => {
114104
throw Error('test');
@@ -124,10 +114,10 @@ test.each([TimerMode.Legacy, TimerMode.Modern])(
124114
}
125115
);
126116

127-
test.each([TimerMode.Legacy, TimerMode.Modern])(
128-
'waits for assertion until timeout is met with %s fake timers',
129-
async (fakeTimerType) => {
130-
jest.useFakeTimers(fakeTimerType);
117+
test.each([false, true])(
118+
'waits for assertion until timeout is met with fake timers (legacyFakeTimers = %s)',
119+
async (legacyFakeTimers) => {
120+
jest.useFakeTimers({ legacyFakeTimers });
131121

132122
const mockErrorFn = jest.fn(() => {
133123
throw Error('test');
@@ -150,10 +140,10 @@ test.each([TimerMode.Legacy, TimerMode.Modern])(
150140
}
151141
);
152142

153-
test.each([TimerMode.Legacy, TimerMode.Legacy])(
154-
'awaiting something that succeeds before timeout works with %s fake timers',
155-
async (fakeTimerType) => {
156-
jest.useFakeTimers(fakeTimerType);
143+
test.each([false, true])(
144+
'awaiting something that succeeds before timeout works with fake timers (legacyFakeTimers = %s)',
145+
async (legacyFakeTimers) => {
146+
jest.useFakeTimers({ legacyFakeTimers });
157147

158148
let calls = 0;
159149
const mockFn = jest.fn(() => {

src/__tests__/waitForElementToBeRemoved.test.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -130,10 +130,10 @@ test('waits with custom interval', async () => {
130130
expect(mockFn).toHaveBeenCalledTimes(4);
131131
});
132132

133-
test.each([TimerMode.Legacy, TimerMode.Modern])(
134-
'works with %s fake timers',
135-
async (fakeTimerType) => {
136-
jest.useFakeTimers(fakeTimerType);
133+
test.each([false, true])(
134+
'works with fake timers (legacyFakeTimers = %s)',
135+
async (legacyFakeTimers) => {
136+
jest.useFakeTimers({ legacyFakeTimers });
137137

138138
const mockFn = jest.fn(() => <View />);
139139

0 commit comments

Comments
 (0)