diff --git a/packages/sdk/electron/__tests__/platform/ElectronInfo.test.ts b/packages/sdk/electron/__tests__/platform/ElectronInfo.test.ts index ebfac53edc..873d520919 100644 --- a/packages/sdk/electron/__tests__/platform/ElectronInfo.test.ts +++ b/packages/sdk/electron/__tests__/platform/ElectronInfo.test.ts @@ -2,6 +2,23 @@ import * as os from 'os'; import ElectronInfo from '../../src/platform/ElectronInfo'; +jest.mock('os', () => ({ + ...jest.requireActual('os'), + platform: jest.fn(), + release: jest.fn(), + arch: jest.fn(), +})); + +const actualOs = jest.requireActual('os'); + +// Restore real os implementations before each test so non-mock tests still work. +// Mock tests override these per-test with mockReturnValue. +beforeEach(() => { + (os.platform as jest.Mock).mockImplementation(actualOs.platform); + (os.release as jest.Mock).mockImplementation(actualOs.release); + (os.arch as jest.Mock).mockImplementation(actualOs.arch); +}); + describe('given an information instance', () => { const info = new ElectronInfo(); @@ -34,15 +51,14 @@ describe('given an information instance', () => { describe('given an information instance with mock data', () => { const info = new ElectronInfo(); - it('can get platform information', () => { - const platformSpy = jest.spyOn(os, 'platform'); - platformSpy.mockReturnValue('darwin'); - - const releaseSpy = jest.spyOn(os, 'release'); - releaseSpy.mockReturnValue('0.0.0'); + afterEach(() => { + jest.clearAllMocks(); + }); - const archSpy = jest.spyOn(os, 'arch'); - archSpy.mockReturnValue('x64'); + it('can get platform information', () => { + (os.platform as jest.Mock).mockReturnValue('darwin'); + (os.release as jest.Mock).mockReturnValue('0.0.0'); + (os.arch as jest.Mock).mockReturnValue('x64'); global.process = { ...process, @@ -65,9 +81,7 @@ describe('given an information instance with mock data', () => { ['linux', 'Linux'], ['some_os', 'some_os'], ])('handles known platforms', (platform, processed) => { - const platformSpy = jest.spyOn(os, 'platform'); - // @ts-ignore - platformSpy.mockReturnValue(platform); + (os.platform as jest.Mock).mockReturnValue(platform); const data = info.platformData(); expect(data.os).toBeDefined(); diff --git a/packages/sdk/server-node/__tests__/platform/NodeInfo.test.ts b/packages/sdk/server-node/__tests__/platform/NodeInfo.test.ts index fd4a208560..16628a6794 100644 --- a/packages/sdk/server-node/__tests__/platform/NodeInfo.test.ts +++ b/packages/sdk/server-node/__tests__/platform/NodeInfo.test.ts @@ -2,6 +2,23 @@ import * as os from 'os'; import NodeInfo from '../../src/platform/NodeInfo'; +jest.mock('os', () => ({ + ...jest.requireActual('os'), + platform: jest.fn(), + version: jest.fn(), + arch: jest.fn(), +})); + +const actualOs = jest.requireActual('os'); + +// Restore real os implementations before each test so non-mock tests still work. +// Mock tests override these per-test with mockReturnValue. +beforeEach(() => { + (os.platform as jest.Mock).mockImplementation(actualOs.platform); + (os.version as jest.Mock).mockImplementation(actualOs.version); + (os.arch as jest.Mock).mockImplementation(actualOs.arch); +}); + describe('given an information instance', () => { const info = new NodeInfo({}); @@ -34,15 +51,14 @@ test('it supports wrapper name and version', () => { describe('given an information instance with mock data', () => { const info = new NodeInfo({}); - it('can get platform information', () => { - const platformSpy = jest.spyOn(os, 'platform'); - platformSpy.mockReturnValue('darwin'); - - const versionSpy = jest.spyOn(os, 'version'); - versionSpy.mockReturnValue('0.0.0'); + afterEach(() => { + jest.clearAllMocks(); + }); - const archSpy = jest.spyOn(os, 'arch'); - archSpy.mockReturnValue('s390x'); + it('can get platform information', () => { + (os.platform as jest.Mock).mockReturnValue('darwin'); + (os.version as jest.Mock).mockReturnValue('0.0.0'); + (os.arch as jest.Mock).mockReturnValue('s390x'); global.process = { ...process, @@ -64,9 +80,7 @@ describe('given an information instance with mock data', () => { ['linux', 'Linux'], ['some_os', 'some_os'], ])('handles known platforms', (platform, processed) => { - const platformSpy = jest.spyOn(os, 'platform'); - // @ts-ignore - platformSpy.mockReturnValue(platform); + (os.platform as jest.Mock).mockReturnValue(platform); const data = info.platformData(); expect(data.os).toBeDefined();