-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathvitest.setup.ts
More file actions
130 lines (110 loc) · 3.2 KB
/
vitest.setup.ts
File metadata and controls
130 lines (110 loc) · 3.2 KB
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import { parseBoolEnvVar } from '$lib/utils/env.utils';
import { mockPage } from '$tests/mocks/page.store.mock';
import {
allowLoggingForDebugging,
disableConsoleLog,
failTestsThatLogToConsole
} from '$tests/utils/console.test-utils';
import type { HttpAgent } from '@icp-sdk/core/agent';
import '@testing-library/jest-dom';
import { configure } from '@testing-library/svelte';
import 'fake-indexeddb/auto';
import { mock } from 'vitest-mock-extended';
// We mock ResizeObserver and element.animate because neither JSDOM nor Happy DOM supports them, while Svelte v5 requires them.
// Interesting related thread: https://github.com/testing-library/svelte-testing-library/issues/284
global.ResizeObserver = class ResizeObserver {
observe() {
// do nothing
}
unobserve() {
// do nothing
}
disconnect() {
// do nothing
}
};
// eslint-disable-next-line local-rules/prefer-object-params
Element.prototype.animate = (
_keyframes: Keyframe[] | PropertyIndexedKeyframes,
options?: number | KeyframeAnimationOptions
): Animation => {
const animation = {
abort: vi.fn(),
cancel: vi.fn(),
finished: Promise.resolve()
// Svelte v5 register onfinish
// Source: https://github.com/sveltejs/svelte/blob/75f81991c27e9602d4bb3eb44aec8775de0713af/packages/svelte/src/internal/client/dom/elements/transitions.js#L386
// onfinish: () => undefined
} as unknown as Animation;
setTimeout(
// @ts-expect-error We are omitting the parameter of onfinish for simplicity reason and because Svelte v5 do not use those.
() => animation.onfinish(),
typeof options === 'number' ? options : Number(options?.duration ?? 0)
);
return animation;
};
Element.prototype.scrollTo = vi.fn();
vi.mock('$app/stores', () => ({
page: mockPage
}));
vi.mock('$app/state', () => ({
page: {}
}));
vi.mock(import('$lib/actors/agents.ic'), async (importOriginal) => {
const actual = await importOriginal();
return {
...actual,
// eslint-disable-next-line require-await
getAgent: async () => mock<HttpAgent>()
};
});
vi.mock('$lib/services/analytics-wrapper', () => ({
loadPlausibleTracker: vi.fn(() => ({
init: vi.fn(),
track: vi.fn()
}))
}));
vi.mock('ethers/providers', () => {
const MockProvider = vi.fn(class {});
const plugin = vi.fn();
const network = vi.fn();
network.prototype.attachPlugin = vi.fn();
return {
EtherscanProvider: MockProvider,
InfuraProvider: MockProvider,
JsonRpcProvider: MockProvider,
EtherscanPlugin: plugin,
Network: network
};
});
vi.mock('idb-keyval', () => ({
createStore: vi.fn(() => ({})),
set: vi.fn(),
get: vi.fn(),
del: vi.fn(),
clear: vi.fn(),
delMany: vi.fn(),
keys: vi.fn(() => []),
update: vi.fn()
}));
failTestsThatLogToConsole();
const ALLOW_LOGGING_FOR_DEBUGGING = parseBoolEnvVar(
process.env.ALLOW_LOGGING_FOR_DEBUGGING ?? import.meta.env.VITE_ALLOW_LOGGING_FOR_DEBUGGING
);
if (ALLOW_LOGGING_FOR_DEBUGGING) {
allowLoggingForDebugging();
}
disableConsoleLog();
configure({
testIdAttribute: 'data-tid'
});
window.matchMedia = vi.fn().mockImplementation((query) => ({
matches: false,
media: query,
onchange: null,
addListener: vi.fn(), // Deprecated
removeListener: vi.fn(), // Deprecated
addEventListener: vi.fn(),
removeEventListener: vi.fn(),
dispatchEvent: vi.fn()
}));