Electron Debugging and Testing
Electron apps involve main process and renderer process two runtime environments, so debugging and testing strategies need to be designed for different processes.
A good debugging and testing workflow can improve development efficiency and app stability.
Debugging the main process
Debugging with Chrome DevTools
- The Electron main process can be started via
--inspector--inspect-brkand debugged with Chrome DevTools. -
Startup example:
electron --inspect=9222 .
- Enter in the Chrome address bar
chrome://inspectto connect to the main process for breakpoint debugging, viewing the call stack, and evaluating expressions.
VS Code breakpoint debugging
-
Configure in VS Code
launch.json:{ "type": "node", "request": "launch", "name": "Electron Main", "program": "${workspaceFolder}/node_modules/electron/cli.js", "args": ["."], "runtimeExecutable": null, "windows": { "runtimeExecutable": "${workspaceFolder}/node_modules/.bin/electron.cmd" }, "sourceMaps": true, "protocol": "inspector"
}
- This lets you set breakpoints directly in main process files to debug logic and IPC communication.
Logging
- Use
console.logor a third-party logging library (such aswinston) to record main process runtime information. - Recommended log categories: info, warn, error, to help locate issues.
Debugging the renderer process
Opening developer tools
-
Call in the renderer process:
mainWindow.webContents.openDevTools()
- You can directly inspect the DOM, debug JS, network requests, and the performance panel.
Remote debugging
-
The renderer process can also open a remote debugging port:
electron --remote-debugging-port=9223 .
- You can connect to remote debugging in Chrome DevTools, suitable for multi-window or cross-device debugging.
React / Vue DevTools
-
For renderer processes using frontend frameworks, install the corresponding DevTools:
npm install --save-dev electron-devtools-installer
-
Example:
const { default: installExtension, REACT_DEVELOPER_TOOLS } = require('electron-devtools-installer')
app.whenReady().then(() => { installExtension(REACT_DEVELOPER_TOOLS) .then(name => console.log(已安装: ${name})) .catch(err => console.log('Installation failed:', err)) })
- Can debug component state, Props, and events, improving frontend development efficiency.
Automated testing
Spectron testing framework
- Spectron is the end-to-end testing tool officially recommended by Electron, based on WebDriver.
- Can test app startup, window behavior, button clicks, and IPC calls.
npm install --save-dev spectron mocha chai
-
Example:
const Application = require('spectron').Application
const assert = require('chai').assert const path = require('path')
describe('App Startup Test', function () { this.timeout(10000) let app
before(async () => { app = new Application({ path: require('electron'), // Electron executable file args: [path.join(__dirname, '..')] }) await app.start() })
after(async () => { if (app && app.isRunning()) await app.stop() })
it('Window count should be 1', async () => { const count = await app.client.getWindowCount() assert.equal(count, 1) }) })
Unit testing
- You can write unit tests for main process logic and renderer process modules.
- Jest or Mocha + Chai recommended: npm install --save-dev jest
End-to-end testing
- Test user operation flows, such as clicking buttons, opening new windows, and whether data interaction works correctly.
- Can be combined with Spectron or Playwright + Electron.
CI/CD Integration
Configure Electron tests in continuous integration platforms (GitHub Actions, GitLab CI):
- Install Node.js + Electron
- Run unit tests and end-to-end tests
- Generate and upload coverage reports
Summary
Key points of Electron debugging and testing:
- Main process: Chrome DevTools or VS Code breakpoint debugging + logging
- Renderer process: developer tools + frontend framework DevTools
- Automated testing: Spectron, unit tests, end-to-end tests + CI/CD
- Goals: improve development efficiency, discover potential issues, ensure app stability