-
Notifications
You must be signed in to change notification settings - Fork 29
/
vitest-setup.ts
106 lines (91 loc) · 2.98 KB
/
vitest-setup.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import '@testing-library/jest-dom/vitest';
import '@testing-library/svelte/vitest';
import { setupServer } from 'msw/node';
import { afterAll, afterEach, beforeAll, vi } from 'vitest';
import * as environment from '$app/environment';
import * as navigation from '$app/navigation';
import * as stores from '$app/stores';
import { fakeAssistants, fakeThreads } from '$testUtils/fakeData';
import OpenAIMock from '$lib/mocks/openai';
import dotenv from 'dotenv';
dotenv.config();
//Calls to vi.mock are hoisted to the top of the file, so you don't have access to variables declared in the global file scope unless they are defined with vi.hoisted before the call.
const { getStores } = await vi.hoisted(() => import('$lib/mocks/svelte'));
// Fixes error: node.scrollIntoView is not a function
window.HTMLElement.prototype.scrollIntoView = function () {};
export const mockOpenAI = new OpenAIMock({ apiKey: '', baseURL: '' });
vi.doMock('$lib/server/constants', () => {
return {
getOpenAiClient: vi.fn().mockReturnValue(mockOpenAI)
};
});
vi.mock('$env/dynamic/public', () => {
return {
env: {
PUBLIC_MESSAGE_LENGTH_LIMIT: '10000'
}
};
});
// Mock SvelteKit runtime module $app/environment
vi.mock('$app/environment', (): typeof environment => ({
browser: false,
dev: true,
building: false,
version: 'any'
}));
// Mock SvelteKit runtime module $app/navigation
vi.mock('$app/navigation', (): typeof navigation => ({
afterNavigate: () => {},
beforeNavigate: () => {},
disableScrollHandling: () => {},
goto: () => Promise.resolve(),
invalidate: () => Promise.resolve(),
invalidateAll: () => Promise.resolve(),
preloadData: () => Promise.resolve({ type: 'loaded', status: 200, data: {} }),
preloadCode: () => Promise.resolve(),
onNavigate: () => {},
pushState: () => {},
replaceState: () => {}
}));
// Mock SvelteKit runtime module $app/stores
vi.mock('$app/stores', (): typeof stores => {
const page: typeof stores.page = {
subscribe(fn) {
return getStores({
url: `http://localhost/chat/${fakeThreads[0].id}`,
params: { thread_id: fakeThreads[0].id },
data: {
threads: fakeThreads,
assistants: fakeAssistants,
assistant: undefined,
files: []
}
}).page.subscribe(fn);
}
};
const navigating: typeof stores.navigating = {
subscribe(fn) {
return getStores().navigating.subscribe(fn);
}
};
const updated: typeof stores.updated = {
subscribe(fn) {
return getStores().updated.subscribe(fn);
},
check: () => Promise.resolve(false)
};
return {
getStores,
navigating,
page,
updated
};
});
export const restHandlers = [];
export const server = setupServer(...restHandlers);
// Start server before all tests
beforeAll(() => server.listen({ onUnhandledRequest: 'error' }));
// Close server after all tests
afterAll(() => server.close());
// Reset handlers after each test `important for test isolation`
afterEach(() => server.resetHandlers());