Skip to content

Commit 37379e0

Browse files
lobsterkatieAbhiPrasad
authored andcommitted
chore(tests): Fix react act warning in tests (#5113)
When running tests, React will throw a warning unless you surround state changes with `act(() => <state change>)`[1]. This does that everywhere necessary to get rid of the warning. [1] https://reactjs.org/docs/test-utils.html#act
1 parent 0c518f5 commit 37379e0

File tree

3 files changed

+49
-17
lines changed

3 files changed

+49
-17
lines changed

packages/react/test/reactrouterv3.test.tsx

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,15 +58,19 @@ describe('React Router V3', () => {
5858
instrumentation(mockStartTransaction);
5959
render(<Router history={history}>{routes}</Router>);
6060

61-
history.push('/about');
61+
act(() => {
62+
history.push('/about');
63+
});
6264
expect(mockStartTransaction).toHaveBeenCalledTimes(2);
6365
expect(mockStartTransaction).toHaveBeenLastCalledWith({
6466
name: '/about',
6567
op: 'navigation',
6668
tags: { from: '/', 'routing.instrumentation': 'react-router-v3' },
6769
});
6870

69-
history.push('/features');
71+
act(() => {
72+
history.push('/features');
73+
});
7074
expect(mockStartTransaction).toHaveBeenCalledTimes(3);
7175
expect(mockStartTransaction).toHaveBeenLastCalledWith({
7276
name: '/features',
@@ -87,7 +91,9 @@ describe('React Router V3', () => {
8791
instrumentation(mockStartTransaction);
8892
render(<Router history={history}>{routes}</Router>);
8993

90-
history.replace('hello');
94+
act(() => {
95+
history.replace('hello');
96+
});
9197
expect(mockStartTransaction).toHaveBeenCalledTimes(1);
9298
});
9399

@@ -98,7 +104,9 @@ describe('React Router V3', () => {
98104
render(<Router history={history}>{routes}</Router>);
99105
expect(mockStartTransaction).toHaveBeenCalledTimes(1);
100106

101-
history.push('/features');
107+
act(() => {
108+
history.push('/features');
109+
});
102110
expect(mockFinish).toHaveBeenCalledTimes(1);
103111
expect(mockStartTransaction).toHaveBeenCalledTimes(2);
104112
});

packages/react/test/reactrouterv4.test.tsx

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,15 +58,19 @@ describe('React Router v4', () => {
5858
</Router>,
5959
);
6060

61-
history.push('/about');
61+
act(() => {
62+
history.push('/about');
63+
});
6264
expect(mockStartTransaction).toHaveBeenCalledTimes(2);
6365
expect(mockStartTransaction).toHaveBeenLastCalledWith({
6466
name: '/about',
6567
op: 'navigation',
6668
tags: { 'routing.instrumentation': 'react-router-v4' },
6769
});
6870

69-
history.push('/features');
71+
act(() => {
72+
history.push('/features');
73+
});
7074
expect(mockStartTransaction).toHaveBeenCalledTimes(3);
7175
expect(mockStartTransaction).toHaveBeenLastCalledWith({
7276
name: '/features',
@@ -101,7 +105,9 @@ describe('React Router v4', () => {
101105
</Router>,
102106
);
103107

104-
history.replace('hello');
108+
act(() => {
109+
history.replace('hello');
110+
});
105111
expect(mockStartTransaction).toHaveBeenCalledTimes(1);
106112
});
107113

@@ -118,7 +124,9 @@ describe('React Router v4', () => {
118124
);
119125
expect(mockStartTransaction).toHaveBeenCalledTimes(1);
120126

121-
history.push('/features');
127+
act(() => {
128+
history.push('/features');
129+
});
122130
expect(mockFinish).toHaveBeenCalledTimes(1);
123131
expect(mockStartTransaction).toHaveBeenCalledTimes(2);
124132
});
@@ -237,15 +245,19 @@ describe('React Router v4', () => {
237245
</Router>,
238246
);
239247

240-
history.push('/organizations/1234/v1/758');
248+
act(() => {
249+
history.push('/organizations/1234/v1/758');
250+
});
241251
expect(mockStartTransaction).toHaveBeenCalledTimes(2);
242252
expect(mockStartTransaction).toHaveBeenLastCalledWith({
243253
name: '/organizations/:orgid/v1/:teamid',
244254
op: 'navigation',
245255
tags: { 'routing.instrumentation': 'react-router-v4' },
246256
});
247257

248-
history.push('/organizations/1234');
258+
act(() => {
259+
history.push('/organizations/1234');
260+
});
249261
expect(mockStartTransaction).toHaveBeenCalledTimes(3);
250262
expect(mockStartTransaction).toHaveBeenLastCalledWith({
251263
name: '/organizations/:orgid',

packages/react/test/reactrouterv5.test.tsx

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { act,render } from '@testing-library/react';
1+
import { act, render } from '@testing-library/react';
22
import { createMemoryHistory } from 'history-4';
33
import * as React from 'react';
44
import { matchPath, Route, Router, Switch } from 'react-router-5';
@@ -58,15 +58,19 @@ describe('React Router v5', () => {
5858
</Router>,
5959
);
6060

61-
history.push('/about');
61+
act(() => {
62+
history.push('/about');
63+
});
6264
expect(mockStartTransaction).toHaveBeenCalledTimes(2);
6365
expect(mockStartTransaction).toHaveBeenLastCalledWith({
6466
name: '/about',
6567
op: 'navigation',
6668
tags: { 'routing.instrumentation': 'react-router-v5' },
6769
});
6870

69-
history.push('/features');
71+
act(() => {
72+
history.push('/features');
73+
});
7074
expect(mockStartTransaction).toHaveBeenCalledTimes(3);
7175
expect(mockStartTransaction).toHaveBeenLastCalledWith({
7276
name: '/features',
@@ -101,7 +105,9 @@ describe('React Router v5', () => {
101105
</Router>,
102106
);
103107

104-
history.replace('hello');
108+
act(() => {
109+
history.replace('hello');
110+
});
105111
expect(mockStartTransaction).toHaveBeenCalledTimes(1);
106112
});
107113

@@ -118,7 +124,9 @@ describe('React Router v5', () => {
118124
);
119125
expect(mockStartTransaction).toHaveBeenCalledTimes(1);
120126

121-
history.push('/features');
127+
act(() => {
128+
history.push('/features');
129+
});
122130
expect(mockFinish).toHaveBeenCalledTimes(1);
123131
expect(mockStartTransaction).toHaveBeenCalledTimes(2);
124132
});
@@ -238,15 +246,19 @@ describe('React Router v5', () => {
238246
</Router>,
239247
);
240248

241-
history.push('/organizations/1234/v1/758');
249+
act(() => {
250+
history.push('/organizations/1234/v1/758');
251+
});
242252
expect(mockStartTransaction).toHaveBeenCalledTimes(2);
243253
expect(mockStartTransaction).toHaveBeenLastCalledWith({
244254
name: '/organizations/:orgid/v1/:teamid',
245255
op: 'navigation',
246256
tags: { 'routing.instrumentation': 'react-router-v5' },
247257
});
248258

249-
history.push('/organizations/1234');
259+
act(() => {
260+
history.push('/organizations/1234');
261+
});
250262
expect(mockStartTransaction).toHaveBeenCalledTimes(3);
251263
expect(mockStartTransaction).toHaveBeenLastCalledWith({
252264
name: '/organizations/:orgid',

0 commit comments

Comments
 (0)