Skip to content

Commit 4163d7a

Browse files
authored
[7.x] [APM] Fix some warnings logged in APM tests (#52487) (#53473)
* [APM] Fix some warnings logged in APM tests (Seemingly) since the React upgrade in 439708a, our tests have started logging various warnings/errors to the console. The test suite is still passing but it creates a lot of noise. Changes: - use `act` or `wait` when appropriate - mock useFetcher calls - cleanup in useDelayedVisbility * Replace tick() with wait()
1 parent 2e97fff commit 4163d7a

File tree

8 files changed

+61
-26
lines changed

8 files changed

+61
-26
lines changed

x-pack/legacy/plugins/apm/public/components/app/TransactionOverview/__jest__/TransactionOverview.test.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import { history } from '../../../../utils/history';
1818
import { TransactionOverview } from '..';
1919
import { IUrlParams } from '../../../../context/UrlParamsContext/types';
2020
import * as useServiceTransactionTypesHook from '../../../../hooks/useServiceTransactionTypes';
21+
import * as useFetcherHook from '../../../../hooks/useFetcher';
2122
import { fromQuery } from '../../../shared/Links/url_helpers';
2223
import { Router } from 'react-router-dom';
2324
import { UrlParamsProvider } from '../../../../context/UrlParamsContext';
@@ -47,6 +48,8 @@ function setup({
4748
.spyOn(useServiceTransactionTypesHook, 'useServiceTransactionTypes')
4849
.mockReturnValue(serviceTransactionTypes);
4950

51+
jest.spyOn(useFetcherHook, 'useFetcher').mockReturnValue({} as any);
52+
5053
return render(
5154
<MockApmPluginContextWrapper>
5255
<Router history={history}>

x-pack/legacy/plugins/apm/public/components/shared/DatePicker/__test__/DatePicker.test.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@ import {
1010
UrlParamsContext,
1111
useUiFilters
1212
} from '../../../../context/UrlParamsContext';
13-
import { tick } from '../../../../utils/testHelpers';
1413
import { DatePicker } from '../index';
1514
import { IUrlParams } from '../../../../context/UrlParamsContext/types';
1615
import { history } from '../../../../utils/history';
1716
import { mount } from 'enzyme';
1817
import { EuiSuperDatePicker } from '@elastic/eui';
1918
import { MemoryRouter } from 'react-router-dom';
19+
import { wait } from '@testing-library/react';
2020

2121
const mockHistoryPush = jest.spyOn(history, 'push');
2222
const mockRefreshTimeRange = jest.fn();
@@ -84,7 +84,7 @@ describe('DatePicker', () => {
8484
});
8585
expect(mockRefreshTimeRange).not.toHaveBeenCalled();
8686
jest.advanceTimersByTime(1000);
87-
await tick();
87+
await wait();
8888
expect(mockRefreshTimeRange).toHaveBeenCalled();
8989
wrapper.unmount();
9090
});
@@ -94,7 +94,7 @@ describe('DatePicker', () => {
9494
mountDatePicker({ refreshPaused: true, refreshInterval: 1000 });
9595
expect(mockRefreshTimeRange).not.toHaveBeenCalled();
9696
jest.advanceTimersByTime(1000);
97-
await tick();
97+
await wait();
9898
expect(mockRefreshTimeRange).not.toHaveBeenCalled();
9999
});
100100
});

x-pack/legacy/plugins/apm/public/components/shared/useDelayedVisibility/Delayed/index.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,4 +57,10 @@ export class Delayed {
5757
public onChange(onChangeCallback: Callback) {
5858
this.onChangeCallback = onChangeCallback;
5959
}
60+
61+
public destroy() {
62+
if (this.timeoutId) {
63+
window.clearTimeout(this.timeoutId);
64+
}
65+
}
6066
}

x-pack/legacy/plugins/apm/public/components/shared/useDelayedVisibility/index.test.tsx

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,16 @@
44
* you may not use this file except in compliance with the Elastic License.
55
*/
66

7-
import { renderHook } from '@testing-library/react-hooks';
7+
import {
8+
renderHook,
9+
act,
10+
RenderHookResult
11+
} from '@testing-library/react-hooks';
812
import { useDelayedVisibility } from '.';
913

1014
describe('useFetcher', () => {
11-
let hook;
15+
let hook: RenderHookResult<any, any>;
16+
1217
beforeEach(() => {
1318
jest.useFakeTimers();
1419
});
@@ -26,9 +31,15 @@ describe('useFetcher', () => {
2631
});
2732

2833
hook.rerender(true);
29-
jest.advanceTimersByTime(10);
34+
act(() => {
35+
jest.advanceTimersByTime(10);
36+
});
37+
3038
expect(hook.result.current).toEqual(false);
31-
jest.advanceTimersByTime(50);
39+
act(() => {
40+
jest.advanceTimersByTime(50);
41+
});
42+
3243
expect(hook.result.current).toEqual(true);
3344
});
3445

@@ -38,8 +49,11 @@ describe('useFetcher', () => {
3849
});
3950

4051
hook.rerender(true);
41-
jest.advanceTimersByTime(100);
52+
act(() => {
53+
jest.advanceTimersByTime(100);
54+
});
4255
hook.rerender(false);
56+
4357
expect(hook.result.current).toEqual(true);
4458
});
4559

@@ -49,11 +63,22 @@ describe('useFetcher', () => {
4963
});
5064

5165
hook.rerender(true);
52-
jest.advanceTimersByTime(100);
66+
67+
act(() => {
68+
jest.advanceTimersByTime(100);
69+
});
70+
5371
hook.rerender(false);
54-
jest.advanceTimersByTime(900);
72+
act(() => {
73+
jest.advanceTimersByTime(900);
74+
});
75+
5576
expect(hook.result.current).toEqual(true);
56-
jest.advanceTimersByTime(100);
77+
78+
act(() => {
79+
jest.advanceTimersByTime(100);
80+
});
81+
5782
expect(hook.result.current).toEqual(false);
5883
});
5984
});

x-pack/legacy/plugins/apm/public/components/shared/useDelayedVisibility/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ export function useDelayedVisibility(
2626
setIsVisible(visibility);
2727
});
2828
delayedRef.current = delayed;
29+
30+
return () => {
31+
delayed.destroy();
32+
};
2933
}, [hideDelayMs, showDelayMs, minimumVisibleDuration]);
3034

3135
useEffect(() => {

x-pack/legacy/plugins/apm/public/context/UrlParamsContext/__tests__/UrlParamsContext.test.tsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ import { Location, History } from 'history';
1111
import { MemoryRouter, Router } from 'react-router-dom';
1212
import moment from 'moment-timezone';
1313
import { IUrlParams } from '../types';
14-
import { tick } from '../../../utils/testHelpers';
1514
import { getParsedDate } from '../helpers';
15+
import { wait } from '@testing-library/react';
1616

1717
function mountParams(location: Location) {
1818
return mount(
@@ -143,13 +143,13 @@ describe('UrlParamsContext', () => {
143143
</Router>
144144
);
145145

146-
await tick();
146+
await wait();
147147

148148
expect(calls.length).toBe(1);
149149

150150
wrapper.find('button').simulate('click');
151151

152-
await tick();
152+
await wait();
153153

154154
expect(calls.length).toBe(2);
155155

@@ -194,11 +194,11 @@ describe('UrlParamsContext', () => {
194194
</Router>
195195
);
196196

197-
await tick();
197+
await wait();
198198

199199
wrapper.find('button').simulate('click');
200200

201-
await tick();
201+
await wait();
202202

203203
const params = getDataFromOutput(wrapper);
204204
expect(params.start).toEqual('2000-06-14T00:00:00.000Z');

x-pack/legacy/plugins/apm/public/hooks/useFetcher.integration.test.tsx

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
* you may not use this file except in compliance with the Elastic License.
55
*/
66

7-
import { render } from '@testing-library/react';
87
import React from 'react';
9-
import { delay, MockApmPluginContextWrapper, tick } from '../utils/testHelpers';
8+
import { render, wait } from '@testing-library/react';
9+
import { delay, MockApmPluginContextWrapper } from '../utils/testHelpers';
1010
import { useFetcher } from './useFetcher';
1111

1212
const wrapper = MockApmPluginContextWrapper;
@@ -63,7 +63,8 @@ describe('when simulating race condition', () => {
6363

6464
it('should render "Hello from Peter" after 200ms', async () => {
6565
jest.advanceTimersByTime(200);
66-
await tick();
66+
67+
await wait();
6768

6869
expect(renderSpy).lastCalledWith({
6970
data: 'Hello from Peter',
@@ -74,7 +75,7 @@ describe('when simulating race condition', () => {
7475

7576
it('should render "Hello from Peter" after 600ms', async () => {
7677
jest.advanceTimersByTime(600);
77-
await tick();
78+
await wait();
7879

7980
expect(renderSpy).lastCalledWith({
8081
data: 'Hello from Peter',
@@ -85,7 +86,7 @@ describe('when simulating race condition', () => {
8586

8687
it('should should NOT have rendered "Hello from John" at any point', async () => {
8788
jest.advanceTimersByTime(600);
88-
await tick();
89+
await wait();
8990

9091
expect(renderSpy).not.toHaveBeenCalledWith({
9192
data: 'Hello from John',
@@ -96,7 +97,7 @@ describe('when simulating race condition', () => {
9697

9798
it('should send and receive calls in the right order', async () => {
9899
jest.advanceTimersByTime(600);
99-
await tick();
100+
await wait();
100101

101102
expect(requestCallOrder).toEqual([
102103
['request', 'John', 500],

x-pack/legacy/plugins/apm/public/utils/testHelpers.tsx

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@ export async function getRenderedHref(Component: React.FC, location: Location) {
6565
</MockApmPluginContextWrapper>
6666
);
6767

68-
await tick();
6968
await waitForElement(() => el.container.querySelector('a'));
7069

7170
const a = el.container.querySelector('a');
@@ -81,9 +80,6 @@ export function delay(ms: number) {
8180
return new Promise(resolve => setTimeout(resolve, ms));
8281
}
8382

84-
// Await this when you need to "flush" promises to immediately resolve or throw in tests
85-
export const tick = () => new Promise(resolve => setImmediate(resolve, 0));
86-
8783
export function expectTextsNotInDocument(output: any, texts: string[]) {
8884
texts.forEach(text => {
8985
try {

0 commit comments

Comments
 (0)