Skip to content

Commit

Permalink
Update tests broken by vitest v3
Browse files Browse the repository at this point in the history
- mainly explicitly stating which timer funcs to fake via useFakeTimers
  • Loading branch information
louise-davies committed Feb 17, 2025
1 parent 044787e commit 13857e3
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 8 deletions.
12 changes: 9 additions & 3 deletions src/api/records.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,9 @@ describe('records api functions', () => {
});

it('sends request to fetch records, returns successful response and uses a select function to format the results', async () => {
vi.useFakeTimers().setSystemTime(new Date('2024-07-02 12:00:00'));
vi.useFakeTimers({ toFake: ['Date'] }).setSystemTime(
new Date('2024-07-02 12:00:00')
);

const pendingRequest = waitForRequest('GET', '/records');

Expand Down Expand Up @@ -557,7 +559,9 @@ describe('records api functions', () => {
});

it('uses a select function to format the results', async () => {
vi.useFakeTimers().setSystemTime(new Date('2024-07-02 12:00:00'));
vi.useFakeTimers({ toFake: ['Date'] }).setSystemTime(
new Date('2024-07-02 12:00:00')
);

const pendingRequest = waitForRequest('GET', '/records');

Expand Down Expand Up @@ -784,7 +788,9 @@ describe('records api functions', () => {
});

it('sends request to fetch records with a projection and returns successful response', async () => {
vi.useFakeTimers().setSystemTime(new Date('2024-07-02 12:00:00'));
vi.useFakeTimers({ toFake: ['Date'] }).setSystemTime(
new Date('2024-07-02 12:00:00')
);

const pendingRequest = waitForRequest('GET', '/records');

Expand Down
2 changes: 1 addition & 1 deletion src/search/components/autoRefreshToggle.component.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import AutoRefreshToggle, {

describe('AutoRefreshToggle', () => {
beforeEach(() => {
vi.useFakeTimers();
vi.useFakeTimers({ toFake: ['setInterval', 'clearInterval'] });
});

afterEach(() => {
Expand Down
14 changes: 12 additions & 2 deletions src/search/searchBar.component.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,9 @@ describe('searchBar component', () => {
});

it('sends default search parameters when none are amended by the user', async () => {
vi.useFakeTimers().setSystemTime(new Date('2024-07-02 12:00:00'));
vi.useFakeTimers({
toFake: ['Date', 'setTimeout', 'clearTimeout'],
}).setSystemTime(new Date('2024-07-02 12:00:00'));

user = userEvent.setup({ advanceTimers: vi.advanceTimersByTime });

Expand Down Expand Up @@ -657,7 +659,15 @@ describe('searchBar component', () => {
advanceTimers: vi.advanceTimersByTime,
});

vi.useFakeTimers().setSystemTime(new Date('2022-01-11 00:05'));
vi.useFakeTimers({
toFake: [
'Date',
'setInterval',
'clearInterval',
'setTimeout',
'clearTimeout',
],
}).setSystemTime(new Date('2022-01-11 00:05'));
});

afterEach(() => {
Expand Down
10 changes: 9 additions & 1 deletion src/session/sessionSaveButtons.component.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,15 @@ describe('session buttons', () => {
onChangeAutoSaveSessionId: onChangeAutoSaveSessionId,
autoSaveSessionId: undefined,
};
vi.useFakeTimers().setSystemTime(new Date('2024-07-15 12:00:00'));
vi.useFakeTimers({
toFake: [
'Date',
'setInterval',
'clearInterval',
'setTimeout',
'clearTimeout',
],
}).setSystemTime(new Date('2024-07-15 12:00:00'));

axiosPostSpy = vi.spyOn(ogApi, 'post');
axiosPatchSpy = vi.spyOn(ogApi, 'patch');
Expand Down
4 changes: 3 additions & 1 deletion src/views/viewTabs.component.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ describe('View Tabs', () => {
});

it('renders correctly', () => {
vi.useFakeTimers().setSystemTime(new Date('2024-07-15 12:00:00'));
vi.useFakeTimers({
toFake: ['Date'],
}).setSystemTime(new Date('2024-07-15 12:00:00'));
const { asFragment } = createView();
expect(asFragment()).toMatchSnapshot();
});
Expand Down
42 changes: 42 additions & 0 deletions src/windows/__snapshots__/windowPortal.component.test.tsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,48 @@
exports[`Window portal component > renders child in separate document and initialises event listeners & scripts, and handles unmounting correctly 1`] = `
<body>
<div>
<style
data-emotion="external-global"
data-s=""
>
html{-webkit-font-smoothing:antialiased;-moz-osx-font-smoothing:grayscale;box-sizing:border-box;-webkit-text-size-adjust:100%;-webkit-print-color-scheme:light;color-scheme:light;}
</style>
<style
data-emotion="external-global"
data-s=""
>
*,*::before,*::after{box-sizing:inherit;}
</style>
<style
data-emotion="external-global"
data-s=""
>
strong,b{font-weight:700;}
</style>
<style
data-emotion="external-global"
data-s=""
>
body{margin:0;color:rgba(0, 0, 0, 0.87);font-family:"Roboto","Helvetica","Arial",sans-serif;font-weight:400;font-size:1rem;line-height:1.5;letter-spacing:0.00938em;background-color:#fff;}
</style>
<style
data-emotion="external-global"
data-s=""
>
@media print{body{background-color:#fff;}}
</style>
<style
data-emotion="external-global"
data-s=""
>
body::backdrop{background-color:#fff;}
</style>
<div
id="test"
>
Expand Down

0 comments on commit 13857e3

Please sign in to comment.