forked from opossum-tool/OpossumUI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetupTests.ts
83 lines (70 loc) · 2.26 KB
/
setupTests.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
// SPDX-FileCopyrightText: Meta Platforms, Inc. and its affiliates
// SPDX-FileCopyrightText: TNG Technology Consulting GmbH <https://www.tngtech.com>
// SPDX-FileCopyrightText: Nico Carl <nicocarl@protonmail.com>
//
// SPDX-License-Identifier: Apache-2.0
import '@testing-library/jest-dom/extend-expect';
import { TextEncoder } from 'util';
// required after bumping axios to 1.3.0
global.TextEncoder = TextEncoder;
const TEST_TIMEOUT = 15000;
const SUBSTRINGS_TO_SUPPRESS_IN_CONSOLE_INFO = [
'Web worker error in workers context',
'Error in rendering folder progress bar',
'Error in rendering top progress bar',
'Error in ResourceDetailsTab',
];
// this is a quick fix for #938
const SUBSTRINGS_TO_SUPPRESS_IN_CONSOLE_ERROR = [
'should be wrapped into act(...)',
];
jest.setTimeout(TEST_TIMEOUT);
jest.mock('./src/Frontend/web-workers/get-new-accordion-workers');
jest.mock('./src/Frontend/web-workers/get-new-progress-bar-workers');
const originalConsoleError = console.error;
const originalConsoleInfo = console.info;
beforeAll(() => {
global.window.electronAPI = {
openLink: jest.fn().mockReturnValue(Promise.resolve()),
openFile: jest.fn(),
deleteFile: jest.fn(),
keepFile: jest.fn(),
sendErrorInformation: jest.fn(),
exportFile: jest.fn(),
saveFile: jest.fn(),
on: jest.fn(),
removeListener: jest.fn(),
};
mockConsoleImplementation(SUBSTRINGS_TO_SUPPRESS_IN_CONSOLE_INFO, 'info');
mockConsoleImplementation(SUBSTRINGS_TO_SUPPRESS_IN_CONSOLE_ERROR, 'error');
});
beforeEach(() => jest.clearAllMocks());
afterAll(() => {
console.error = originalConsoleError;
console.error = originalConsoleInfo;
});
function mockConsoleImplementation(
selectors: Array<string>,
type: 'info' | 'error'
): void {
jest
.spyOn(console, type)
.mockImplementation(getMockConsoleImplementation(selectors, type));
}
function getMockConsoleImplementation(
selectors: Array<string>,
type: 'info' | 'error'
) {
return (...args: Array<unknown>): void => {
if (
selectors.filter(
(selector) => typeof args[0] === 'string' && args[0].includes(selector)
).length
) {
return;
}
return type === 'error'
? originalConsoleError.call(console, args)
: originalConsoleInfo.call(console, args);
};
}