Skip to content

Commit

Permalink
Move flushPromises into testUtils file to avoid repeated mocks during…
Browse files Browse the repository at this point in the history
… import #1379
  • Loading branch information
joelvdavies committed Mar 19, 2024
1 parent 1c5ef55 commit 38139cc
Show file tree
Hide file tree
Showing 8 changed files with 8 additions and 9 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -125,4 +125,4 @@
"wait-on": "7.2.0"
},
"packageManager": "[email protected]"
}
}
2 changes: 1 addition & 1 deletion src/App.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { act, fireEvent, render, screen } from '@testing-library/react';
import axios from 'axios';
import { createRoot } from 'react-dom/client';
import App, { AppSansHoc } from './App';
import { flushPromises } from './setupTests';
import { flushPromises } from './testUtils';
import { RegisterRouteType } from './state/scigateway.types';

vi.mock('./state/actions/loadMicroFrontends', () => ({
Expand Down
2 changes: 1 addition & 1 deletion src/routing/authorisedRoute.component.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
siteLoadingUpdate,
verifyUsernameAndPassword,
} from '../state/actions/scigateway.actions';
import { flushPromises } from '../setupTests';
import { flushPromises } from '../testUtils';
import { Provider } from 'react-redux';
import {
act,
Expand Down
3 changes: 0 additions & 3 deletions src/setupTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,6 @@ Object.defineProperty(window, 'matchMedia', {
})),
});

export const flushPromises = async (): Promise<unknown> =>
(await vi.importActual('timers')).setImmediate;

// globally mock as we never want to actually call single-spa funcs in unit tests
vi.mock('single-spa', () => ({
unloadApplication: vi.fn(),
Expand Down
2 changes: 1 addition & 1 deletion src/state/actions/scigateway.actions.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ import { StateType } from '../state.types';
import loadMicroFrontends from './loadMicroFrontends';
import log from 'loglevel';
import { createLocation } from 'history';
import { flushPromises } from '../../setupTests';
import { flushPromises } from '../../testUtils';

function mockAxiosGetResponse(message: string): void {
vi.mocked(mockAxios.get).mockImplementationOnce(() =>
Expand Down
2 changes: 1 addition & 1 deletion src/state/middleware/scigateway.middleware.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import { toastr } from 'react-redux-toastr';
import { AddHelpTourStepsType } from '../scigateway.types';
import { StateType } from '../state.types';
import TestAuthProvider from '../../authentication/testAuthProvider';
import { flushPromises } from '../../setupTests';
import { flushPromises } from '../../testUtils';
import { authState, initialState } from '../reducers/scigateway.reducer';
import { buildTheme } from '../../theming';
import { thunk } from 'redux-thunk';
Expand Down
2 changes: 2 additions & 0 deletions src/testUtils.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export const flushPromises = async (): Promise<unknown> =>
(await vi.importActual('timers')).setImmediate;
2 changes: 1 addition & 1 deletion tsconfig.build.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"extends": "./tsconfig.base.json",
// Exclude test files from build
"exclude": ["**/?*test.*", "src/setupTests.ts"],
"exclude": ["**/?*test.*", "src/setupTests.ts", "src/testUtils.tsx"],
}

0 comments on commit 38139cc

Please sign in to comment.