Skip to content

Commit 9b9ede8

Browse files
fix: mock electron module for CI environment in systemControl tests
The handleElectronActions tests were failing in GitHub Actions because require.cache manipulation doesn't work for the electron module in CI. Solution: Extend Module.prototype.require to intercept electron imports and return a mock when currentElectronMock is set. This approach works consistently across local and CI environments. - Add currentElectronMock global variable - Intercept 'electron' requires in Module.prototype.require override - Update beforeEach/afterEach to set/clear currentElectronMock - Remove require.cache manipulation for electron
1 parent 888a611 commit 9b9ede8

File tree

1 file changed

+21
-6
lines changed

1 file changed

+21
-6
lines changed

tests/unit/systemControl.test.js

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,17 @@ childProcess.exec = (command) => {
2424
// Mock logger as console (has .log, .error, .warn methods)
2525
const Module = require("node:module");
2626
const originalRequire = Module.prototype.require;
27+
28+
// Global electron mock for handleElectronActions tests
29+
let currentElectronMock = null;
30+
2731
Module.prototype.require = function (id, ...args) {
2832
if (id === "logger") {
2933
return console;
3034
}
35+
if (id === "electron" && currentElectronMock) {
36+
return currentElectronMock;
37+
}
3138
return Reflect.apply(originalRequire, this, [id, ...args]);
3239
};
3340

@@ -119,9 +126,10 @@ describe("systemControl", () => {
119126
monitorControl("MONITOROFF", config, options, res, mocks.mockCheckForExecError, mocks.mockSendSocketNotification);
120127

121128
setTimeout(() => {
122-
assert.strictEqual(mocks.execResults.length, 1);
123-
assert.ok(mocks.execResults[0].command.includes("vcgencmd display_power 0"));
124-
assert.strictEqual(mocks.socketNotifications.length, 1);
129+
assert.strictEqual(mocks.execResults.length, 1, "Should execute one command");
130+
assert.ok(mocks.execResults[0].command.includes("vcgencmd display_power 0"), "Should execute monitor off command");
131+
assert.strictEqual(mocks.errorCheckResults.length, 1, "Should call error check callback");
132+
assert.strictEqual(mocks.socketNotifications.length, 1, "Should send one socket notification");
125133
assert.strictEqual(mocks.socketNotifications[0].notification, "USER_PRESENCE");
126134
assert.strictEqual(mocks.socketNotifications[0].payload, false);
127135
done();
@@ -244,9 +252,16 @@ describe("systemControl", () => {
244252
beforeEach(() => {
245253
responses = [];
246254
windowActions = [];
247-
// Mock electron module
248-
require.cache[require.resolve("electron")] = {exports: mockElectron};
249-
// Load handleElectronActions
255+
// Set electron mock globally so Module.prototype.require can return it
256+
currentElectronMock = mockElectron;
257+
// Load handleElectronActions with mocked electron
258+
delete require.cache[require.resolve("../../lib/systemControl.js")];
259+
const sc = require("../../lib/systemControl.js");
260+
handleElectronActions = sc.handleElectronActions;
261+
});
262+
263+
afterEach(() => {
264+
currentElectronMock = null;
250265
delete require.cache[require.resolve("../../lib/systemControl.js")];
251266
const sc = require("../../lib/systemControl.js");
252267
handleElectronActions = sc.handleElectronActions;

0 commit comments

Comments
 (0)