Lesson 18 of 28 · javascript
Testing Electron Apps – Spectron & Playwright
Duration: 11 mins
Testing Electron Apps – Spectron & Playwright
Spectron (official but now deprecated)
npm install --save-dev spectron mocha
const { Application } = require('spectron');
const assert = require('assert');
describe('App launch', function(){
this.timeout(10000);
let app;
beforeEach(()=>{ app = new Application({ path: require('electron'), args: ['.'] }); return app.start(); });
afterEach(()=> app && app.stop());
it('shows a window', async()=>{ const count = await app.client.getWindowCount(); assert.equal(count, 1); });
});
Playwright (modern, cross‑platform)
npm install --save-dev @playwright/test electron
// tests/electron.test.js
const { test, expect } = require('@playwright/test');
const { _electron: electron } = require('playwright');
test('app has title', async () => {
const app = await electron.launch({ args: ['.'] });
const window = await app.firstWindow();
await expect(window).toHaveTitle(/MyApp/);
});
Playwright can also record videos and run headless CI.
Unit testing the main process
Use any Node test runner (Jest, Mocha) because the main process is just a Node script.
// main.test.js
const { app } = require('electron');
test('app is not null', () => { expect(app).toBeDefined(); });