-
Notifications
You must be signed in to change notification settings - Fork 334
/
testSetup.ts
87 lines (69 loc) · 2.89 KB
/
testSetup.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
/**
* @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/>.
*/
// replace unsupported fetch API by xhr requests
import { TextEncoder, TextDecoder } from 'util';
import 'whatwg-fetch';
import escape from 'css.escape';
import mockBrowser from 'sinon-chrome';
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
import chrome from 'sinon-chrome/extensions';
import type { DebouncedFunc } from 'lodash-es';
// After mocked user-agent, we can import all other mocks
// eslint-disable-next-line import/first
import {
MockedTsWebExtension,
mockLocalStorage,
mockXhrRequests,
} from './tests/helpers';
// set missing CSS.escape polyfill for test env
global.CSS.escape = escape;
Object.assign(global, { TextDecoder, TextEncoder });
/**
* 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 = 'text';
// mock chrome webextension api
global.chrome = chrome;
// implements some global function for 'webextension-polyfill' before mocking
mockBrowser.runtime.getURL.callsFake((url: string) => `chrome-extension://test/${url}`);
mockBrowser.runtime.getManifest.returns({ version: '0.0.0' });
mockBrowser.i18n.getUILanguage.returns('en');
mockBrowser.i18n.getMessage.callsFake((value: string) => value);
mockBrowser.tabs.query.returns([]);
jest.mock('webextension-polyfill', () => mockBrowser);
jest.mock('nanoid', () => ({
nanoid: jest.fn((): string => 'cTkoV5Vs'),
}));
// Mock log to hide all logger message
jest.mock('./Extension/src/common/logger.ts');
jest.mock('@adguard/tswebextension', () => ({
...(jest.requireActual('@adguard/tswebextension')),
TsWebExtension: MockedTsWebExtension,
}));
jest.mock('lodash-es', () => ({
...jest.requireActual('lodash-es'),
debounce: ((func: (...args: unknown[]) => unknown) => func as DebouncedFunc<(...args: unknown[]) => unknown>),
}));
// create browser.storage.local emulator and bound it with sinon-chrome stub
mockLocalStorage();
// register fake server for xhr requests
const server = mockXhrRequests();
export { server };