Skip to content

Commit 59450af

Browse files
committed
Create sample test
- Added module '_utils/node-env' to quickly determine the value of `NODE_ENV`. - Created example of export private properties for unit testing. - Added Jest unit test sample.
1 parent 2b83a60 commit 59450af

File tree

3 files changed

+28
-2
lines changed

3 files changed

+28
-2
lines changed

src/main/main.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@
33
*/
44
import * as path from 'path';
55
import * as url from 'url';
6+
import * as nodeEnv from '_utils/node-env';
67
// eslint-disable-next-line import/no-extraneous-dependencies
78
import { BrowserWindow, app } from 'electron';
89

9-
let mainWindow: Electron.BrowserWindow | null;
10+
let mainWindow: Electron.BrowserWindow | null = null;
1011

1112
function createWindow(): void {
1213
// Create the browser window.
@@ -15,7 +16,7 @@ function createWindow(): void {
1516
width: 800,
1617
webPreferences: {
1718
webSecurity: false,
18-
devTools: process.env.NODE_ENV !== 'production',
19+
devTools: nodeEnv.dev,
1920
},
2021
});
2122

@@ -61,3 +62,7 @@ app.on('activate', () => {
6162

6263
// In this file you can include the rest of your app"s specific main process
6364
// code. You can also put them in separate files and require them here.
65+
66+
// eslint-disable-next-line import/prefer-default-export
67+
export const exportedForTests = nodeEnv.test
68+
? { mainWindow, createWindow } : undefined;

src/utils/node-env.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/**
2+
* Predefined NODE_ENV values
3+
*/
4+
5+
/** `NODE_ENV=production` */
6+
export const prod = process.env.NODE_ENV === 'production';
7+
8+
/** `NODE_ENV=development` */
9+
export const dev = process.env.NODE_ENV === 'development';
10+
11+
/** `NODE_ENV=test` */
12+
export const test = process.env.NODE_ENV === 'test';

tests/main/main.spec.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { exportedForTests } from '_main/main';
2+
3+
jest.mock('electron', () => ({
4+
app: { on: jest.fn() },
5+
}));
6+
7+
test('Private props exported for unit tests', () => {
8+
expect(exportedForTests).toBeDefined();
9+
});

0 commit comments

Comments
 (0)