Skip to content

Commit 18e65a1

Browse files
committed
fix flaky tests
1 parent 3b88f33 commit 18e65a1

File tree

4 files changed

+15
-7
lines changed

4 files changed

+15
-7
lines changed

examples/vite/src/App.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ import { HttpQueryBuilder } from 'react-query-builder';
88
import { queryClient } from './client';
99
import './index.css';
1010

11-
await setupWorker(...getMockHandlers()).start({
11+
const isTest = import.meta.env.MODE === 'test';
12+
13+
await setupWorker(...getMockHandlers(!isTest)).start({
1214
onUnhandledRequest: 'bypass',
1315
quiet: true,
1416
});

examples/vite/src/vite-env.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
/// <reference types="vite/client" />
2+
/// <reference types="vitest/importMeta" />

examples/vite/tests/Render.test.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@ import { baseUrl } from 'react-query-builder-example-mocks';
22
import { expect, test } from 'vitest';
33
import { render } from 'vitest-browser-react';
44
import { App } from '../src/App';
5+
import { queryClient } from '../src/client';
56

67
beforeEach(async () => {
78
await fetch(`${baseUrl}/reset`, { method: 'POST' });
9+
queryClient.clear();
810
});
911

1012
test('Renders the app', async () => {
@@ -45,7 +47,7 @@ test('Deletes posts immediately on clicking delete button', async () => {
4547
await expect.element(el).not.toBeInTheDocument();
4648
});
4749

48-
test('Deletes deletes but reverts the deletion after server fails', async () => {
50+
test('Deletes the post but reverts the deletion after server fails', async () => {
4951
const { getByText } = render(<App />);
5052

5153
const el = getByText('0 - Exploring the Future of AI');

packages/example-mocks/src/mocks.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,9 @@ function createMockData() {
2323
return { users, posts, comments };
2424
}
2525

26-
export function getMockHandlers() {
26+
export function getMockHandlers(withLocalStorage = true) {
2727
function saveMockData(saveData = { users, posts, comments }) {
28+
if (!withLocalStorage) return;
2829
localStorage.setItem('mockData', JSON.stringify(saveData));
2930
}
3031

@@ -35,11 +36,13 @@ export function getMockHandlers() {
3536
}
3637

3738
function getMockData() {
38-
const ls = localStorage.getItem('mockData');
3939
try {
40-
if (ls) {
41-
const data = JSON.parse(ls);
42-
if (data.users && data.posts && data.comments) return data as ReturnType<typeof createMockData>;
40+
if (withLocalStorage) {
41+
const ls = localStorage.getItem('mockData');
42+
if (ls) {
43+
const data = JSON.parse(ls);
44+
if (data.users && data.posts && data.comments) return data as ReturnType<typeof createMockData>;
45+
}
4346
}
4447
} catch (e) {}
4548

0 commit comments

Comments
 (0)