Merge pull request #24 from th-ch/tests

[Tests] Add integration tests
This commit is contained in:
th-ch
2020-05-03 13:35:41 +02:00
committed by GitHub
10 changed files with 2453 additions and 39 deletions

View File

@ -9,6 +9,8 @@ jobs:
osx_image: xcode11.3
- os: linux
dist: xenial
services:
- xvfb
cache:
yarn: false
@ -18,6 +20,7 @@ cache:
script:
- |
yarn test
if [ "$TRAVIS_OS_NAME" == "linux" ]; then
yarn run release:linux
else

View File

@ -21,6 +21,11 @@ install:
# # https://www.appveyor.com/docs/how-to/rdp-to-build-worker/
# - ps: $blockRdp = $true; iex ((new-object net.webclient).DownloadString('https://raw.githubusercontent.com/appveyor/ci/master/scripts/enable-rdp.ps1'))
# @FIXME: tests disabled because app fails to launch on AppVeyor/Windows
# os: unstable # https://github.com/electron-userland/spectron#on-appveyor
# test_script:
# - yarn test
build_script:
- yarn run release:win

View File

@ -14,6 +14,7 @@ const {
store,
} = require("./store");
const { fileExists, injectCSS } = require("./plugins/utils");
const { isTesting } = require("./utils/testing");
const { setUpTray } = require("./tray");
const app = electron.app;
@ -49,7 +50,7 @@ function createMainWindow() {
backgroundColor: "#000",
show: false,
webPreferences: {
nodeIntegration: false,
nodeIntegration: isTesting(), // Only necessary when testing with Spectron
preload: path.join(__dirname, "preload.js"),
nativeWindowOpen: true, // window.open return Window object(like in regular browsers), not BrowserWindowProxy
affinity: "main-window", // main window, and addition windows should work in one process

7
jest.config.js Normal file
View File

@ -0,0 +1,7 @@
module.exports = {
globals: {
__APP__: undefined, // A different app will be launched in each test environment
},
testEnvironment: "./tests/environment",
testTimeout: 30000, // 30s
};

View File

@ -26,7 +26,7 @@
}
},
"scripts": {
"test": "xo",
"test": "jest",
"start": "electron .",
"icon": "rimraf assets/generated && electron-icon-maker --input=assets/youtube-music.png --output=assets/generated",
"generate:package": "node utils/generate-package-json.js",
@ -61,7 +61,10 @@
"electron-builder": "^22.4.1",
"electron-devtools-installer": "^3.0.0",
"electron-icon-maker": "0.0.4",
"get-port": "^5.1.1",
"jest": "^25.5.1",
"rimraf": "^3.0.2",
"spectron": "^10.0.1",
"xo": "^0.29.0"
},
"xo": {

View File

@ -98,6 +98,14 @@ yarn run build
Builds the app for macOS, Linux, and Windows, using [electron-builder](https://github.com/electron-userland/electron-builder).
## Tests
```sh
yarn test
```
Uses [Spectron](https://www.electronjs.org/spectron) to test the app.
## License
MIT © [th-ch](https://github.com/th-ch/youtube-music)

47
tests/environment.js Normal file
View File

@ -0,0 +1,47 @@
const path = require("path");
const getPort = require("get-port");
const NodeEnvironment = require("jest-environment-node");
const { Application } = require("spectron");
class TestEnvironment extends NodeEnvironment {
constructor(config) {
super(config);
}
async setup() {
await super.setup();
const electronPath = path.resolve(
__dirname,
"..",
"node_modules",
".bin",
"electron"
);
const appPath = path.resolve(__dirname, "..");
const port = await getPort();
this.global.__APP__ = new Application({
path: electronPath,
args: [appPath],
port,
});
await this.global.__APP__.start();
const { client } = this.global.__APP__;
await client.waitUntilWindowLoaded();
}
async teardown() {
if (this.global.__APP__.isRunning()) {
await this.global.__APP__.stop();
}
await super.teardown();
}
runScript(script) {
return super.runScript(script);
}
}
module.exports = TestEnvironment;

20
tests/index.test.js Normal file
View File

@ -0,0 +1,20 @@
describe("YouTube Music App", () => {
const app = global.__APP__;
test("With default settings, app is launched and visible", async () => {
expect(app.isRunning()).toBe(true);
const win = app.browserWindow;
const isVisible = await win.isVisible();
expect(isVisible).toBe(true);
const { width, height } = await win.getBounds();
expect(width).toBeGreaterThan(0);
expect(height).toBeGreaterThan(0);
const { client } = app;
const title = await client.getTitle();
expect(title).toEqual("YouTube Music");
});
});

3
utils/testing.js Normal file
View File

@ -0,0 +1,3 @@
const isTesting = () => process.env.NODE_ENV === "test";
module.exports = { isTesting };

2391
yarn.lock

File diff suppressed because it is too large Load Diff