-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjest.setup.js
More file actions
152 lines (136 loc) · 4.21 KB
/
jest.setup.js
File metadata and controls
152 lines (136 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
const path = require('path');
const { TextDecoder, TextEncoder } = require('util');
// Ensure the working directory is set to the project root
process.chdir(path.resolve(__dirname));
// Mock environment variables if needed
process.env.NODE_ENV = 'test';
// Some transitive Node dependencies expect the util implementations explicitly.
if (typeof globalThis.TextEncoder !== 'function') {
globalThis.TextEncoder = TextEncoder;
}
if (typeof globalThis.TextDecoder !== 'function') {
globalThis.TextDecoder = TextDecoder;
}
// Setup jsdom-like globals for component tests
if (typeof window === 'undefined') {
global.window = global;
}
if (!global.window.document) {
global.window.document = { createElement: () => ({ style: {} }) };
}
// Simple localStorage mock with quota simulation
class LocalStorageMock {
constructor() { this.store = new Map(); this.maxBytes = 5 * 1024 * 1024; }
clear() { this.store.clear(); }
getItem(key) { return this.store.has(key) ? this.store.get(key) : null; }
key(i) { return Array.from(this.store.keys())[i]; }
removeItem(key) { this.store.delete(key); }
setItem(key, value) {
const str = String(value);
// simulate quota exceed
const currentSize = Array.from(this.store.values()).reduce((s, v) => s + String(v).length, 0);
if (currentSize + str.length > this.maxBytes) {
const err = new Error('QuotaExceededError');
err.name = 'QuotaExceededError';
throw err;
}
this.store.set(key, str);
}
get length() { return this.store.size; }
}
if (!global.localStorage) {
global.localStorage = new LocalStorageMock();
}
// Mock preload bridge used in renderer code
global.window.electron = {
system: {
on: {
confirmEditorClose: jest.fn(),
confirmEditorReload: jest.fn(),
},
off: {
confirmEditorClose: jest.fn(),
confirmEditorReload: jest.fn(),
},
openExternal: jest.fn(),
confirmEditorCloseApproved: jest.fn(),
confirmEditorReloadApproved: jest.fn(),
getModuleFiles: jest.fn().mockResolvedValue({ files: [] }),
getFdoSdkTypes: jest.fn().mockResolvedValue({ files: [] }),
},
plugin: {
compile: jest.fn().mockResolvedValue({ result: 'ok' }),
deploy: jest.fn().mockResolvedValue({ result: 'ok' }),
},
settings: {
certificates: {
getRoot: jest.fn().mockResolvedValue([]),
},
},
};
// Mock Electron module for Node context
jest.mock('electron', () => ({
app: {
isPackaged: false,
getPath: jest.fn((name) => {
if (name === 'userData') return '/tmp/fdo-jest-user-data';
if (name === 'sessionData') return '/tmp/fdo-jest-session-data';
return '/tmp';
}),
},
BrowserWindow: jest.fn(),
dialog: {},
nativeTheme: {},
net: {},
protocol: {},
session: {},
}));
jest.mock('electron-store', () => {
return class MockElectronStore {
constructor(options = {}) {
this.store = { ...(options.defaults || {}) };
}
get(key, defaultValue = undefined) {
if (!key) return this.store;
return key.split('.').reduce((acc, part) => (
acc && Object.prototype.hasOwnProperty.call(acc, part) ? acc[part] : defaultValue
), this.store);
}
set(key, value) {
const parts = String(key).split('.');
let cursor = this.store;
while (parts.length > 1) {
const part = parts.shift();
if (!cursor[part] || typeof cursor[part] !== 'object') {
cursor[part] = {};
}
cursor = cursor[part];
}
cursor[parts[0]] = value;
}
delete(key) {
const parts = String(key).split('.');
let cursor = this.store;
while (parts.length > 1 && cursor) {
cursor = cursor[parts.shift()];
}
if (cursor) {
delete cursor[parts[0]];
}
}
clear() {
this.store = {};
}
};
});
jest.mock('file-type', () => ({
fileTypeFromFile: jest.fn().mockResolvedValue(null),
}), { virtual: true });
// Silence console noise during tests
const origError = console.error;
console.error = (...args) => {
const msg = (args && args[0]) || '';
if (typeof msg === 'string' && (msg.includes('act(') || msg.includes('Not implemented'))) return;
origError.apply(console, args);
};
console.log('Test setup complete. Current working directory:', process.cwd());