Skip to content
This repository was archived by the owner on Aug 5, 2025. It is now read-only.

Commit 96809c3

Browse files
authored
Merge pull request #560 from lifeomic/fix-failing-async-tests
Fix Flaky Async Tests
2 parents 9ef1ade + eaee15b commit 96809c3

3 files changed

Lines changed: 55 additions & 49 deletions

File tree

src/hooks/Circles/useJoinCircles.test.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ const queryClient = new QueryClient({
1717
},
1818
});
1919

20-
const renderHookInContext = async () => {
20+
const renderHookInContext = () => {
2121
return renderHook(() => useJoinCircles(), {
2222
wrapper: ({ children }) => (
2323
<QueryClientProvider client={queryClient}>{children}</QueryClientProvider>
@@ -92,7 +92,7 @@ test('joins circles listed in app-config', async () => {
9292
axiosMock
9393
.onPatch('/v1/life-research/projects/projectId/app-config/circles')
9494
.reply(200, {});
95-
const { result } = await renderHookInContext();
95+
const { result } = renderHookInContext();
9696
await waitFor(() => expect(result.current.isSuccess).toBeDefined());
9797
expect(axiosMock.history.patch[0].url).toBe(
9898
'/v1/life-research/projects/projectId/app-config/circles',
@@ -117,7 +117,7 @@ test('does nothing if all circles are joined', async () => {
117117
},
118118
});
119119
axiosMock.reset();
120-
const { result } = await renderHookInContext();
120+
const { result } = renderHookInContext();
121121
await waitFor(() => expect(result.current.isSuccess).toBeDefined());
122122
await waitFor(() => expect(axiosMock.history.patch.length).toEqual(0));
123123
});

src/hooks/Circles/usePrivatePost.test.tsx

Lines changed: 36 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -111,44 +111,52 @@ describe('useCreatePrivatePostAttachmentMutation', () => {
111111
});
112112

113113
test('throws on missing asset type', async () => {
114-
expect.assertions(1);
115114
const { result } = renderHookWithInjectedClient();
116115
const { mutateAsync } = result.current;
117116

118-
await act(async () => {
119-
try {
120-
await mutateAsync({
121-
asset: {
122-
id: 'assetId',
123-
type: undefined, // purpose of the test
124-
uri: 'https://some-image-url',
125-
},
126-
userIds: ['user1', 'user2'],
127-
});
128-
} catch (e) {
129-
expect(e).toEqual(new Error("Unknown asset type, can't upload"));
130-
}
117+
let exception: Error | undefined;
118+
119+
act(() => {
120+
mutateAsync({
121+
asset: {
122+
id: 'assetId',
123+
type: undefined, // purpose of the test
124+
uri: 'https://some-image-url',
125+
},
126+
userIds: ['user1', 'user2'],
127+
}).catch((e) => {
128+
exception = e;
129+
});
130+
});
131+
132+
await waitFor(() => {
133+
expect(exception).toEqual(new Error("Unknown asset type, can't upload"));
131134
});
132135
});
133136

134137
test('throws on unknown asset type', async () => {
135-
expect.assertions(1);
136138
const { result } = renderHookWithInjectedClient();
137139
const { mutateAsync } = result.current;
138140

139-
await act(async () => {
140-
try {
141-
await mutateAsync({
142-
asset: {
143-
id: 'assetId',
144-
type: 'application/pdf', // purpose of the test
145-
uri: 'https://some-image-url',
146-
},
147-
userIds: ['user1', 'user2'],
148-
});
149-
} catch (e) {
150-
expect(e).toEqual(new Error('Unsupported file type: application/pdf'));
151-
}
141+
let exception: Error | undefined;
142+
143+
act(() => {
144+
mutateAsync({
145+
asset: {
146+
id: 'assetId',
147+
type: 'application/pdf', // purpose of the test
148+
uri: 'https://some-image-url',
149+
},
150+
userIds: ['user1', 'user2'],
151+
}).catch((e) => {
152+
exception = e;
153+
});
154+
});
155+
156+
await waitFor(() => {
157+
expect(exception).toEqual(
158+
new Error('Unsupported file type: application/pdf'),
159+
);
152160
});
153161
});
154162
});

src/hooks/useFhirClient.test.tsx

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -384,10 +384,13 @@ describe('useCreateResourceMutation', () => {
384384
value: 73.5,
385385
},
386386
};
387-
await act(async () => {
388-
await result.current.mutateAsync(observation);
387+
act(() => {
388+
result.current.mutateAsync(observation);
389389
});
390390

391+
await waitFor(() => {
392+
expect(result.current.data).toEqual(resultBundle);
393+
});
391394
expect(axiosMock.history.post[0].url).toBe('/v1/fhir/dstu3/Observation');
392395
expect(JSON.parse(axiosMock.history.post[0].data)).toEqual({
393396
...observation,
@@ -403,9 +406,6 @@ describe('useCreateResourceMutation', () => {
403406
],
404407
},
405408
});
406-
await waitFor(() => {
407-
expect(result.current.data).toEqual(resultBundle);
408-
});
409409
});
410410
});
411411

@@ -462,10 +462,13 @@ describe('useUpsertMutation', () => {
462462
value: 1.8,
463463
},
464464
};
465-
await act(async () => {
466-
await result.current.mutateAsync([observation1, observation2]);
465+
act(() => {
466+
result.current.mutateAsync([observation1, observation2]);
467467
});
468468

469+
await waitFor(() => {
470+
expect(result.current.data).toEqual(resultBundle);
471+
});
469472
expect(axiosMock.history.post[0].url).toBe(
470473
'https://fhir.us.lifeomic.com/acct1/dstu3',
471474
);
@@ -511,9 +514,6 @@ describe('useUpsertMutation', () => {
511514
},
512515
],
513516
});
514-
await waitFor(() => {
515-
expect(result.current.data).toEqual(resultBundle);
516-
});
517517
});
518518
});
519519

@@ -529,19 +529,17 @@ describe('useDeleteResourceMutation', () => {
529529
return useDeleteResourceMutation();
530530
}
531531
const { result } = renderHookInContext(useTestHook);
532-
await act(async () => {
533-
await result.current.mutateAsync({
532+
act(() => {
533+
result.current.mutateAsync({
534534
id: observationId,
535535
resourceType: 'Observation',
536536
});
537537
});
538538

539-
await act(async () => {
540-
await waitFor(() => {
541-
expect(axiosMock.history.delete[0].url).toBe(
542-
`/v1/fhir/dstu3/Observation/${observationId}`,
543-
);
544-
});
539+
await waitFor(() => {
540+
expect(axiosMock.history.delete[0].url).toBe(
541+
`/v1/fhir/dstu3/Observation/${observationId}`,
542+
);
545543
});
546544
});
547545
});

0 commit comments

Comments
 (0)