Skip to content

Commit c3eaa46

Browse files
authored
chore: updating test files to not break when node is in v24 (#1434)
Node v24 makes `os.platform` immutable which causes the `spyOn` to fail. <!-- CURSOR_SUMMARY --> --- > [!NOTE] > **Low Risk** > Test-only changes with no production code paths affected. > > **Overview** > Updates **Electron** and **server-node** platform info tests so they run on **Node v24**, where `os.platform` (and related APIs) are no longer spyable with `jest.spyOn`. > > Both test files now **`jest.mock('os')`** with `jest.fn()` stand-ins for the `os` methods under test, **`beforeEach`** restores real implementations for tests that use the live OS, and the mock-data suites set **`mockReturnValue`** instead of spies. Mock suites also add **`afterEach(jest.clearAllMocks)`**. Production SDK code is unchanged. > > <sup>Reviewed by [Cursor Bugbot](https://cursor.com/bugbot) for commit 4828bca. Bugbot is set up for automated code reviews on this repo. Configure [here](https://www.cursor.com/dashboard/bugbot).</sup> <!-- /CURSOR_SUMMARY -->
1 parent 2c55f5b commit c3eaa46

2 files changed

Lines changed: 50 additions & 22 deletions

File tree

packages/sdk/electron/__tests__/platform/ElectronInfo.test.ts

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,23 @@ import * as os from 'os';
22

33
import ElectronInfo from '../../src/platform/ElectronInfo';
44

5+
jest.mock('os', () => ({
6+
...jest.requireActual('os'),
7+
platform: jest.fn(),
8+
release: jest.fn(),
9+
arch: jest.fn(),
10+
}));
11+
12+
const actualOs = jest.requireActual<typeof os>('os');
13+
14+
// Restore real os implementations before each test so non-mock tests still work.
15+
// Mock tests override these per-test with mockReturnValue.
16+
beforeEach(() => {
17+
(os.platform as jest.Mock).mockImplementation(actualOs.platform);
18+
(os.release as jest.Mock).mockImplementation(actualOs.release);
19+
(os.arch as jest.Mock).mockImplementation(actualOs.arch);
20+
});
21+
522
describe('given an information instance', () => {
623
const info = new ElectronInfo();
724

@@ -34,15 +51,14 @@ describe('given an information instance', () => {
3451
describe('given an information instance with mock data', () => {
3552
const info = new ElectronInfo();
3653

37-
it('can get platform information', () => {
38-
const platformSpy = jest.spyOn(os, 'platform');
39-
platformSpy.mockReturnValue('darwin');
40-
41-
const releaseSpy = jest.spyOn(os, 'release');
42-
releaseSpy.mockReturnValue('0.0.0');
54+
afterEach(() => {
55+
jest.clearAllMocks();
56+
});
4357

44-
const archSpy = jest.spyOn(os, 'arch');
45-
archSpy.mockReturnValue('x64');
58+
it('can get platform information', () => {
59+
(os.platform as jest.Mock).mockReturnValue('darwin');
60+
(os.release as jest.Mock).mockReturnValue('0.0.0');
61+
(os.arch as jest.Mock).mockReturnValue('x64');
4662

4763
global.process = {
4864
...process,
@@ -65,9 +81,7 @@ describe('given an information instance with mock data', () => {
6581
['linux', 'Linux'],
6682
['some_os', 'some_os'],
6783
])('handles known platforms', (platform, processed) => {
68-
const platformSpy = jest.spyOn(os, 'platform');
69-
// @ts-ignore
70-
platformSpy.mockReturnValue(platform);
84+
(os.platform as jest.Mock).mockReturnValue(platform);
7185

7286
const data = info.platformData();
7387
expect(data.os).toBeDefined();

packages/sdk/server-node/__tests__/platform/NodeInfo.test.ts

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,23 @@ import * as os from 'os';
22

33
import NodeInfo from '../../src/platform/NodeInfo';
44

5+
jest.mock('os', () => ({
6+
...jest.requireActual('os'),
7+
platform: jest.fn(),
8+
version: jest.fn(),
9+
arch: jest.fn(),
10+
}));
11+
12+
const actualOs = jest.requireActual<typeof os>('os');
13+
14+
// Restore real os implementations before each test so non-mock tests still work.
15+
// Mock tests override these per-test with mockReturnValue.
16+
beforeEach(() => {
17+
(os.platform as jest.Mock).mockImplementation(actualOs.platform);
18+
(os.version as jest.Mock).mockImplementation(actualOs.version);
19+
(os.arch as jest.Mock).mockImplementation(actualOs.arch);
20+
});
21+
522
describe('given an information instance', () => {
623
const info = new NodeInfo({});
724

@@ -34,15 +51,14 @@ test('it supports wrapper name and version', () => {
3451
describe('given an information instance with mock data', () => {
3552
const info = new NodeInfo({});
3653

37-
it('can get platform information', () => {
38-
const platformSpy = jest.spyOn(os, 'platform');
39-
platformSpy.mockReturnValue('darwin');
40-
41-
const versionSpy = jest.spyOn(os, 'version');
42-
versionSpy.mockReturnValue('0.0.0');
54+
afterEach(() => {
55+
jest.clearAllMocks();
56+
});
4357

44-
const archSpy = jest.spyOn(os, 'arch');
45-
archSpy.mockReturnValue('s390x');
58+
it('can get platform information', () => {
59+
(os.platform as jest.Mock).mockReturnValue('darwin');
60+
(os.version as jest.Mock).mockReturnValue('0.0.0');
61+
(os.arch as jest.Mock).mockReturnValue('s390x');
4662

4763
global.process = {
4864
...process,
@@ -64,9 +80,7 @@ describe('given an information instance with mock data', () => {
6480
['linux', 'Linux'],
6581
['some_os', 'some_os'],
6682
])('handles known platforms', (platform, processed) => {
67-
const platformSpy = jest.spyOn(os, 'platform');
68-
// @ts-ignore
69-
platformSpy.mockReturnValue(platform);
83+
(os.platform as jest.Mock).mockReturnValue(platform);
7084

7185
const data = info.platformData();
7286
expect(data.os).toBeDefined();

0 commit comments

Comments
 (0)