Skip to content

Commit c1d0be8

Browse files
authored
test: use overridePlatform util (#1347)
1 parent 7fe2fdd commit c1d0be8

File tree

3 files changed

+14
-36
lines changed

3 files changed

+14
-36
lines changed

tests/main/first-run-spec.ts

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { app, dialog } from 'electron';
66

77
import { onFirstRunMaybe } from '../../src/main/first-run';
88
import { isFirstRun } from '../../src/utils/check-first-run';
9+
import { overridePlatform, resetPlatform } from '../utils';
910

1011
jest.mock('../../src/utils/check-first-run', () => ({
1112
isFirstRun: jest.fn(),
@@ -17,22 +18,17 @@ const mockDialogResponse = {
1718

1819
describe('first-run', () => {
1920
const oldDefaultApp = process.defaultApp;
20-
const oldPlatform = process.platform;
2121

2222
beforeEach(() => {
23-
Object.defineProperty(process, 'platform', {
24-
value: 'darwin',
25-
});
23+
overridePlatform('darwin');
2624

2725
(dialog.showMessageBox as jest.Mock<any>).mockResolvedValue(
2826
mockDialogResponse,
2927
);
3028
});
3129

3230
afterEach(() => {
33-
Object.defineProperty(process, 'platform', {
34-
value: oldPlatform,
35-
});
31+
resetPlatform();
3632
});
3733

3834
afterEach(() => {
@@ -65,9 +61,7 @@ describe('first-run', () => {
6561
});
6662

6763
it(`doesn't run unless required (Windows, Linux)`, () => {
68-
Object.defineProperty(process, 'platform', {
69-
value: 'win32',
70-
});
64+
overridePlatform('win32');
7165

7266
(isFirstRun as jest.Mock).mockReturnValueOnce(true);
7367
(app.isInApplicationsFolder as jest.Mock).mockReturnValue(false);

tests/main/main-spec.ts

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import { shouldQuit } from '../../src/main/squirrel';
2020
import { setupUpdates } from '../../src/main/update';
2121
import { getOrCreateMainWindow } from '../../src/main/windows';
2222
import { BrowserWindowMock } from '../mocks/browser-window';
23-
import { overridePlatform } from '../utils';
23+
import { overridePlatform, resetPlatform } from '../utils';
2424

2525
jest.mock('../../src/main/windows', () => ({
2626
getOrCreateMainWindow: jest.fn(),
@@ -47,18 +47,12 @@ jest.mock('../../src/main/ipc');
4747
* for CI to know that the app is still opening a window.
4848
*/
4949
describe('main', () => {
50-
const oldPlatform = process.platform;
51-
5250
beforeAll(() => {
53-
Object.defineProperty(process, 'platform', {
54-
value: 'win32',
55-
});
51+
overridePlatform('win32');
5652
});
5753

5854
afterAll(() => {
59-
Object.defineProperty(process, 'platform', {
60-
value: oldPlatform,
61-
});
55+
resetPlatform();
6256
});
6357

6458
beforeEach(() => {
@@ -109,9 +103,7 @@ describe('main', () => {
109103
});
110104

111105
it('does not quit the app on macOS', () => {
112-
Object.defineProperty(process, 'platform', {
113-
value: 'darwin',
114-
});
106+
overridePlatform('darwin');
115107

116108
onWindowsAllClosed();
117109

tests/utils/exec-spec.ts

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1+
import { overridePlatform, resetPlatform } from '../utils';
2+
13
jest.mock('child_process');
24

35
const mockShellEnv = jest.fn();
46
jest.mock('shell-env', () => mockShellEnv);
57

68
describe('exec', () => {
7-
const oldPlatform = process.platform;
8-
99
// Allow us to reset the module between each run
1010
let execModule = require('../../src/utils/exec');
1111

@@ -16,9 +16,7 @@ describe('exec', () => {
1616
});
1717

1818
afterEach(() => {
19-
Object.defineProperty(process, 'platform', {
20-
value: oldPlatform,
21-
});
19+
resetPlatform();
2220
});
2321

2422
describe('exec()', () => {
@@ -76,29 +74,23 @@ describe('exec', () => {
7674

7775
describe('maybeFixPath()', () => {
7876
it('does not do anything on Windows', async () => {
79-
Object.defineProperty(process, 'platform', {
80-
value: 'win32',
81-
});
77+
overridePlatform('win32');
8278

8379
await execModule.maybeFixPath();
8480

8581
expect(mockShellEnv).toHaveBeenCalledTimes(0);
8682
});
8783

8884
it('calls shell-env on macOS', async () => {
89-
Object.defineProperty(process, 'platform', {
90-
value: 'darwin',
91-
});
85+
overridePlatform('darwin');
9286

9387
await execModule.maybeFixPath();
9488

9589
expect(mockShellEnv).toHaveBeenCalledTimes(1);
9690
});
9791

9892
it('calls shell-env on Linux', async () => {
99-
Object.defineProperty(process, 'platform', {
100-
value: 'linux',
101-
});
93+
overridePlatform('linux');
10294

10395
await execModule.maybeFixPath();
10496

0 commit comments

Comments
 (0)