Skip to content

Commit

Permalink
test: [M3-7330] - Attempt to fix hanging unit tests (linode#10591)
Browse files Browse the repository at this point in the history
* Use React Query infinity cache time for unit tests

* Improve event test stability by mocking the current system time when comparing relative date strings

* Apply `pool: forks` option to eliminate hanging

* Added changeset: Fix hanging unit tests
  • Loading branch information
jdamore-linode authored Jun 18, 2024
1 parent 1f2d666 commit 5ec87a1
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 7 deletions.
5 changes: 5 additions & 0 deletions packages/manager/.changeset/pr-10591-tests-1718746383365.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@linode/manager": Tests
---

Fix hanging unit tests ([#10591](https://github.com/linode/manager/pull/10591))
9 changes: 9 additions & 0 deletions packages/manager/src/features/Events/utils.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
} from './utils';

import type { Event } from '@linode/api-v4';
import { DateTime } from 'luxon';

describe('getEventMessage', () => {
const mockEvent1: Event = eventFactory.build({
Expand Down Expand Up @@ -126,6 +127,10 @@ describe('formatProgressEvent', () => {
});

it('returns the correct format for a finished Event', () => {
const currentDateMock = DateTime.fromISO(mockEvent1.created).plus({
seconds: 1,
});
vi.setSystemTime(currentDateMock.toJSDate());
const { progressEventDisplay, showProgress } = formatProgressEvent(
mockEvent1
);
Expand All @@ -135,6 +140,10 @@ describe('formatProgressEvent', () => {
});

it('returns the correct format for a "started" event without time remaining info', () => {
const currentDateMock = DateTime.fromISO(mockEvent2.created).plus({
seconds: 1,
});
vi.setSystemTime(currentDateMock.toJSDate());
const { progressEventDisplay, showProgress } = formatProgressEvent(
mockEvent2
);
Expand Down
5 changes: 1 addition & 4 deletions packages/manager/src/hooks/useOrder.test.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import { QueryClient } from '@tanstack/react-query';
import { act, renderHook, waitFor } from '@testing-library/react';

import { HttpResponse, http, server } from 'src/mocks/testServer';
import { queryClientFactory } from 'src/queries/base';
import { usePreferences } from 'src/queries/profile/preferences';
Expand Down Expand Up @@ -77,8 +75,7 @@ describe('useOrder hook', () => {
});

it('use preferences are used when there are no query params', async () => {
const queryClient = new QueryClient();

const queryClient = queryClientFactory();
server.use(
http.get('*/profile/preferences', () => {
return HttpResponse.json({
Expand Down
2 changes: 1 addition & 1 deletion packages/manager/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import './index.css';
import { LinodeThemeWrapper } from './LinodeThemeWrapper';
import { queryClientFactory } from './queries/base';

const queryClient = queryClientFactory();
const queryClient = queryClientFactory('longLived');
const store = storeFactory();

setupInterceptors(store);
Expand Down
17 changes: 15 additions & 2 deletions packages/manager/src/queries/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,22 @@ export const queryPresets = {
},
};

export const queryClientFactory = () => {
/**
* Creates and returns a new TanStack Query query client instance.
*
* Allows the query client behavior to be configured by specifying a preset. The
* 'longLived' preset is most suitable for production use, while 'oneTimeFetch' is
* preferred for tests.
*
* @param preset - Optional query preset for client. Either 'longLived' or 'oneTimeFetch'.
*
* @returns New `QueryClient` instance.
*/
export const queryClientFactory = (
preset: 'longLived' | 'oneTimeFetch' = 'oneTimeFetch'
) => {
return new QueryClient({
defaultOptions: { queries: queryPresets.longLived },
defaultOptions: { queries: queryPresets[preset] },
});
};

Expand Down
1 change: 1 addition & 0 deletions packages/manager/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export default defineConfig({
'src/**/*.utils.{js,jsx,ts,tsx}',
],
},
pool: 'forks',
environment: 'jsdom',
globals: true,
setupFiles: './src/testSetup.ts',
Expand Down

0 comments on commit 5ec87a1

Please sign in to comment.