Skip to content

Commit 08d145b

Browse files
authored
Merge pull request #24 from th-ch/tests
[Tests] Add integration tests
2 parents 37cac19 + 57d3545 commit 08d145b

File tree

10 files changed

+2453
-39
lines changed

10 files changed

+2453
-39
lines changed

.travis.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ jobs:
99
osx_image: xcode11.3
1010
- os: linux
1111
dist: xenial
12+
services:
13+
- xvfb
1214

1315
cache:
1416
yarn: false
@@ -18,6 +20,7 @@ cache:
1820

1921
script:
2022
- |
23+
yarn test
2124
if [ "$TRAVIS_OS_NAME" == "linux" ]; then
2225
yarn run release:linux
2326
else

appveyor.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ install:
2121
# # https://www.appveyor.com/docs/how-to/rdp-to-build-worker/
2222
# - ps: $blockRdp = $true; iex ((new-object net.webclient).DownloadString('https://raw.githubusercontent.com/appveyor/ci/master/scripts/enable-rdp.ps1'))
2323

24+
# @FIXME: tests disabled because app fails to launch on AppVeyor/Windows
25+
# os: unstable # https://github.com/electron-userland/spectron#on-appveyor
26+
# test_script:
27+
# - yarn test
28+
2429
build_script:
2530
- yarn run release:win
2631

index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ const {
1414
store,
1515
} = require("./store");
1616
const { fileExists, injectCSS } = require("./plugins/utils");
17+
const { isTesting } = require("./utils/testing");
1718
const { setUpTray } = require("./tray");
1819

1920
const app = electron.app;
@@ -49,7 +50,7 @@ function createMainWindow() {
4950
backgroundColor: "#000",
5051
show: false,
5152
webPreferences: {
52-
nodeIntegration: false,
53+
nodeIntegration: isTesting(), // Only necessary when testing with Spectron
5354
preload: path.join(__dirname, "preload.js"),
5455
nativeWindowOpen: true, // window.open return Window object(like in regular browsers), not BrowserWindowProxy
5556
affinity: "main-window", // main window, and addition windows should work in one process

jest.config.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
module.exports = {
2+
globals: {
3+
__APP__: undefined, // A different app will be launched in each test environment
4+
},
5+
testEnvironment: "./tests/environment",
6+
testTimeout: 30000, // 30s
7+
};

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
}
2727
},
2828
"scripts": {
29-
"test": "xo",
29+
"test": "jest",
3030
"start": "electron .",
3131
"icon": "rimraf assets/generated && electron-icon-maker --input=assets/youtube-music.png --output=assets/generated",
3232
"generate:package": "node utils/generate-package-json.js",
@@ -61,7 +61,10 @@
6161
"electron-builder": "^22.4.1",
6262
"electron-devtools-installer": "^3.0.0",
6363
"electron-icon-maker": "0.0.4",
64+
"get-port": "^5.1.1",
65+
"jest": "^25.5.1",
6466
"rimraf": "^3.0.2",
67+
"spectron": "^10.0.1",
6568
"xo": "^0.29.0"
6669
},
6770
"xo": {

readme.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,14 @@ yarn run build
9898

9999
Builds the app for macOS, Linux, and Windows, using [electron-builder](https://github.com/electron-userland/electron-builder).
100100

101+
## Tests
102+
103+
```sh
104+
yarn test
105+
```
106+
107+
Uses [Spectron](https://www.electronjs.org/spectron) to test the app.
108+
101109
## License
102110

103111
MIT © [th-ch](https://github.com/th-ch/youtube-music)

tests/environment.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
const path = require("path");
2+
3+
const getPort = require("get-port");
4+
const NodeEnvironment = require("jest-environment-node");
5+
const { Application } = require("spectron");
6+
7+
class TestEnvironment extends NodeEnvironment {
8+
constructor(config) {
9+
super(config);
10+
}
11+
12+
async setup() {
13+
await super.setup();
14+
15+
const electronPath = path.resolve(
16+
__dirname,
17+
"..",
18+
"node_modules",
19+
".bin",
20+
"electron"
21+
);
22+
const appPath = path.resolve(__dirname, "..");
23+
const port = await getPort();
24+
25+
this.global.__APP__ = new Application({
26+
path: electronPath,
27+
args: [appPath],
28+
port,
29+
});
30+
await this.global.__APP__.start();
31+
const { client } = this.global.__APP__;
32+
await client.waitUntilWindowLoaded();
33+
}
34+
35+
async teardown() {
36+
if (this.global.__APP__.isRunning()) {
37+
await this.global.__APP__.stop();
38+
}
39+
await super.teardown();
40+
}
41+
42+
runScript(script) {
43+
return super.runScript(script);
44+
}
45+
}
46+
47+
module.exports = TestEnvironment;

tests/index.test.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
describe("YouTube Music App", () => {
2+
const app = global.__APP__;
3+
4+
test("With default settings, app is launched and visible", async () => {
5+
expect(app.isRunning()).toBe(true);
6+
7+
const win = app.browserWindow;
8+
9+
const isVisible = await win.isVisible();
10+
expect(isVisible).toBe(true);
11+
12+
const { width, height } = await win.getBounds();
13+
expect(width).toBeGreaterThan(0);
14+
expect(height).toBeGreaterThan(0);
15+
16+
const { client } = app;
17+
const title = await client.getTitle();
18+
expect(title).toEqual("YouTube Music");
19+
});
20+
});

utils/testing.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
const isTesting = () => process.env.NODE_ENV === "test";
2+
3+
module.exports = { isTesting };

0 commit comments

Comments
 (0)