replace spectron with playwright, rewrite electron tests

This commit is contained in:
Karsten Hassel
2021-11-27 00:19:26 +01:00
parent 2b87d6ec01
commit 4a7f498683
5 changed files with 37 additions and 153 deletions

View File

@@ -56,10 +56,10 @@
"jsdom": "^18.0.0",
"lodash": "^4.17.21",
"nyc": "^15.1.0",
"playwright": "^1.16.3",
"prettier": "^2.4.1",
"pretty-quick": "^3.1.1",
"sinon": "^11.1.2",
"spectron": "^15.0.0",
"stylelint": "^13.13.1",
"stylelint-config-prettier": "^9.0.3",
"stylelint-config-standard": "^22.0.0",
@@ -112,9 +112,6 @@
"displayName": "electron",
"testMatch": [
"**/tests/electron/**/*.[jt]s?(x)"
],
"testPathIgnorePatterns": [
"<rootDir>/tests/electron/global-setup.js"
]
},
{

View File

@@ -1,11 +0,0 @@
/* Magic Mirror Test config sample environment
*
* By Rodrigo Ramírez Norambuena https://rodrigoramirez.com
* MIT Licensed.
*/
let config = require(process.cwd() + "/tests/configs/default.js").configFactory();
/*************** DO NOT EDIT THE LINE BELOW ***************/
if (typeof module !== "undefined") {
module.exports = config;
}

View File

@@ -1,53 +0,0 @@
const helpers = require("./global-setup");
describe("Development console tests", function () {
helpers.setupTimeout(this);
let app = null;
beforeAll(function () {
// Set config sample for use in test
process.env.MM_CONFIG_FILE = "tests/configs/env.js";
});
describe("Without 'dev' commandline argument", function () {
beforeAll(function () {
return helpers
.startApplication({
args: ["js/electron.js"]
})
.then(function (startedApp) {
app = startedApp;
});
});
afterAll(function () {
return helpers.stopApplication(app);
});
it("should not open dev console when absent", async function () {
await app.client.waitUntilWindowLoaded();
return expect(await app.browserWindow.isDevToolsOpened()).toBe(false);
});
});
describe("With 'dev' commandline argument", function () {
beforeAll(function () {
return helpers
.startApplication({
args: ["js/electron.js", "dev"]
})
.then(function (startedApp) {
app = startedApp;
});
});
afterAll(function () {
return helpers.stopApplication(app);
});
it("should open dev console when provided", async function () {
expect(await app.client.getWindowCount()).toBe(2);
});
});
});

View File

@@ -1,40 +1,44 @@
const helpers = require("./global-setup");
// see https://playwright.dev/docs/api/class-electronapplication
const { _electron: electron } = require("playwright");
let electronApp = null;
process.env.MM_CONFIG_FILE = "tests/configs/modules/display.js";
describe("Electron app environment", function () {
helpers.setupTimeout(this);
let app = null;
beforeAll(function () {
// Set config sample for use in test
process.env.MM_CONFIG_FILE = "tests/configs/env.js";
beforeEach(async function () {
electronApp = await electron.launch({ args: ["js/electron.js"] });
});
beforeEach(function () {
return helpers
.startApplication({
args: ["js/electron.js"]
})
.then(function (startedApp) {
app = startedApp;
});
afterEach(async function () {
await electronApp.close();
});
afterEach(function () {
return helpers.stopApplication(app);
});
it("should open a browserwindow", async function () {
await app.client.waitUntilWindowLoaded();
app.browserWindow.focus();
expect(await app.client.getWindowCount()).toBe(1);
expect(await app.browserWindow.isMinimized()).toBe(false);
expect(await app.browserWindow.isDevToolsOpened()).toBe(false);
expect(await app.browserWindow.isVisible()).toBe(true);
expect(await app.browserWindow.isFocused()).toBe(true);
const bounds = await app.browserWindow.getBounds();
expect(bounds.width).toBeGreaterThan(0);
expect(bounds.height).toBeGreaterThan(0);
expect(await app.browserWindow.getTitle()).toBe("MagicMirror²");
it("should open browserwindow", async function () {
expect(await electronApp.windows().length).toBe(1);
const page = await electronApp.firstWindow();
expect(await page.title()).toBe("MagicMirror²");
expect(await page.isVisible("body")).toBe(true);
const module = page.locator("#module_0_helloworld");
await module.waitFor();
expect(await module.textContent()).toContain("Test Display Header");
});
});
describe("Development console tests", function () {
beforeEach(async function () {
electronApp = await electron.launch({ args: ["js/electron.js", "dev"] });
});
afterEach(async function () {
await electronApp.close();
});
it("should open browserwindow and dev console", async function () {
const pageArray = await electronApp.windows();
expect(pageArray.length).toBe(2);
for (const page of pageArray) {
expect(["MagicMirror²", "DevTools"]).toContain(await page.title());
}
});
});

View File

@@ -1,53 +0,0 @@
/*
* Magic Mirror Global Setup Test Suite
*
* By Rodrigo Ramírez Norambuena https://rodrigoramirez.com
* MIT Licensed.
*/
const Application = require("spectron").Application;
const assert = require("assert");
const path = require("path");
const EventEmitter = require("events");
exports.getElectronPath = function () {
let electronPath = path.join(__dirname, "..", "..", "node_modules", ".bin", "electron");
if (process.platform === "win32") {
electronPath += ".cmd";
}
return electronPath;
};
// Set timeout - if this is run as CI Job, increase timeout
exports.setupTimeout = function (test) {
if (process.env.CI) {
jest.setTimeout(30000);
} else {
jest.setTimeout(10000);
}
};
exports.startApplication = function (options) {
const emitter = new EventEmitter();
emitter.setMaxListeners(100);
options.path = exports.getElectronPath();
if (process.env.CI) {
options.startTimeout = 30000;
}
const app = new Application(options);
return app.start().then(function () {
assert.strictEqual(app.isRunning(), true);
return app;
});
};
exports.stopApplication = function (app) {
if (!app || !app.isRunning()) {
return;
}
return app.stop().then(function () {
assert.strictEqual(app.isRunning(), false);
});
};