-
Notifications
You must be signed in to change notification settings - Fork 448
Expand file tree
/
Copy pathvitest.setup.ts
More file actions
133 lines (116 loc) · 4.21 KB
/
Copy pathvitest.setup.ts
File metadata and controls
133 lines (116 loc) · 4.21 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
131
132
133
/**
* Copyright (c) 2015-2026 Adguard Software Ltd.
*
* @file
* This file is part of AdGuard Browser Extension (https://github.com/AdguardTeam/AdguardBrowserExtension).
*
* AdGuard Browser Extension is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* AdGuard Browser Extension is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with AdGuard Browser Extension. If not, see <http://www.gnu.org/licenses/>.
*/
import { vi } from 'vitest';
import browser from 'sinon-chrome';
import escape from 'css.escape';
// @ts-ignore
import chrome from 'sinon-chrome/extensions';
import { type DebouncedFunc } from 'lodash-es/debounce';
import { ResourceType } from '@adguard/dnr-converter';
import { MANIFEST_ENV } from './tools/constants';
import {
MockedTsWebExtension,
MockedTsWebExtensionMV3,
mockLocalStorage,
mockXhrRequests,
} from './tests/helpers';
import { mockGlobalFetch } from './tests/helpers/mocks/fetch';
/**
* sinon-chrome does declare a browser.runtime.id property, but its value is null, which caused the duck-typing to fail.
*
* @see https://github.com/mozilla/webextension-polyfill/issues/218#issuecomment-584936358
*/
chrome.runtime.id = 'test';
const EXTENSION_URL_PREFIX = 'chrome-extension://';
browser.runtime.getURL.callsFake((url: string) => `${EXTENSION_URL_PREFIX}test/${url}`);
browser.runtime.getManifest.callsFake(() => ({
name: 'AdGuard AdBlocker',
version: '0.0.0',
manifest_version: MANIFEST_ENV as any,
}));
browser.runtime.getPlatformInfo.resolves({
os: 'mac',
arch: 'x86-64',
});
Object.assign(browser, {
/**
* These values are used in the background script to determine the maximum
* number of rules that can be added.
*/
declarativeNetRequest: {
/** @see https://developer.chrome.com/docs/extensions/reference/api/declarativeNetRequest#property-MAX_NUMBER_OF_REGEX_RULES */
MAX_NUMBER_OF_REGEX_RULES: 1000,
MAX_NUMBER_OF_DYNAMIC_AND_SESSION_RULES: 5000,
ResourceType,
},
webRequest: {
...browser.webRequest,
filterResponseData: vi.fn(),
},
tabs: {
...browser.tabs,
query: vi.fn(() => []),
},
i18n: {
getUILanguage: vi.fn(() => 'en'),
getMessage: vi.fn((value: string) => value),
},
});
// Mock webextension-polyfill
vi.mock('webextension-polyfill', () => ({ default: browser }));
vi.mock('nanoid', () => ({
nanoid: (): string => 'cd42f5fa',
customAlphabet: (): Function => (): string => 'cd42f5fa',
}));
// Mock log to hide all logger message
vi.mock('./Extension/src/common/logger.ts');
// Polyfill ResizeObserver for jsdom (not natively supported)
// eslint-disable-next-line class-methods-use-this
global.ResizeObserver = class ResizeObserver {
// eslint-disable-next-line class-methods-use-this
observe() {}
// eslint-disable-next-line class-methods-use-this
unobserve() {}
// eslint-disable-next-line class-methods-use-this
disconnect() {}
};
vi.mock('@adguard/tswebextension', async () => ({
...(await vi.importActual('@adguard/tswebextension')),
TsWebExtension: MockedTsWebExtension,
isExtensionUrl: vi.fn((url: string) => url.startsWith(EXTENSION_URL_PREFIX)),
}));
vi.mock('@adguard/tswebextension/mv3', async () => ({
...(await vi.importActual('@adguard/tswebextension/mv3')),
TsWebExtension: MockedTsWebExtensionMV3,
isExtensionUrl: vi.fn((url: string) => url.startsWith(EXTENSION_URL_PREFIX)),
}));
vi.mock('lodash-es', async () => ({
...await vi.importActual('lodash-es'),
debounce: ((func: (...args: unknown[]) => unknown) => func as DebouncedFunc<(...args: unknown[]) => unknown>),
}));
mockLocalStorage();
Object.assign(global, {
sinonFakeServer: mockXhrRequests(),
fetch: mockGlobalFetch(),
CSS: {
escape,
},
chrome: browser,
});